C#读取Excel文件方法一:直接读取(这种直接读取单元格的方法释放很重要)
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了三原免费建站欢迎大家使用!
- Excel.Applicationexcel=null;
- Excel.Workbookswbs=null;
- Excel.Workbookwb=null;
- Excel.Worksheetws=null;
- Excel.Rangerange1=null;
- objectNothing=System.Reflection.Missing.Value;
- try
- {
- excel=newExcel.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"];
- introwCount=ws.UsedRange.Rows.Count;
- intcolCount=ws.UsedRange.Columns.Count;
- if(rowCount<=0)
- thrownewInvalidFormatException
- ("文件中没有数据记录");
- if(colCount<4)
- thrownewInvalidFormatException
- ("字段个数不对");
- for(inti=0;i{
- this.rowNo=i+1;
- object[]row=newobject[4];
- for(intj=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;
- DataRowdataRow=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();
- }
- }
C#读取Excel文件方法二:通过OleDb连接,把excel文件作为数据源来读取(这里是fill进dataset,也可以返回OleDbDataReader来逐行读,数据较快)
注:这种方法容易把混合型的字段作为null值读取进来,解决办法是改造连接字符串
- strConn = "Provider=Microsoft.Jet.
- OLEDB.4.0;Data Source=C:\\Erp1912.xls;Extended
- Properties='Excel8.0;HDR=Yes;IMEX=1'";
通过Imex=1来把混合型作为文本型读取,避免null值,来实现C#读取Excel文件
- privateDataSetimportExcelToDataSet
- (stringFilePath)
- {
- stringstrConn;
- strConn="Provider=Microsoft.Jet.
- OLEDB.4.0;"+"DataSource="+FilePath+";
- ExtendedProperties=Excel8.0;";
- OleDbConnectionconn=newOleDbConnection
- (strConn);
- OleDbDataAdaptermyCommand=newOleDbDataAdapter
- ("SELECT*FROM[Sheet1$]",strConn);
- DataSetmyDataSet=newDataSet();
- try
- {
- myCommand.Fill(myDataSet);
- }
- catch(Exceptionex)
- {
- thrownewInvalidFormatException
- ("该Excel文件的工作表的名字不正确,"+ex.Message);
- }
- returnmyDataSet;
- }
网站栏目:两招搞定C#读取Excel文件
分享URL:http://www.shufengxianlan.com/qtweb/news8/244358.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联