600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php ligerui 导出excel asp.net+Ligerui实现grid导出Excel和Word的方法

php ligerui 导出excel asp.net+Ligerui实现grid导出Excel和Word的方法

时间:2019-11-23 10:25:26

相关推荐

php ligerui 导出excel asp.net+Ligerui实现grid导出Excel和Word的方法

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

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

代码部分:

grid.htm:

/p>

"/TR/xhtml1/DTD/xhtml1-transitional.dtd">

$(function () {

$("#toptoolbar").ligerToolBar({ items: [

{text: '导出Excel',id:'excel',icon:'print',click:itemclick},

{text: '导出Word' ,id:'word',icon:'print',click:itemclick}

]

});

$("#maingrid").ligerGrid({

columns: [

{ display: '主键', name: 'ProductID', type: 'int', totalSummary:{type: 'count'}},

{ display: '产品名', name: 'ProductName', align: 'left', width: 200 },

{ display: '单价', name: 'UnitPrice', align: 'right', type:'float',totalSummary:{render: function (suminf, column, cell){return '

最大值:' + suminf.max + '';},align: 'left'}},

{ display: '仓库数量', name: 'UnitsInStock', align: 'right', type: 'float',totalSummary:{type: 'sum'}}

],

dataAction: 'local',

data: AllProductData, sortName: 'ProductID',

showTitle: false, totalRender: f_totalRender,

width: '100%', height: '100%',heightDiff:-10

});

$("#pageloading").hide();

});

function f_totalRender(data, currentPageData)

{

return "总仓库数量:"+data.UnitsInStockTotal;

}

function itemclick(item)

{

grid = $("#maingrid").ligerGetGridManager();

if(item.id)

{

switch (item.id)

{

case "excel":$.ligerDialog.open({url: "../service/print.aspx?exporttype=xls"});return;

case "word":$.ligerDialog.open({url: "../service/print.aspx?exporttype=doc"});return;

}

}

}

导出页面print.aspx

function GetQueryString(name)

{

var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");

var r= window.location.search.substr(1).match(reg);

if (r!=null) return unescape(r[2]);return null;

}

function gethtml(g)

{

parent.$(".l-grid-header-table",g).attr("border","1");

parent.$(".l-grid-body-table",g).attr("border","1");

$("#hf").val(

parent.$(".l-grid-header",g).html()+ //这里把表头捞出来

parent.$(".l-grid-body-inner",g).html()+ //表身,具体数据

parent.$(".l-panel-bar-total",g).html()+"

"+ //这是全局汇总,1.1.0版本新添加的

parent.$(".l-bar-text",g).html() //这是翻页讯息

);

parent.$(".l-grid-header-table",g).attr("border","0");

parent.$(".l-grid-body-table",g).attr("border","0");

// parent.$(".l-grid-header-table",g).removeAttr("border");

// parent.$(".l-grid-body-table",g).removeAttr("border");

}

function init()

{

if (GetQueryString("exporttype")=="xls")

{

document.getElementById("btnxls").click();

}

else

{

document.getElementById("btndoc").click();

}

setTimeout(function ()

{

parent.$.ligerDialog.close();

}, 3000);

}

导出中...

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

希望本文所述对大家程序设计有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。