Implement Easyui's DataGrid export as an example code for Excel _jquery

Source: Internet
Author: User

Before have introduced how to realize the Easyui in the DataGrid content printing, today to introduce how to implement the DataGrid content exported to Excel files. The following is the code implementation:

Export.js

function Changetotable (printdatagrid) {var tablestring = ' <table cellspacing= ' 0 ' class= ' PB ' > '; var frozencolumns = Printdatagrid.datagrid ("Options"). FrozenColumns;  Get FrozenColumns object var columns = Printdatagrid.datagrid ("Options"). Columns;

  Get Columns object var namelist = new Array (); Load title if (typeof columns!= ' undefined ' && columns!= ') {$ (columns). Each (function (index) {TA
      Blestring + = ' \n<tr> ';  if (typeof frozencolumns!= ' undefined ' && typeof Frozencolumns[index]!= ' undefined ') {for (var i = 0; i < Frozencolumns[index].length; ++i) {if (!frozencolumns[index][i].hidden) {tablestring = ' \n<th width= ' + Frozencolumns[inde
            X][i].width + ' "';
              if (typeof Frozencolumns[index][i].rowspan!= ' undefined ' && frozencolumns[index][i].rowspan > 1) {
            Tablestring + = ' rowspan= ' + Frozencolumns[index][i].rowspan + ' "; } if (typeof Frozencolumns[index][i].colspan!= ' undefined ' && frozencolumns[index][i].colspan > 1) {table
            String = = ' colspan= ' + Frozencolumns[index][i].colspan + ' "';
              } if (typeof Frozencolumns[index][i].field!= ' undefined ' && frozencolumns[index][i].field!= ') {
            Namelist.push (Frozencolumns[index][i]);
          tablestring + = ' > ' + frozencolumns[0][i].title + ' </th> ';
          for (var i = 0; i < columns[index].length; ++i) {if (!columns[index][i].hidden) {
          Tablestring + = ' \n<th width= ' + columns[index][i].width + ' ";  if (typeof Columns[index][i].rowspan!= ' undefined ' && columns[index][i].rowspan > 1) {tablestring
          + = ' rowspan= ' + Columns[index][i].rowspan + ' "';
            } if (typeof Columns[index][i].colspan!= ' undefined ' && columns[index][i].colspan > 1) { Tablestring + = ' colspan= ' + Columns[index][i].colspan + ' "'; } if (typeof Columns[index][i].field!= ' undefined ' && columns[index][i].field!= ') {Name
          List.push (Columns[index][i]);
        tablestring + = ' > ' + columns[index][i].title + ' </th> ';
    } tablestring + = ' \n</tr> ';
  }); }//load content var rows = Printdatagrid.datagrid ("GetRows");
    This code is to get all the lines for the current page for (var i = 0; i < rows.length ++i) {tablestring = ' \n<tr> ';

      for (var j = 0; j < namelist.length; ++j) {var e = namelist[j].field.lastindexof (' _0 ');
      Tablestring + = ' \n<td '; if (namelist[j].align!= ' undefined ' && namelist[j].align!= ') {tablestring = ' style= ' text-align: ' +
      Namelist[j].align + '; ';}
      tablestring + = ' > ';
      if (e + 2 = namelist[j].field.length) {tablestring + = rows[i][namelist[j].field.substring (0, E)]; else tablestRing + + Rows[i][namelist[j].field];
    Tablestring + = ' </td> ';
  } tablestring + = ' \n</tr> ';
  } tablestring + = ' \n</table> ';
return tablestring; function Export (strxlsname, Exportgrid) {var f = $ (' <form action=/export.aspx "method=" post "id=" FM1 "></fo
  Rm> ');
  var i = $ (' <input type= "hidden" id= "txtcontent" name= "Txtcontent"/> ");
  var L = $ (' <input type= "hidden" id= "txtname" name= "Txtname"/> ");
  I.val (Changetotable (Exportgrid));
  I.appendto (f);
  L.val (Strxlsname);
  L.appendto (f);
  F.appendto (document.body). Submit ();
Document.body.removeChild (f); }

export.aspx

 protected void Page_Load (object sender, EventArgs e) {response.clear ();
      Response.Buffer = true;
      Response.Charset = "Utf-8";
      response.contentencoding = System.Text.Encoding.UTF8; Response.appendheader ("Content-disposition", "attachment;filename=\" "+ Httputility.htmlencode" (Request["TxtName") ??
      DateTime.Now.ToString ("YyyyMMdd")) + ". Xls\" ");
      Response.ContentType = "Application/ms-excel";
      Response.Write (" 
 

where export.aspx in order to prevent the front page content interference, the front page only stay:

 
 

This sentence, the other is all deleted.

Call Method:

 
 

The above is a small series for everyone to bring the implementation of the Easyui DataGrid exported to Excel sample code all content, I hope that we support the cloud-Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.