主頁 > 知識庫 > asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法

asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法

熱門標(biāo)簽:塔城代理外呼系統(tǒng) 遂寧市地圖標(biāo)注app 400電話辦理哪家性價比高 天心智能電銷機器人 地圖標(biāo)注專業(yè)團隊 濮陽外呼電銷系統(tǒng)怎么樣 地圖標(biāo)注的公司有哪些 地圖定位圖標(biāo)標(biāo)注 代理接電話機器人如何取消

本文實例講述了asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法。分享給大家供大家參考,具體如下:

下面采用的導(dǎo)EXCEL方法,適合不翻頁的grid,而且無需再讀一次數(shù)據(jù)庫,對于翻頁的grid來說,要導(dǎo)全部,當(dāng)然后臺要再讀一次數(shù)據(jù)庫,這種導(dǎo)EXCEL方法baidu一大堆,這里不重復(fù)

代碼部分:

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: '導(dǎo)出Excel',id:'excel',icon:'print',click:itemclick},
            {text: '導(dǎo)出Word' ,id:'word',icon:'print',click:itemclick}
          ]
      });
      $("#maingrid").ligerGrid({
        columns: [
          { display: '主鍵', name: 'ProductID', type: 'int', totalSummary:{type: 'count'}},
          { display: '產(chǎn)品名', 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: '倉庫數(shù)量', 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 "總倉庫數(shù)量:"+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 style="padding:0px; overflow:hidden; height:100% ">
  div id="toptoolbar">/div>
  div id="maingrid" style="margin:0; padding:0">/div>
  div style="display:none;">/div>
/body>
/html>

導(dǎo)出頁面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()+     //表身,具體數(shù)據(jù)
            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 style="padding:20px" onload="init()">
  form id="form1" runat="server">
  導(dǎo)出中...
  div style="visibility:hidden">
  asp:Button ID="btnxls" runat="server" Text="導(dǎo)出Excel" onclick="Button1_Click" OnClientClick="gethtml('#maingrid')"/>
  asp:Button ID="btndoc" runat="server" Text="導(dǎo)出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();
    }
  }
}

原理:在點導(dǎo)出按鈕的時候,彈一個print.aspx頁面,這個頁面把grid的html傳給自己一個叫hf的hidden里面,然后后臺response輸出這個html

更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。

希望本文所述對大家asp.net程序設(shè)計有所幫助。

您可能感興趣的文章:
  • asp.net 按指定模板導(dǎo)出word,pdf實例代碼
  • asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔
  • asp.net實現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
  • ASP.NET MVC 導(dǎo)出Word報表
  • ASP.NET導(dǎo)出word實例

標(biāo)簽:汕頭 婁底 吉林 宜春 重慶 本溪 麗江 河南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法》,本文關(guān)鍵詞  asp.net+Ligerui,實現(xiàn),grid,導(dǎo)出,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章