国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰和县| 荃湾区| 通江县| 灵宝市| 宝应县| 柞水县| 高邮市| 松滋市| 隆回县| 定襄县| 琼海市| 英吉沙县| 桓台县| 威信县| 淮安市| 丘北县| 桂平市| 武穴市| 云南省| 阳东县| 娄底市| 梨树县| 铁力市| 阿拉尔市| 隆林| 河西区| 嵊州市| 门头沟区| 壶关县| 云霄县| 偃师市| 楚雄市| 玉溪市| 泗水县| 响水县| 惠东县| 孝义市| 隆化县| 兴业县| 建水县| 建水县|