Export EXCEL from the Asp.net Client

Source: Internet
Author: User

For a complete system, the export function should be essential. Here we will share with youWebHow to export a project on the clientExcel.

Below is the export FunctionCode:

# Region export excel at the client /// <summary> /// export the datatable content to excel and return to the client /// </Summary> /// <Param name = "dtdata "> datatable to be exported </param> // <Param name =" strfilename "> File Name of the exported Excel file </param> Public static void datatabletoexcel (system. data. datatable dtdata, string strfilename) {system. web. UI. webcontrols. dataGrid dgexport = NULL; // The current Dialog System. web. httpcontext curcontext = system. web. httpcontext. current; // Io is used to export and return exc El file system. Io. stringwriter strwriter = NULL; system. Web. UI. htmltextwriter htmlwriter = NULL; If (dtdata! = NULL) {curcontext. response. clearcontent (); curcontext. response. buffer = true; curcontext. response. write ("<metahttp-equiv = Content-Type content = text/html; charset = UTF-8>"); curcontext. response. contenttype = "application/Excel"; // set the file name curcontext. response. appendheader ("content-disposition", "attachment; filename =" + strfilename); curcontext. response. charset = "utf8"; // export the Excel file strwriter = new system. io. stringwriter (); htmlwriter = new system. web. UI. htmltextwriter (strwriter); // to solve the possible paging problem in dgdata, You need to redefine a non-Paging DataGrid dgexport = new system. web. UI. webcontrols. dataGrid (); dgexport. datasource = dtdata. defaultview; dgexport. allowpaging = false; dgexport. databind (); // return the client dgexport. rendercontrol (htmlwriter); curcontext. response. write (strwriter. tostring (); curcontext. response. end () ;}# endregion

Code for calling this function:

 
Tabletoexcel. datatabletoexcel (dtteacherexpscore, server. urlencode ("instructor score" + ". xls "));

Note: The Chinese character file name must use server. urlencode.Function encoding. Otherwise, garbled characters may occur.

So far, the client can export excel. However, when I tried to export a numeric string starting with "0", I found that "0" at the beginning of the exported Excel file is missing, for example: 01234, shown in Excel as 1234. at this point, I realized that this method is not perfect.

It turns out that "0" is mysteriously missing, and the problem occurs in the display mode of the Excel cell. Knowing the problem, add two lines of code to solve the problem perfectly.

The final export code is:

# Region export excel at the client /// <summary> /// export the datatable content to excel and return to the client /// </Summary> /// <Param name = "dtdata "> datatable to be exported </param> // <Param name =" strfilename "> File Name of the exported Excel file </param> Public static void datatabletoexcel (system. data. datatable dtdata, string strfilename) {system. web. UI. webcontrols. dataGrid dgexport = NULL; // The current Dialog System. web. httpcontext curcontext = system. web. httpcontext. current; // Io is used to export and return exc El file system. Io. stringwriter strwriter = NULL; system. Web. UI. htmltextwriter htmlwriter = NULL; If (dtdata! = NULL) {curcontext. response. clearcontent (); curcontext. response. buffer = true; curcontext. response. write ("<metahttp-equiv = Content-Type content = text/html; charset = UTF-8>"); curcontext. response. contenttype = "application/Excel"; // set the file name curcontext. response. appendheader ("content-disposition", "attachment; filename =" + strfilename); curcontext. response. charset = "utf8"; // export the Excel file strwriter = new system. io. stringwriter (); htmlwriter = new system. web. UI. htmltextwriter (strwriter); // to solve the possible paging problem in dgdata, You need to redefine a non-Paging DataGrid dgexport = new system. web. UI. webcontrols. dataGrid (); dgexport. datasource = dtdata. defaultview; dgexport. allowpaging = false; dgexport. databind (); // set it to the text format string strstyle = "<style> TD {MSO-number-format: \" \\@\" ;}</style> "; // set the format of the exported Excel text to prevent this situation: data is 0111, and output is 111 strwriter. writeline (strstyle); // return the client dgexport. rendercontrol (htmlwriter); curcontext. response. write (strwriter. tostring (); curcontext. response. end () ;}# endregion

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.