asp.net+Ligerui에서 grid로 Excel과 Word를 내보내는 방법

9149 단어
본고의 실례는 asp를 서술하였다.net+Ligerui는 grid에서 Excel과 Word를 내보내는 방법을 실현합니다.다음과 같이 여러분에게 참고할 수 있도록 공유합니다.
다음에 채택한 가이드 EXCEL 방법은 페이지를 넘기지 않는grid에 적합하고 데이터베이스를 다시 읽을 필요가 없다. 페이지를 넘기는grid에 대해 말하자면 모든 것을 가이드해야 한다. 물론 백그라운드에서 데이터베이스를 다시 읽어야 한다. 이런 가이드 EXCEL 방법은baidu가 무더기로 쌓여 있다. 여기는 중복되지 않는다.
코드 섹션:
grid.htm:




  
  <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 src="../lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"/>
  <script src="../lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"/>
  <script src="../lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"/>
  <script src="AllProductData.js" type="text/javascript"/>
  <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>


  <div id="toptoolbar"/>
  <div id="maingrid" style="margin:0; padding:0"/>
  <div style="display:none;"/>



</code></pre> 
 </div> 
 <p> print.aspx</p> 
 <div class="jb51code"> 
  <pre><code>



  <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 src="../lib/ligerUI1.1.0/js/ligerui.min.js" type="text/javascript"/>
  <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>


  </code></pre><form id="form1" runat="server">
   ...
  <div style="visibility:hidden">
  <button id="btnxls" runat="server" text=" Excel" onclick="Button1_Click" onclientclick="gethtml('#maingrid')"/>
  <button id="btndoc" runat="server" text=" Word" onclick="Button2_Click" onclientclick="gethtml('#maingrid')"/>
  </div>
  <hiddenfield id="hf" runat="server"/>
  </form>



 
 </div> 
 <p>print.aspx.cs</p> 
 <div class="jb51code"> 
  <pre><code>
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();
    }
  }
}

</code></pre> 
 </div> 
 <p> : , print.aspx , grid html hf hidden , response html</p> 
 <p> asp.net :《asp.net 》、《asp.net ajax 》 《asp.net 》。</p> 
 <p> asp.net 。</p> 
 <div class="clearfix"> 
  <span id="art_bot" class="jbTestPos"/> 
 </div> 
</div>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기