首页 > 开发 > 综合 > 正文

C#中Excel文件的读取

2024-07-21 02:25:52
字体:
来源:转载
供稿:网友
提供两种方法:一个是直接打开excel文件,然后逐行读取,速度较慢;还有一种方法是通过oledb连接,把excel文件作为数据源来读取
方法一:这种直接读取单元格的方法释放很重要。

   excel.application excel = null;
   excel.workbooks wbs = null;
   excel.workbook wb = null;
   excel.worksheet ws = null;
   excel.range range1 = null;
   object nothing = system.reflection.missing.value;
    
   try
   {
    excel = new excel.application();
    excel.usercontrol = true;
    excel.displayalerts = false;
                    
    excel.application.workbooks.open(this.filepath,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing,nothing ) ;
    
    wbs = excel.workbooks;
    wb = wbs[1];
    ws = (excel.worksheet)wb.worksheets["sheet2"];
    
         
    int rowcount = ws.usedrange.rows.count;
    int colcount = ws.usedrange.columns.count;
    if (rowcount <= 0)
     throw new invalidformatexception("文件中没有数据记录");
    if (colcount < 4 )
     throw new invalidformatexception("字段个数不对");
    
    for (int i = 0;i    {

     this.rowno = i + 1;
     object[] row = new object[4];
     for (int j = 0;j<4;j++)
     {
      range1 = ws.get_range(ws.cells[i+2,j+1],ws.cells[i+2,j+1]);
      row[j] = range1.value;

      if (row[0] == null)
      {
       this.isnullrecord++;
       break;
      }
     }
                   
     if (this.isnullrecord > 0)
      continue;

     datarow datarow = this.readexcel(row);

     if (this.isnullrecord == 1)
      continue;
  
     if (this.verifydata(datarow) == false)
      errflag++;
  
     this.updatetablecurr(datarow);
    }
    
   }
   finally
   {
    if (excel != null)
    {
     if (wbs != null)
     {
      if (wb != null)
      {
       if (ws != null)
       {
        if (range1 != null)
        {
         system.runtime.interopservices.marshal.releasecomobject(range1);
         range1 = null;
        }
        system.runtime.interopservices.marshal.releasecomobject(ws);
        ws = null;
       }
       wb.close(false,nothing,nothing); 
       system.runtime.interopservices.marshal.releasecomobject(wb);
       wb = null;
      }
      wbs.close();
      system.runtime.interopservices.marshal.releasecomobject(wbs);
      wbs = null;
     }
     excel.application.workbooks.close();
     excel.quit();
     system.runtime.interopservices.marshal.releasecomobject(excel);
     excel = null;
     gc.collect();
    }
   }

方法二:这里是fill进dataset,也可以返回oledbdatareader来逐行读,数据较快
注:这种方法容易把混合型的字段作为null值读取进来,解决办法是改造连接字符串
strconn = "provider=microsoft.jet.oledb.4.0;data source=c://erp1912.xls;extended properties='excel 8.0;hdr=yes;imex=1'";
通过imex=1来把混合型作为文本型读取,避免null值,详细请见http://www.connectionstrings.com
private dataset importexceltodataset(string filepath)
  {
   string strconn;
   strconn = "provider=microsoft.jet.oledb.4.0;" + "data source=" + filepath + ";extended properties=excel 8.0;";
   oledbconnection conn = new oledbconnection(strconn);
   oledbdataadapter mycommand = new oledbdataadapter("select * from [sheet1$]",strconn);
   dataset mydataset = new dataset();
   try
   {
    mycommand.fill(mydataset);
   }
   catch(exception ex)
   {
    throw new invalidformatexception("该excel文件的工作表的名字不正确," + ex.message);
   }
   return mydataset;
  }
 
 
 

中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表