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 "

Source: Internet
Author: User
Tags rowcount

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 "

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.