EasyUI Forum
November 05, 2025, 10:43:11 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Datagrid to Excel  (Read 28274 times)
rezzonico
Full Member
***
Posts: 186


View Profile
« on: August 28, 2015, 08:29:17 AM »

Hi all,

I am trying to export a Datagrid to Excel.
I have found the following thread:
http://www.jeasyui.com/forum/index.php?topic=4192.0

The solution works very well.

I only have the following problem:
if I export a Datagrid with 10 lines, an Excel file with 19 lines is created (9 blank lines are added between the data).
Is it possible to avoid the creation of the blank lines ?

I have tryed to modify the code but without success.

Any help is appreciated.

Regards.
Miche
« Last Edit: August 28, 2015, 08:32:26 AM by rezzonico » Logged
devnull
Sr. Member
****
Posts: 431


View Profile
« Reply #1 on: August 29, 2015, 04:18:13 AM »

This is what I am using, it works fine in all my datagrids and also supports frozen columns.

Code:
$.extend($.fn.datagrid.methods, {
  toExcel: function(jq, filename){
    return jq.each(function(){
      var uri = 'data:application/vnd.ms-excel;base64,'
      , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
      , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
      , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

      var alink = $('<a style="display:none"></a>').appendTo('body');
      var view = $(this).datagrid('getPanel').find('div.datagrid-view');
      
      var table = view.find('div.datagrid-view2 table.datagrid-btable').clone();
      var tbody = table.find('>tbody');
      view.find('div.datagrid-view1 table.datagrid-btable>tbody>tr').each(function(index){
        $(this).clone().children().prependTo(tbody.children('tr:eq('+index+')'));
      });
      
      var head = view.find('div.datagrid-view2 table.datagrid-htable').clone();
      var hbody = head.find('>tbody');
      view.find('div.datagrid-view1 table.datagrid-htable>tbody>tr').each(function(index){
        $(this).clone().children().prependTo(hbody.children('tr:eq('+index+')'));
      });    
      hbody.prependTo(table);
      
      var ctx = { worksheet: name || 'Worksheet', table: table.html()||'' };
      alink[0].href = uri + base64(format(template, ctx));
      alink[0].download = filename;
      alink[0].click();
      alink.remove();
    })
  }
});

Logged

-- Licensed User --
rezzonico
Full Member
***
Posts: 186


View Profile
« Reply #2 on: August 31, 2015, 07:07:42 AM »

Hi devnull,

thanks for your answer.
The code that I use is exacly your code.
Looking in my code I found that the problem is when adding "view: datailview" in a datagrid.

When using "view: datailview" the following line is added after each line:

<tr style="display:none;"><td colspan="1" style="border-right:0"><div class="datagrid-row-detail">&nbsp;</div></td><td colspan="4"><div class="datagrid-row-detail"><div class="ddv"></div></div></td></tr>

Can someone help me in modifying the code (I think that the solution is to remove the above line) ?
I have tried but without success ...

Thanks in advance.
Miche
« Last Edit: August 31, 2015, 07:23:46 AM by rezzonico » Logged
rezzonico
Full Member
***
Posts: 186


View Profile
« Reply #3 on: September 01, 2015, 03:50:36 AM »

I have found the following solution:

table.find('[style*="display:none"]').remove();

Regards
Miche
Logged
devnull
Sr. Member
****
Posts: 431


View Profile
« Reply #4 on: September 02, 2015, 04:39:34 AM »

Hi, sorrie, but I have never used 'detailviews', I am just using standard datagrids.

Logged

-- Licensed User --
rezzonico
Full Member
***
Posts: 186


View Profile
« Reply #5 on: September 02, 2015, 05:32:26 AM »

No problem,

your script helped me a lot.

Thanks !
Miche
Logged
jmansur
Newbie
*
Posts: 31


View Profile
« Reply #6 on: September 03, 2015, 05:31:26 AM »

Hi.

I try using this solution in a datagrid - bufferview , but when I exported the records, I only exported the amount of the pageSize property records .

It's happened ?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!