How to output Asp.Net pages to EXCEL
This article mainly explains "how to output Asp.Net pages to EXCEL", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to output Asp.Net pages to EXCEL" bar!
In fact, it is easy to use ASP.NET to output WORD, EXCEL, TXT, HTM and other types of documents with specified content. It is mainly divided into three steps.
First, define the document type and character encoding
The copy code is as follows
Response.Clear ()
Response.Buffer= true
Response.Charset= "utf-8";
/ / the following line is very important. The attachment parameter indicates that it can be downloaded as an attachment, and you can change it to online to open it online.
/ / filename=FileFlow.xls specifies the name of the output file. Note that its extension matches the specified file type, which can be: .doc .xls .txt .htm
Response.AppendHeader ("Content-Disposition", "attachment;filename=FileFlow.xls")
Response.ContentEncoding=System.Text.Encoding.GetEncoding ("utf-8");
/ / Response.ContentType specifies that the file type can be application/ms-excel application/ms-word application/ms-txt application/ms-html or other browsers can directly support documents
Response.ContentType = "application/ms-excel"
This.EnableViewState = false;
Define an input stream
The copy code is as follows:
System.IO.StringWriter oStringWriter = new System.IO.StringWriter ()
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter (oStringWriter)
Bind the target data to the input stream output
The copy code is as follows:
This.RenderControl (oHtmlTextWriter);
/ / this means to output this page. You can also bind datagrid or other controls that support the obj.RenderControl () property.
Response.Write (oStringWriter.ToString ())
Response.End ();
Thank you for your reading, the above is the content of "how to output Asp.Net pages to EXCEL". After the study of this article, I believe you have a deeper understanding of how to output Asp.Net pages to EXCEL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!