Personal Project Practice, feasible
Asp.net C # export Excel
/// <Summary>
/// Al: columnname of the record set to be printed: Display name of the header
/// </Summary>
Public static void outtoexcel (arraylist Al, string [] columnname ){
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application ();
Microsoft. Office. InterOP. Excel. Workbook workbook = excel. application. workbooks. Add (true );
Microsoft. office. interOP. excel. _ worksheet wsheet = (Microsoft. office. interOP. excel. _ worksheet) workbook. worksheets. add (type. missing, type. missing, 1, type. missing );
Wsheet. Name = "report ";
Int I = 1;
Foreach (string [] STR in Al)
{
If (I = 1) // Header
{
// Object [] columnname = daobean. gettablecolumnname (tableflag );
Int K = 0;
// Foreach (dictionarycontent str2 in columnname)
//{
Foreach (string strname in columnname)
{
Excel. cells [1, k + 1] = strname; // header Initialization
K ++;
}
//}
}
I ++;
For (Int J = 0; j <Str. length; j ++)
{// Enter the internal loop to add table columns
Excel. cells [I, j + 1] = STR [J];
}
}
Excel. Visible = true;
// Response. Write ("<SCRIPT> parent. delscreenconvert (); </SCRIPT> ");
// ======
String filename = Global. getramfilename ();
Workbook. savecopyas (system. Web. httpcontext. Current. server. mappath (".") + "// Excel //" + filename + ". xls ");
Workbook. Close (false, null, null );
Excel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (workbook );
System. runtime. interopservices. Marshal. releasecomobject (Excel );
System. runtime. interopservices. Marshal. releasecomobject (wsheet );
Workbook = NULL;
Excel = NULL;
Wsheet = NULL;
String Path = system. Web. httpcontext. Current. server. mappath ("Excel //" + filename + ". xls ");
System. Io. fileinfo file = new system. Io. fileinfo (PATH );
System. Web. httpcontext. Current. response. Clear ();
System. Web. httpcontext. Current. response. charset = "UTF-8 ";
System. Web. httpcontext. Current. response. contentencoding = system. Text. encoding. utf8;
// Add the header information and specify the default file name for the "download/Save as" dialog box
System. web. httpcontext. current. response. addheader ("content-disposition", "attachment; filename =" + system. web. httpcontext. current. server. urlencode (file. name ));
// Add header information and specify the file size so that the browser can display the download progress.
System. Web. httpcontext. Current. response. addheader ("Content-Length", file. length. tostring ());
// Specify a stream that cannot be read by the client and must be downloaded.
System. Web. httpcontext. Current. response. contenttype = "application/MS-excel ";
// Send the file stream to the client
System. Web. httpcontext. Current. response. writefile (file. fullname );
// Stop page execution
System. Web. httpcontext. Current. response. End ();
// Checkboxlist1.items. Clear ();
}