欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-19 12:16:28
本文实例讲述了asp.net+ligerui实现grid导出excel和word的方法。分享给大家供大家参考,具体如下: 下面采用的导excel方法,适合不翻页的grid...

本文实例讲述了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 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>

导出页面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 style="padding:20px" onload="init()">
  <form id="form1" runat="server">
  导出中...
  <div style="visibility:hidden">
  <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相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

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

上一篇:

下一篇: