Asp.net+ligerui implement grid export Excel and Word method _ practical Tips

Source: Internet
Author: User

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.

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.