您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關asp.net+Ligerui如何實現grid導出Excel和Word的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體如下:
下面采用的導EXCEL方法,適合不翻頁的grid,而且無需再讀一次數據庫,對于翻頁的grid來說,要導全部,當然后臺要再讀一次數據庫,這種導EXCEL方法baidu一大堆
代碼部分:
grid.htm:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title></title> <link href="../lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" /> <link href="../lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" /> <script src="../lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="../lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script> <script src="../lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script> <script src="../lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script> <script src="AllProductData.js" type="text/javascript"></script> <script type="text/javascript"> $(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 '<div>最大值:' + suminf.max + '</div>';},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; } } } </script> </head> <body > <div id="toptoolbar"></div> <div id="maingrid" ></div> <div ></div> </body> </html>
導出頁面print.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="print.aspx.cs" Inherits="example" EnableEventValidation = "false" ValidateRequest="false" %> <html> <head> <title></title> <link href="../lib/ligerUI/skins/aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" /> <script src="../lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="../lib/ligerUI1.1.0/js/ligerui.min.js" type="text/javascript"></script> <script type="text/javascript"> 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()+"<br/>"+ //這是全局匯總,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); } </script> </head> <body onload="init()"> <form id="form1" runat="server"> 導出中... <div > <asp:Button ID="btnxls" runat="server" Text="導出Excel" onclick="Button1_Click" OnClientClick="gethtml('#maingrid')"/> <asp:Button ID="btndoc" runat="server" Text="導出Word" onclick="Button2_Click" OnClientClick="gethtml('#maingrid')"/> </div> <asp:HiddenField ID="hf" runat="server" /> </form> </body> </html>
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+Ligerui如何實現grid導出Excel和Word”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。