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.