The example in this article describes how Asp.net+ligerui implements grid to export Excel and Word. Share to everyone for your reference, specific as follows:
The following guide to Excel method, suitable for not paging the grid, and no need to read a database, for paging grid, to guide all, of course, the backstage to read a database, this guide Excel method Baidu A lot of, here does not repeat
Code section:
Grid.htm:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Print.aspx.cs" inherits= "Example" enableEventValidation = "false" validaterequest= "false"%>
Print.aspx.cs
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts; Namespace Service {public partial class Print:System.Web.UI.Page {protected void Page_Load (object sender, even Targs e) {if (!
IsPostBack) {}} void Exportexcel () {response.clear ();
Response.Buffer = true;
Response.Charset = "Utf-8";
Response.appendheader ("Content-disposition", "Attachment;filename=tmp.xls");
response.contentencoding = System.Text.Encoding.GetEncoding ("Utf-8");
Response.ContentType = "Application/ms-excel"; This.
EnableViewState = false;
System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter); OhtmlteXtwriter.writeline (HF.
Value);
Response.Write (Ostringwriter.tostring ());
Response.End ();
} void Exportword () {response.clear ();
Response.Buffer = true;
Response.Charset = "Utf-8";
Response.appendheader ("Content-disposition", "Attachment;filename=tmp.doc");
response.contentencoding = System.Text.Encoding.GetEncoding ("Utf-8");
Response.ContentType = "Application/ms-word"; This.
EnableViewState = false;
System.IO.StringWriter ostringwriter = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter); Ohtmltextwriter.writeline (HF.
Value);
Response.Write (Ostringwriter.tostring ());
Response.End ();
} protected void button1_click (object sender, EventArgs e) {exportexcel ();
} protected void Button2_Click (object sender, EventArgs e) {Exportword ();
}
}
}
Principle: At the point of export button, play a print.aspx page, this page to the grid of HTML to its own called HF hidden inside, and then backstage response output this HTML
More interested in asp.net related content readers can view the site topics: "asp.net file Operation skills Summary", "ASP.net ajax Skills Summary" and "asp.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.