Use the Excel library component to export data to excel

Source: Internet
Author: User
Run the following command to obtain the latest Excel library version through nuget: PM> install-package excellibrary

 

Use ASP. NET as follows:

Encapsulate methods first:

 
Public void exportexcel (string exclefilename, datatable DT) {httpcontext context = httpcontext. current; context. response. clear (); // httpcontext. current. response. charset = "gb2312"; // httpcontext. current. response. contentencoding = system. text. encoding. getencoding ("gb2312"); context. response. addheader ("content-disposition", String. format ("attachment; filename = {0}", excelfilename); context. response. contenttype = "application/MS-excel"; memorystream M = new memorystream (); excellibrary. datasethelper. createworkbook (M, DT. dataset); context. response. binarywrite (Ms. toarray (); context. response. end ();}

Parameter description:

Excelfilename: name of the exported Excel File

DT: data. You can set a name for the able. In this way, the sheet name of the exported data will be available,CodeWrite like this

Datatable dt = .........

DT. tablename = "Sheet Name ";

Exportexcel('excel.xls ', DT );

 

In this way, the data is exported to excel! Try it.

 

 

 

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.