Export and write standard table data such as the visual DataGrid and datalist to the xls or DOC format for storage.

Source: Internet
Author: User
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 ();
}

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.