您现在的位置是:网站首页> 编程资料编程资料
asp.net+Ligerui实现grid导出Excel和Word的方法_实用技巧_
2023-05-24
283人已围观
简介 asp.net+Ligerui实现grid导出Excel和Word的方法_实用技巧_
本文实例讲述了asp.net+Ligerui实现grid导出Excel和Word的方法。分享给大家供大家参考,具体如下:
下面采用的导EXCEL方法,适合不翻页的grid,而且无需再读一次数据库,对于翻页的grid来说,要导全部,当然后台要再读一次数据库,这种导EXCEL方法baidu一大堆,这里不重复
代码部分:
grid.htm:
导出页面print.aspx
<%@ 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, EventArgs 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(); } } } 原理:在点导出按钮的时候,弹一个print.aspx页面,这个页面把grid的html传给自己一个叫hf的hidden里面,然后后台response输出这个html
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。
您可能感兴趣的文章:
相关内容
- ASP.NET中HyperLink超链接控件的使用方法_基础应用_
- asp.net实现拒绝频繁的IP访问的方法_实用技巧_
- ASP.NET中HiddenField隐藏域控件的使用方法_基础应用_
- ASP.NET中FileUpload文件上传控件应用实例_基础应用_
- 在ASP.NET 2.0中操作数据之二十七:创建自定义排序用户界面_自学过程_
- 在ASP.NET 2.0中操作数据之二十六:排序自定义分页数据_自学过程_
- 在ASP.NET 2.0中操作数据之二十五:大数据量时提高分页的效率_自学过程_
- 在ASP.NET 2.0中操作数据之二十四:分页和排序报表数据_自学过程_
- 在ASP.NET 2.0中操作数据之二十三:基于用户对修改数据进行限制_自学过程_
- 在ASP.NET 2.0中操作数据之二十二:为删除数据添加客户端确认_自学过程_
