// Save data in the Table control to Excel or Word Public Void Save (System.Web.ui.Control Source, DocumentType Type)
{Response.clear (); response.buffer = true;
// Set the header information of HTTP, 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 the encoded response.charset = "gb2312"; response.contentencoding = system.text.Encoding.Getencoding ("GB2312");
// Close the view status of the control Source.enableViewState = FALSE;
// Initialization 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 ();
Public enum documenttype {word, excel}
When the data is saved when the data is saved, you need to create an inconspicuous object and then resold the data and save the data. Otherwise, the button is not saved because it is not the client's control.
// The following is saving 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 the HTTP header information response.binaryWrite (Content) ; // Output response.end ();