Asp.net import excel or Word Document code

Source: Internet
Author: User

Asp tutorial. net import excel or Word Document code
 
// Describe the use of enableeventvalidation = "false;
// When an input server control (such as textbox) is added to the page, you must set it to false. Otherwise, an error is returned. That is, page verification is disabled. The default setting is enabled.


Private void dbexport ()
{
Httpcontext. current. response. charset = "gb2312 ";
Httpcontext. current. response. contentencoding = encoding. utf8;
// Some articles use utf7 because garbled characters may occur in Chinese in special cases. utf8 is recommended. utf7 is not highly secure;
// The following two lines can ensure their correctness. For usage instructions, see the code.
// Response. write ("// Response. write ("</body> // Encode the file name to prevent Chinese name garbled characters.
Httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode ("file name .xls", encoding. utf8 ));
// Export the excelformat. The format is different, and the suffix of the file name must be changed accordingly (.xls)
Httpcontext. current. response. contenttype = "vnd. ms-excel ";
// Export the wordformat, which varies with the format and is suffixed with the file name (. DOC)
// Httpcontext. current. response. contenttype = "vnd. ms-word ";
// Export the HTML format. The format is different, and the suffix of the file name must be different (.html)
// Httpcontext. current. response. contenttype = "text/html ";

// There are two other ways of writing that seem to be able to directly output the image and not to validate it, but it should not be possible to change the format like above, you must create a graphical object before setting response. contenttype can be output
// Which one has a simple method to post it? Learn it. Thank you.
// Httpcontext. current. response. contenttype = "image/gif ";
// Httpcontext. current. response. contenttype = "image/jpeg ";

// Describe what divid is. Here it should be the control you want to transfer out, it can be a server control or an html control (to add runat = "server" or else the control cannot be found here)
// A div id = "divid" runat = "server" in my page contains a gridview used to display data.
Divid. page. enableviewstate = false;
System. io. stringwriter tw = new system. io. stringwriter ();
Htmltextwriter hw = new htmltextwriter (tw );
Divid. rendercontrol (hw );
// The following three rows are the data output.
Response. write ("Httpcontext. current. response. write (tw. tostring ());
Response. write ("</body>

Response. flush ();
Response. close ();
}
// This method must be rewritten; otherwise, an error is returned.
Public override void verifyrenderinginserverform (control)
{

}

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.