Npoi Export the xlsx format file, the following conditions can occur:
Click "Yes":
The export code is as follows:
/// <summary> ///write DataTable data to Excel and download/// </summary> /// <param name= "DT" >DataTable</param> /// <param name= "Excelname" >file name</param> /// <param name= "TemplatePath" >Template Path</param> /// <returns></returns> Public Static voidDatatabletoexcelanddownload (DataTable DT,stringExcelname,stringTemplatePath) {Iworkbook Workbook=NULL; FileStream FS=NULL; IRow Row=NULL; Isheet sheet=NULL; Icell Cell=NULL; Icellstyle CellStyle=NULL; Try { if(dt! =NULL&& dt. Rows.Count >0) { varRowCount = dt. Rows.Count;//Number of rows varColumnCount = dt. Columns.count;//Number of columns using(fs =File.openread (TemplatePath)) { //High- volume data export, you need to pay attention to such a problem, Excel2003 format a sheet only support 65536 lines, Excel 2007 is more, is 1048576//workbook = new Hssfworkbook (fs);//Version 2003. xlsWorkbook =NewXssfworkbook (FS);//version 2007. xlsx } if(Workbook! =NULL) {Sheet= Workbook. Getsheetat (0);//read the first sheet//set the cells for each column in each row, for(vari =0; i < RowCount; i++) {row= Sheet. CreateRow (i +1); for(varj =0; J < ColumnCount; J + +) {cell=row. Createcell (j); varValue =dt. ROWS[I][J]; varBdtype =value. GetType (). ToString (); Switch(bdtype) { Case "System.String": Cell. Setcellvalue (value. ToString ()); Break; Case "System.DateTime"://Date Typecell. Setcellvalue (Convert.todatetime (value. ToString ()). ToString ("YYYY-MM-DD HH:mm:ss")); Break; Case "system.int16"://Integral type Case "System.Int32": Case "System.Int64": Case "System.Byte": varIntv =0; int. TryParse (value. ToString (), outIntv); Cell. Setcellvalue (INTV); Break; Case "System.Decimal"://floating Point Type Case "System.Double": DoubleDOUBV =0; Double. TryParse (value. ToString (), outDOUBV);//Formatting ValuesCellStyle =workbook. Createcellstyle (); Cellstyle.dataformat= Hssfdataformat.getbuiltinformat ("0.00"); Cell. Setcellvalue (DOUBV); Cell. CellStyle=CellStyle; Break; default: Cell. Setcellvalue (value. ToString ()); Break; } } } varContext =HttpContext.Current; Context. Response.Clear (); Context. Response.ContentType="Application/vnd.ms-excel"; Context. Response.appendheader ("content-disposition","attachment; Filename="+excelname); using(varms =NewMemoryStream ()) {Workbook. Write (MS); Long fileSize = Ms. Length; // plus set size download down the. xlsx file is not reported "Excel has completed file-level validation and repair." Some parts of this workbook may have been repaired or discarded "context." Response.AddHeader ("Content-length" , filesize.tostring ()); Context. Response.BinaryWrite (Ms. GetBuffer ()); Context.ApplicationInstance.CompleteRequest (); } } } } Catch(Exception ex) {if(FS! =NULL) {fs. Close (); } ExceptionHandling.ExceptionHandler.HandleException (ex); } }
Plus the setting size downloaded when the. xlsx file is opened, it is not reported that Excel has completed file-level validation and repair. Some parts of this workbook may have been repaired or discarded "
Long fileSize = Ms. Length;
Context. Response.AddHeader ("Content-length", filesize.tostring ());
Can.
ASP. Npoi Export the xlsx format file, open the file "Excel has completed file-level validation and repair." Some parts of this workbook may have been repaired or discarded "