asp.net+Ligerui实现grid导出Excel和Word的方法
来源: 阅读:974 次 日期:2016-08-09 16:14:48
温馨提示: 小编为您整理了“asp.net+Ligerui实现grid导出Excel和Word的方法”,方便广大网友查阅!

本文实例讲述了asp.net+Ligerui实现grid导出Excel和Word的方法。分享给大家供大家参考,具体如下:

下面采用的导EXCEL方法,适合不翻页的grid,而且无需再读一次数据库,对于翻页的grid来说,要导全部,当然后台要再读一次数据库,这种导EXCEL方法baidu一大堆,这里不重复

代码部分:

grid.htm:

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Baidu
map

导出页面print.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="print.aspx.cs" Inherits="example" EnableEventValidation = "false" ValidateRequest="false" %>

导出中...

Baidu
map

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程序设计有所帮助。

更多信息请查看 网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 加入群交流| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map