asp.net import Excel or Word document code

Source: Internet
Author: User
Tags httpcontext

ASP tutorial. NET import Excel or Word document code

Description of the use of enableeventvalidation= "false";
When you add an input server control (such as a TextBox) to a page, you need to set to false, or you will get an error, which is to turn off page validation, which is turned on by default.


private void Dbexport ()
{
Httpcontext.current.response.charset = "gb2312";
httpcontext.current.response.contentencoding = Encoding.UTF8;
Some articles in the use of UTF7, is because in special circumstances Chinese will appear garbled; Here it is recommended to use utf8,msdn mentioned Utf7 no UTF8 high security;
The following two lines are guaranteed to be correct, using the method shown in the code
Response.Write ("Response.Write ("</body>Here the file name is encoded to prevent the occurrence of Chinese name garbled phenomenon
Httpcontext.current.response.appendheader ("Content-disposition", "attachment;filename=" + Httputility.urlencode) (" File name. xls ", Encoding.UTF8));
Export Excel format, because the format is different, so the file name suffix should be adjusted according to the actual situation (. xls)
Httpcontext.current.response.contenttype = "Vnd.ms-excel";
Export Word format, because the format is different, so the file name suffix should be adjusted according to the actual situation (. doc)
Httpcontext.current.response.contenttype = "Vnd.ms-word";
Export HTML format, because the format is different, so the file name suffix should be adjusted according to the actual situation (. html)
Httpcontext.current.response.contenttype = "text/html";

There are two kinds of writing seems to be able to directly output the image, did not have time to textual research, but it should not be like the above changes in the format can be, should be the first to create graphics objects can be set Response.ContentType to output
Who has a simple way of wishing to post, study, thank you
Httpcontext.current.response.contenttype = "Image/gif";
Httpcontext.current.response.contenttype = "Image/jpeg";

Explain what the next divid is, here should be the control you want to turn out, can be the server control can also HTML control (to add runat= "server" otherwise this is not found in the control)
My page is a div id= "divid" runat= "Server" with a GridView for displaying data
Divid.page.enableviewstate = false;
System.IO.StringWriter tw = new System.IO.StringWriter ();
HtmlTextWriter HW = new HtmlTextWriter (TW);
Divid.rendercontrol (HW);
The bottom three lines are the output of the data.
Response.Write ("Httpcontext.current.response.write (Tw.tostring ());
Response.Write ("</body>

Response.Flush ();
Response.close ();
}
This method needs to be rewritten or it will be an error
public override void Verifyrenderinginserverform (Control 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.