Save the data output in the control to local Excel or Word, while saving the picture to local (C #)

Source: Internet
Author: User
excel|word| Control | data//SAVE data in a table control to Excel or Word
public void Save (System.Web.UI.Control source, DocumentType type)

{
Response.Clear ();
Response.buffer= true;

Set HTTP header information, encoding format
if (type = = Documenttype.excel)
{
Excel
Response.appendheader ("Content-disposition", "Attachment;filename=result.xls");
Response.ContentType = "Application/ms-excel";
}
else if (type = = Documenttype.word)
{
Word
Response.appendheader ("Content-disposition", "Attachment;filename=result.doc");
Response.ContentType = "Application/ms-word";
}

Set encoding
Response.charset= "GB2312";
Response.contentencoding=system.text.encoding.getencoding ("GB2312");

To close the view state of a control
Source. EnableViewState =false;

Initialize HTMLWriter
System.IO.StringWriter writer = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter HTMLWriter = new System.Web.UI.HtmlTextWriter (writer);
Source. RenderControl (HTMLWriter);

Output
Response.Write (writer. ToString ());

Response.End ();
}


Here is the Save picture
public void Savepic ()
{
String path = Server.MapPath (".") + @ "\images\chart.jpeg";
FileStream file = file.openread (path);
byte[] content = new Byte[file. Length];
File. Read (content,0,content. Length);
File. Close ();

Response.Clear ();
Response.appendheader ("Content-disposition", "attachment;filename=chart.jpeg");
Response.ContentType = "Image/jpeg";//Set HTTP header information
Response.BinaryWrite (content);//Output

Response.End ();
}

However, after the picture is saved, the DropDownList select event on the page can not promote, do not know what is the cause, and the button event on the page can inspire events, do not know if we have not seen this problem? Can I talk about it, or is there a problem with the process of saving my pictures?



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.