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

   2016-04-27 0
核心提示:这篇文章主要介绍了asp.net+Ligerui实现grid导出Excel和Word的方法,实例分析了asp.net结合jQuery的Ligerui插件操作excel和word文件的技巧,需要的朋友可以参考下

本文实例讲述了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.aspxexporttype=xls"});return;
          case "word":$.ligerDialog.open({url: "../service/print.aspxexporttype=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" on
load="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程序设计有所帮助。

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • [VB][ASP.NET]FileUpload控件「批次上传 / 多档
    FileUpload控件「批次上传 / 多档案同时上传」的范例 (VB语法) http://www.dotblogs.com.tw/mis2000lab/archive/2008/05/14/3986.aspx    FileUpload控件真的简单好用,不使用它来作批次上传,却要改用别的方法,实在不聪明。要用就一次用到底,公开File
    02-10
  • 使用WebClient自动填写并提交ASP.NET页面表单的源代码
    使用WebClient自动填写并提交ASP.NET页面表单的
    转自:http://www.cnblogs.com/anjou/archive/2007/03/07/667253.html 在.NET中通过程序填写和提交表单还是比较简单。比如,要提交一个如下图所示的登录表单:           填写和提交以上表单的代码如下:       // 要提交表单的URI字符串
    02-09
  • asp.net mvc多条件+分页查询解决方案
    


            
asp.net mvc多条件+分页查询解决方案
    asp.net mvc多条件+分页查询解决方案
    http://www.cnblogs.com/nickppa/p/3232535.html开发环境vs2010css:bootstrapjs:jquery    bootstrap paginator原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了MVC多条件+分页查询因为美工不是很好,所以用的是
    02-09
  • ASP.NET操作Cookies的问题(Bug or Not)
    以下存和取都是在不同的页面中,如果是在同一个页面也没必要用cookies了。 Test1: 给Cookies赋值: const string AAA="aaa"; Response.Cookies[AAA].Value = "111;222;333"; 取值: string value = Request.Cookies[AAA].Value; // value为111 Test2: 给Cooki
    02-09
  • Asp.Net Core 自定义验证属性
      很多时候,在模型上的验证需要自己定义一些特定于我们需求的验证属性。所以这一篇我们就来介绍一下怎么自定义验证属性。  我们来实现一个验证邮箱域名的自定义验证属性,当然,最重要的是需要定义一个继承自ValidationAttribute的类,然后在实现其IsVal
    02-09
  • Asp.Net 之 枚举类型的下拉列表绑定
    有这样一个学科枚举类型:/// 学科 /// /summary public enum Subject {None = 0,[Description("语文")]Chinese = 1,[Description("数学")]Mathematics = 2,[Description("英语")]English = 3,[Description("政治")]Politics = 4,[Description("物理&qu
    02-09
  • [ASP.NET笔记] 1.Web基础知识
         1:http协议:     2:web服务器:     3:静态网页的概念     4:动态网页的概念       http协议:http(hypertext transfer protocol) 即超文本传输协议,这个协议是在internet上进行信息传送的协议任何网页之间要相互沟通,必须要尊循
    02-09
  • ASP.NET邮件发送 .net 发送邮件
      今天做了个ASP.NET做发送邮件功能,发现QQ邮箱好奇怪,当你用QQ邮箱做服务器的时候什么邮件都发送不出去(QQ邮箱除外)。而且爆出这样的错误:"邮箱不可用。 服务器响应为: Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.ht
    02-09
  • 由ASP.NET Core根据路径下载文件异常引发的探究
    前言    最近在开发新的项目,使用的是ASP.NET Core6.0版本的框架。由于项目中存在文件下载功能,没有使用类似MinIO或OSS之类的分布式文件系统,而是下载本地文件,也就是根据本地文件路径进行下载。这其中遇到了一个问题,是关于如何提供文件路径的,通
    02-09
  • ASP.NET的运行原理与运行机制 ASP.NET的开发模式包括
    ASP.NET的运行原理与运行机制 ASP.NET的开发模
    在Asp.net4和4.5中,新增了WebPages Framework,编写页面代码使用了新的Razor语法,代码更加的简洁和符合Web标准,编写方式更接近于PHP和以前的Asp,和使用WebForms这种模仿Windows Form编程方式有了很大不同,不再有大量控件和控件生成的大量不够灵活的代码
    02-09
点击排行