C# 读取excel 文件的方法

来源:互联网 发布:手机永不休眠软件 编辑:程序博客网 时间:2024/06/05 18:26

方法一:把EXCEL 当做数据源读取,返回dataset  见代码

 /// <summary>        /// 把EXCEL文件当做一个数据源来进行数据的读取操作        /// </summary>        /// <param name="Path"></param>        /// <returns></returns>        public static DataSet ExcelToDS(string Path)        {            //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";    --适合 2007以前的版本            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";  // 适合 2003 和 2007 HDR=Yes 表示 第一行是列名不是数据            OleDbConnection conn = new OleDbConnection(strConn);            conn.Open();            string strExcel = "";            OleDbDataAdapter myCommand = null;            DataSet ds = null;            strExcel = "select * from [sheet1$]";            myCommand = new OleDbDataAdapter(strExcel, strConn);            ds = new DataSet();            myCommand.Fill(ds, "table1");            return ds;        }   

待续......

0 0