C#npoi reading Excel is much better than interop and Microsoft.Jet.OLEDB.4.0.

Source: Internet
Author: User
Tags rowcount

This afternoon began to tidy up this piece of Excel, Microsoft made those libraries is a pit, what COM registration and the like are all the errors.

Search information on the internet happened to meet Npoi, good things, worth using

Npoi is a program built on a POI 3.x version that npoi can read or write to a Word or Excel document without installing Office. Npoi is an open source C # project that reads and writes Microsoft OLE2 component documents such as Excel, Word, and so on. Https://github.com/tonyqus/npoi I was in the project inside NuGet directly search Npoi installation directly can be used on the Internet a better program
usingSystem;usingSystem.Data;usingSystem.IO;usingNpoi. Ss. Usermodel;usingNpoi. XSSF. Usermodel;usingNpoi. HSSF. Usermodel;namespacemakedata{ Public classReadexcelhelper {/// <summary>        ///import Excel into a DataTable/// </summary>        /// <param name= "FilePath" >Excel Path</param>        /// <param name= "Iscolumnname" >whether the first row is a column name</param>        /// <returns>Back to DataTable</returns>         Public StaticDataTable exceltodatatable (stringFilePath,BOOLiscolumnname) {DataTable DataTable=NULL; FileStream FS=NULL; DataColumn column=NULL; DataRow DataRow=NULL; Iworkbook Workbook=NULL; Isheet sheet=NULL; IRow Row=NULL; Icell Cell=NULL; intStartRow =0; Try            {                using(fs =File.openread (FilePath)) {                    //version 2007                    if(Filepath.indexof (". xlsx") >0) Workbook=NewXssfworkbook (FS); //Version 2003                    Else if(Filepath.indexof (". xls") >0) Workbook=NewHssfworkbook (FS); if(Workbook! =NULL) {Sheet= Workbook. Getsheetat (0);//read the first sheet, and of course you can iterate through each sheetDataTable =NewDataTable (); if(Sheet! =NULL)                        {                            intRowCount = sheet. Lastrownum;//total number of rows                            if(RowCount >0) {IRow firstrow= Sheet. GetRow (0);//First line                                intCellcount = Firstrow.lastcellnum;//Number of columns//to build a column for a DataTable                                if(iscolumnname) {StartRow=1;//If the first row is a column name, start reading from the second row                                     for(inti = Firstrow.firstcellnum; i < Cellcount; ++i) {cell=Firstrow.getcell (i); if(Cell! =NULL)                                        {                                            if(Cell. Stringcellvalue! =NULL) {column=NewDataColumn (cell.                                                Stringcellvalue);                                            DATATABLE.COLUMNS.ADD (column);                                }                                        }                                    }                                } Else                                {                                     for(inti = Firstrow.firstcellnum; i < Cellcount; ++i) {column=NewDataColumn ("column"+ (i +1));                                    DATATABLE.COLUMNS.ADD (column); }                                }                                //Fill Row                                 for(inti = StartRow; I <= RowCount; ++i) {row=sheet.                                    GetRow (i); if(Row = =NULL)Continue; DataRow=Datatable.newrow ();  for(intj = row. Firstcellnum; J < Cellcount; ++j) {cell=row.                                        Getcell (j); if(Cell = =NULL) {Datarow[j]=""; }                                        Else                                        {                                            //Celltype (Unknown = -1,numeric = 0,string = 1,formula = 2,blank = 3,boolean = 4,error = 5,)                                            Switch(cell. Celltype) { CaseCelltype.blank:datarow[j]="";  Break;  CaseCelltype.numeric: ShortFormat =cell.                                                    Cellstyle.dataformat; //Handling of time formats (2015.12.5, 2015/12/5, 2015-12-5, etc.)                                                    if(Format = = -|| Format = = to|| Format = = $|| Format = = -) Datarow[j]=cell.                                                    Datecellvalue; ElseDatarow[j]=cell.                                                    Numericcellvalue;  Break;  CaseCelltype.string:datarow[j]=cell.                                                    Stringcellvalue;  Break; }}} Datatab Le.                                Rows.Add (DataRow); }                            }                        }                    }                }                returndataTable; }            Catch(Exception ex) {stringMess =Ex.                Message; if(FS! =NULL) {fs.                Close (); }                return NULL; }        }    }}


C#npoi reading Excel is much better than interop and Microsoft.Jet.OLEDB.4.0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.