.NET 使用OleDbConnection读取Excel文件

来源:互联网 发布:windows键位组合关了 编辑:程序博客网 时间:2024/05/14 07:32
 DataSet ds;            string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'";            OleDbConnection conn = new OleDbConnection(conStr);            conn.Open();            try            {                string strExcel = "";                OleDbDataAdapter myCommand = null;                strExcel = "select * from [sheet1$]";                myCommand = new OleDbDataAdapter(strExcel, conStr);                ds = new DataSet();                myCommand.Fill(ds, "table1");            }            catch (Exception ex)            {                //读取Excel失败 返回失败                result = 1;                return;            }            finally            {                conn.Close();                conn.Dispose();            }

如果读取出来的数字被转为了科学计数法 ,将Excel对应列格式改为常规即可
原创粉丝点击