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