Asp.net C # export Excel Web

Source: Internet
Author: User

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 ();


}

 

 

Related Article

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.