asp. net + Ligerui Implementation of grid Deriving Excel and Word

  • 2021-07-21 08:14:42
  • OfStack

In this paper, the method of asp. net + Ligerui to derive Excel and Word from grid is described with examples. Share it for your reference, as follows:

The following guide EXCEL method is suitable for grid without turning pages, and there is no need to read the database again. For grid turning pages, it is necessary to guide all, of course, the background should read the database again. This guide EXCEL method baidu1 is a lot, so it is not repeated here

Code section:

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: ' Export Excel',id:'excel',icon:'print',click:itemclick},
            {text: ' Export Word' ,id:'word',icon:'print',click:itemclick}
          ]
      });
      $("#maingrid").ligerGrid({
        columns: [
          { display: ' Primary key ', name: 'ProductID', type: 'int', totalSummary:{type: 'count'}},
          { display: ' Product name ', name: 'ProductName', align: 'left', width: 200 },
          { display: ' Unit price ', name: 'UnitPrice', align: 'right', type:'float',totalSummary:{render: function (suminf, column, cell){return '<div> Maximum value :' + suminf.max + '</div>';},align: 'left'}},
          { display: ' Number of warehouses ', 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 " Total warehouse quantity :"+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>

Export page 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()+       // Take the watch out here 
            parent.$(".l-grid-body-inner",g).html()+     // Body, specific data 
            parent.$(".l-panel-bar-total",g).html()+"<br/>"+ // This is a global summary, 1.1.0 Version newly added 
            parent.$(".l-bar-text",g).html()         // This is a page-turning message 
            );
      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">
   Exporting ...
  <div style="visibility:hidden">
  <asp:Button ID="btnxls" runat="server" Text=" Export Excel" onclick="Button1_Click" OnClientClick="gethtml('#maingrid')"/>
  <asp:Button ID="btndoc" runat="server" Text=" Export 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();
    }
  }
}

Principle: When you click the export button, play an print. aspx page. This page transfers the html of grid to an hidden called hf, and then the background response outputs this html

Readers who are interested in asp. net can check the topics of this site: "Summary of File Operation Skills of asp", "Summary of Skills of asp. net ajax" and "Summary of Cache Operation Skills of asp. net".

I hope this paper is helpful to everyone's asp.net programming.


Related articles: