Npoi reading from Excel to DataTable reading a column of columns

Source: Internet
Author: User

Processing ideas:

1. Open Excel to read with Npoi;

2. Read the first sheet;

During the read process:

A. Set the corresponding column first to not hide

B. When reading a cell, first determine if it contains a formula

The corresponding code is as follows:

 Public StaticDataTable Readdatafromexcelbynpoi () {DataTable dt=NewDataTable (); varFilepathandname = Path.Combine (Server.MapPath ("~/content/excel"),"Excelforuploadtest.xls"); //Open File read Datastream =System.IO.File.Open (filePathAndName2, FileMode.Open); //Create workbook with streamHssfworkbook Workbook =NewHssfworkbook (stream); //get the first sheet of ExcelHssfsheet sheet = (hssfsheet) workbook. Getsheetat (0); //set hidden column not hidden     for(intIhide =0; Ihide <= +; ihide++) {sheet. Setcolumnhidden (Ihide,false); }        //the label of the last column (that is, the total number of rows)    intRowCount =sheet.    Lastrownum; //get the first line of sheetHssfrow HeaderRow = (hssfrow) sheet. GetRow (0); //the number of the last square in a row (that is, the total number of columns)    intCellcount =Headerrow.lastcellnum; stringColumnNames =@"A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, Aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao, Ap,aq,ar,as,at,au,av,aw,ax,ay,az"; string[] columns = Columnnames.split (','); //use A B C D ... Names of the columns of the DataTable in the way of letters     for(inti = Headerrow.firstcellnum; i < Cellcount; i++)    {                //DataColumn column = new DataColumn (Headerrow.getcell (i). Stringcellvalue);DataColumn column =NewDataColumn (Columns[i]); Dt.    Columns.Add (column); }     for(inti = (Iheaderrowindex.value +1); I <= RowCount; i++) {Hssfrow row=(hssfrow) sheet.        GetRow (i); DataRow DataRow=dt.        NewRow (); if(Row! =NULL)        {             for(intj = row. Firstcellnum; J < Cellcount; J + +)            {                if(Row. Getcell (j)! =NULL)                {                    //if the formula cell//Read Only the display value of its cell cells instead of reading the formula                    if(Row. Getcell (j). Celltype = =Celltype.formula) {Datarow[j]=row. Getcell (j).                    Stringcellvalue; }Else{Datarow[j]=row. Getcell (j).                    ToString (); }                }            }        }        //a marked column//a marked column//the value of the non-null indicator is valid data//the value is empty and the indicator ends        if(string. IsNullOrEmpty (datarow[ -]. ToString ())&&string. IsNullOrEmpty (datarow[ -]. ToString ())) { Break;//read end exit For Loop        }        Else{dt.        Rows.Add (DataRow); }} Workbook=NULL; Sheet=NULL; returnDT;}#endregion

Npoi reading from Excel to DataTable reading a column of columns

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.