Example Code As follows:
Using system. IO;
Using system. text;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Business. Conn con = new... system. configuration. configurationsettings. deleettings ["conn"]);
Dataset da = new dataset ();
DA = con. Read ("tbl_company", "companyid, companyName, addeddate", "'' ", 125," id <= ");
Datagridhuashanlin . Datasource = da. Tables [0]. defaultview;
Datagridhuashanlin . Databind ();
}
Private void button#click (Object sender, system. eventargs E)
{
Response. Clear (); // clear all data streams in the buffer
Response. Buffer = true; // you can specify whether to buffer data streams.
Response. charset = "gb2312"; // sets the HTTP character set format of the output stream.
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode ("here we can use the Chinese name .xls", system. text. encoding. utf8); // Add an HTTP header before the data stream
// Attachment --- indicates downloading as an attachment
// Inline --- indicates opening online
// If filename is Chinese, you can use httputility. urlencode (filename, system. Text. encoding. utf8) for encoding to solve the problem of garbled file names.
Response. contentencoding = system. Text. encoding. getencoding ("gb2312"); // set the output stream to simplified Chinese
Response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.
// Response. contenttype is the http mime type of the output stream.
// Response. contenttype = "response. contenttype ";
// Application/MS-word --- Word file
// Application/vnd. MS-Excel --- Excel File
This. enableviewstate = false;
System. Globalization. cultureinfo mycitrad = new system. Globalization. cultureinfo ("ZH-CN", true );
System. Io. stringwriter ostringwriter = new system. Io. stringwriter (mycitrad );
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
This. Datagridhuashanlin . Rendercontrol (ohtmltextwriter );
// Page is the object to be exported. It is currently a page. It can be used for DataGrid, datalist, etc.
Response. Write (ostringwriter. tostring ());
Response. End ();
}