久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

兩招搞定C#讀取Excel文件

 天道酬勤0117 2012-12-19

C#讀取Excel文件方法一:直接讀取(這種直接讀取單元格的方法釋放很重要)

  1. Excel.Applicationexcel=null;  
  2. Excel.Workbookswbs=null;  
  3. Excel.Workbookwb=null;  
  4. Excel.Worksheetws=null;  
  5. Excel.Rangerange1=null;  
  6. objectNothing=System.Reflection.Missing.Value;  
  7.  
  8. try  
  9. {  
  10. excel=newExcel.Application();  
  11. excel.UserControl=true;  
  12. excel.DisplayAlerts=false;  
  13.  
  14. excel.Application.Workbooks.Open(this.  
  15. FilePath,Nothing,Nothing,Nothing,Nothing,  
  16. Nothing,Nothing,Nothing,Nothing,Nothing,  
  17. Nothing,Nothing,Nothing);  
  18.  
  19. wbs=excel.Workbooks;  
  20. wb=wbs[1];  
  21. ws=(Excel.Worksheet)wb.Worksheets["Sheet2"];  
  22.  
  23.  
  24. introwCount=ws.UsedRange.Rows.Count;  
  25. intcolCount=ws.UsedRange.Columns.Count;  
  26. if(rowCount<=0)  
  27. thrownewInvalidFormatException  
  28. ("文件中沒(méi)有數(shù)據(jù)記錄");  
  29. if(colCount<4)  
  30. thrownewInvalidFormatException  
  31. ("字段個(gè)數(shù)不對(duì)");  
  32.  
  33. for(inti=0;i{  
  34. this.rowNo=i+1;  
  35. object[]row=newobject[4];  
  36. for(intj=0;j<4;j++)  
  37. {  
  38. range1=ws.get_Range(ws.Cells[i+2,j+1],  
  39. ws.Cells[i+2,j+1]);  
  40. row[j]=range1.Value;  
  41.  
  42. if(row[0]==null)  
  43. {  
  44. this.isNullRecord++;  
  45. break;  
  46. }  
  47. }  
  48.  
  49. if(this.isNullRecord>0)  
  50. continue;  
  51.  
  52. DataRowdataRow=this.readExcel(row);  
  53.  
  54. if(this.isNullRecord==1)  
  55. continue;  
  56.  
  57. if(this.verifyData(dataRow)==false)  
  58. errFlag++;  
  59.  
  60. this.updateTableCurr(dataRow);  
  61. }  
  62. }  
  63. finally  
  64. {  
  65. if(excel!=null)  
  66. {  
  67. if(wbs!=null)  
  68. {  
  69. if(wb!=null)  
  70. {  
  71. if(ws!=null)  
  72. {  
  73. if(range1!=null)  
  74. {  
  75. System.Runtime.InteropServices.Marshal.  
  76. ReleaseComObject(range1);  
  77. range1=null;  
  78. }  
  79. System.Runtime.InteropServices.Marshal.  
  80. ReleaseComObject(ws);  
  81. ws=null;  
  82. }  
  83. wb.Close(false,Nothing,Nothing);  
  84. System.Runtime.InteropServices.Marshal.  
  85. ReleaseComObject(wb);  
  86. wb=null;  
  87. }  
  88. wbs.Close();  
  89. System.Runtime.InteropServices.Marshal.  
  90. ReleaseComObject(wbs);  
  91. wbs=null;  
  92. }  
  93. excel.Application.Workbooks.Close();  
  94. excel.Quit();  
  95. System.Runtime.InteropServices.Marshal.  
  96. ReleaseComObject(excel);  
  97. excel=null;  
  98. GC.Collect();  
  99. }  
  100. }  

C#讀取Excel文件方法二:通過(guò)OleDb連接,,把excel文件作為數(shù)據(jù)源來(lái)讀取(這里是fill進(jìn)dataset,,也可以返回OleDbDataReader來(lái)逐行讀,數(shù)據(jù)較快)

注:這種方法容易把混合型的字段作為null值讀取進(jìn)來(lái),,解決辦法是改造連接字符串

  1. strConn = "Provider=Microsoft.Jet.  
  2. OLEDB.4.0;Data Source=C:\\Erp1912.xls;Extended   
  3. Properties='Excel8.0;HDR=Yes;IMEX=1'";  

通過(guò)Imex=1來(lái)把混合型作為文本型讀取,,避免null值,來(lái)實(shí)現(xiàn)C#讀取Excel文件

  1. privateDataSetimportExcelToDataSet  
  2. (stringFilePath)  
  3. {  
  4. stringstrConn;  
  5. strConn="Provider=Microsoft.Jet.  
  6. OLEDB.4.0;"+"DataSource="+FilePath+";  
  7. ExtendedProperties=Excel8.0;";  
  8. OleDbConnectionconn=newOleDbConnection  
  9. (strConn);  
  10. OleDbDataAdaptermyCommand=newOleDbDataAdapter  
  11. ("SELECT*FROM[Sheet1$]",strConn);  
  12. DataSetmyDataSet=newDataSet();  
  13. try  
  14. {  
  15. myCommand.Fill(myDataSet);  
  16. }  
  17. catch(Exceptionex)  
  18. {  
  19. thrownewInvalidFormatException  
  20. ("該Excel文件的工作表的名字不正確,"+ex.Message);  
  21. }  
  22. returnmyDataSet;  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多