Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export #58

Closed
ghost opened this issue Nov 6, 2013 · 44 comments
Closed

Export #58

ghost opened this issue Nov 6, 2013 · 44 comments

Comments

@ghost
Copy link

ghost commented Nov 6, 2013

Hi there. I'm sorry if I'm writing this at the wrong place, but I was wondering if there's already something implemented to export the pivot table as an Excel file or anything like that. It doesn't have to be fancy or anything. Just the raw data used to form the current pivot table.

Thanks in advance!

@nicolaskruchten
Copy link
Owner

Hi there! Unfortunately there is no built-in feature for this at the moment... Sorry!

@ghost
Copy link
Author

ghost commented Nov 8, 2013

Hey Nicolas!

Do you mind if I pull a fork to try and implement? Do you have any pointers for me to start? I'll try to work on something over the weekend.

@nicolaskruchten
Copy link
Owner

No problem! I don't really have any pointers... It'll be easier for me to merge your changes in if you do it in CoffeeScript but if you prefer JavaScript I can port them by hand as well.

@tgabrielle
Copy link

would also be interested in this...thanks guys!

@kwesley
Copy link

kwesley commented Nov 21, 2013

+1

@nicolaskruchten
Copy link
Owner

Are people wanting to export the input data or the resulting pivot table?

@tgabrielle
Copy link

Should be the pivot table result.

@ogamot
Copy link

ogamot commented Nov 23, 2013

People always want to export :)

Le samedi 23 novembre 2013, Nicolas Kruchten a écrit :

Are people wanting to export the input data or the resulting pivot table?


Reply to this email directly or view it on GitHubhttps://github.com//issues/58#issuecomment-29124967
.

Cordialement,

Olivier Gamot

06 60 09 53 09 / 09 72 36 97 03
ogYs
www.ogYs.fr http://www.ogys.fr
www.busYtimesheet.com http://www.busytimesheet.com
56 Rue de Londres, 75008 Paris, Plan d'accès http://goo.gl/maps/zwsh

@fastcatch
Copy link
Contributor

Guys, this is a holy grail: "everybody" wants to export to Excel from JS and there seems to be no universally applicable solution. IMHO this feature should not be built into this (or any) pivottable component: it generates a proper HTML table that can be exported (as Excel, PDF, etc) via other plugins (if there's any :)).

@nicolaskruchten
Copy link
Owner

+1 to @fastcatch

I should add that the simplest way to "export" to Excel is simple copy-paste. If you select the whole table and copy it, you can then paste it into Excel and it keeps almost all of the formatting intact.

@cihadturhan
Copy link
Contributor

tl;dr A live demo
Hi,
Seems like everyone is willing to see an excel export implementation :) I'm sure there are people know how to export to xls better than me. Anyway, I'll propose the steps below.

  • Build an XML tree.
    Why? - Because if you create an xml, it's easy to convert it xml xls. Xls file is a base64 encoded xml file actually.
    How? - Every browser is a xml parser already because they parse HTML, a kind of XML. Here is the snippet to create a simple excel-like XML file using jquery:
//create root of xml
$root = $('<xml/>');

//create a workbook
$workbook = $('<ss:Workbook/>').attr({
    "xmlns:ss":"urn:schemas-microsoft-com:office:spreadsheet", 
    "xmlns:x" :"urn:schemas-microsoft-com:office:excel",
    "xmlns:o" :"urn:schemas-microsoft-com:office:office"
});

/*
* Add other elements here
*/

//add workbook to root
$root.append($workbook);

//retrieve full xml string
alert($root.wrap($('<div>')).parent().html())
  • Now convert it base64 format and append required headers
    Why? - As I said above, xls file is a base64 encoded xml string.
    How? -Simple: Using web toolkit's small script will suffice. Just one line:
xlsObject = 'data:application/vnd.ms-excel;base64,' + Base64.encode(testXML);
  • Just say document to download it.
    Why? - To download.
    How? - like this:
document.location = xlsObject

Prettified xml file and result

Edit: typo corrected

@romulosilvacardoso
Copy link

Very Simple to resolve this:

  1. Edit de pivot.js

result = $("<table class='table table-bordered pvtTable'>");

to

result = $("<table id='testTable' class='table table-bordered pvtTable'>");

  1. Create a script in local.html
<script type="text/javascript">
        var tableToExcel = (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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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]; }) }
          return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            window.location.href = uri + base64(format(template, ctx))
          }
        })()
    </script>
  1. Create a button in local.html

<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">

Done!

@ken-muturi
Copy link

works like a charm

@cihadturhan
Copy link
Contributor

Note that this is not a cross-browser solution. Most probably it won't work on ie :(

@etwoss
Copy link

etwoss commented May 14, 2014

is this true? does it not work in ie?

@ken-muturi
Copy link

@etwoss
Copy link

etwoss commented May 14, 2014

Hi

Thanks. I will take a look at it

Eric

Eric ten Westenend
Senior Developer
[Description: SCREEN_INFOmatch.jpg]http://www.screeninfomatch.com/

Screen INFOmatch
Vughterweg 47f
5211 CK 's-Hertogenbosch
The Netherlands

Tel
Fax
Http
Email

+31 73 6122 940
+31 73 6142 595
www.screeninfomatch.comhttp://www.screeninfomatch.com/
e.ten.westenend@screeninfomatch.commailto:e.ten.westenend@screeninfomatch.com

From: muturiken [mailto:notifications@github.com]
Sent: woensdag 14 mei 2014 9:42
To: nicolaskruchten/pivottable
Cc: Westenend, Eric ten
Subject: Re: [pivottable] Export (#58)

I have see this, not tried it though..
https://sites.google.com/site/firefoxaddondev/home/export-html-table-to-excel-in-firefox-and-ie


Reply to this email directly or view it on GitHubhttps://github.com//issues/58#issuecomment-43051191.

@cihadturhan
Copy link
Contributor

Try jsfiddle example I provided above and see if it works on ie10+

@etwoss
Copy link

etwoss commented May 14, 2014

Problem is that i do not directly use HTML and JavaScript. I use Delphi with CGDevtools. They take care of the created HTML/Javascript

@etwoss
Copy link

etwoss commented May 16, 2014

Hi

using the example of romulosilvacardoso i het an error in InnerHTML

Uncaught type error, cannot read poperty ínnerHTML of null

Eric

@dipeshchauhan
Copy link

I have used romulosilvacardoso method and its working in Firefox, however doesn't work in IE. Does anyone know how to resolve this? When i click on the button nothing happens. One other issue i have with Firefox is when the exel file opens the file name is a temporary file name followed by two extentions of .xls.xls therefore the browser asks "The file you are trying to open 'gGGl9L_M.xls.xls', is in a different format then specified by the file extension.

@ashesnz
Copy link

ashesnz commented Oct 8, 2014

Hello there, I've attempted to build a export method into the PivotUI, have a look and let me know what you think..

https://github.com/ashesnz/pivottable/tree/ExportToCSV

@gani4ibt
Copy link

gani4ibt commented Dec 4, 2014

+1

@nicolaskruchten
Copy link
Owner

Issue was auto-closed by Github, which was a bit premature, but f636484 does implement, finally, reliable export functionality! You can see it in action here: http://nicolas.kruchten.com/pivottable/examples/mps_export.html

@brianalu
Copy link

I'm currently trying to export using PHP to be 1 worksheet in a much larger report. Was wondering if this was ever completely implemented? :)

-Brian

@nicolaskruchten
Copy link
Owner

This is a client-side only library, so the backend technology (i.e. PHP) does not really come into play... Does the export_renderers functionality meet your needs? If not, what more would you need?

@brianalu
Copy link

Is there any way to get the output of the table pivot into a string or something that I could easily pass to PHP or a file? Similar to the export method in the branch @ashesnz made
https://github.com/ashesnz/pivottable/tree/ExportToCSV

@nicolaskruchten
Copy link
Owner

Yes, with the export_renderers... have you tried to see if this can meet your needs?

@LukeBrumfield
Copy link

I've been using this library on a reporting project, and I have something that might help out anyone wanting to export to PDF. This example uses jspdf, and should warn about "pvtAxisLabel" causing some problems if you do not exclude them
This uses jspdf: https://github.com/MrRio/jsPDF

<script src="javascripts/pivot.js"></script>
<script src="javascripts/jsPDF/dist/jspdf.debug.js"></script>
<script src="javascripts/jsPDF/dist/jspdf.min.js">

var genPDF = function() {
  var pdf = new jsPDF('p', 'pt', 'letter');
  source = $(".pvtTable")[0].outerHTML; //pvtTable is the default element class
  pdf.fromHTML(source);
  pdf.save('Test.pdf');
};

@nicolaskruchten
Copy link
Owner

Thanks @LukeBrumfield !

@GauravP77
Copy link

@LukeBrumfield @nicolaskruchten thanks for the above snippet for export to pdf. It worked but had challenges with css, grouping and was shrinking so made following changes which worked like charm.

var pdf = new jsPDF('landscape');

var specialElementHandlers = {
'.pvtTable': function(element, renderer){
return true;
}
};
var options = {
pagesplit: true
};

pdf.addHTML($('.pvtTable')[0], 15, 30, {
'elementHandlers': specialElementHandlers,
}, function() {
pdf.save('Test.pdf');
});

@meghbrume
Copy link

Hi, I am also struggling with the same problem. But I only want to download the output generated after selecting all the renderers and aggregators. How can I solve it?

Thanks in advance :)

@nicolaskruchten
Copy link
Owner

I'm marking this as closed, given that the FAQ addresses it quite well: https://github.com/nicolaskruchten/pivottable/wiki/Frequently-Asked-Questions#exporting-renderer-output-to-excel-or-images

@Bill-VA
Copy link

Bill-VA commented Nov 16, 2017

romulosilvacardoso, thanks for the code snippets. Instead of editing the pivottable.js file (using CDN), I just did this: $('.pvtTable').attr('id', 'testTable');

To remove the totals columns and bottom row, I did this:

$('#testTable thead tr:first').find('th:last').remove();
$('#testTable tr').find('td:last').remove();
$('#testTable').find('tr:last').remove();

@ghost
Copy link
Author

ghost commented Jan 1, 2018

Sir,

How can i do excel export of particular cell data in this

@susandotmark
Copy link

tried romulosilvacardoso solution via typescript, hits the function all codes are executed, no erros but nothing happens, can someone please guide me through this

@PRINCEHR
Copy link

PRINCEHR commented Mar 18, 2019

Guys, You can try this code

// In body

Export Table Data To Excel File

<script type="text/javascript">
  function exportTableToExcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

filename = filename?filename+'.xls':'excel_data.xls';
downloadLink = document.createElement("a");

document.body.appendChild(downloadLink);

if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
  type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{

downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

downloadLink.download = filename;

downloadLink.click();
}
}
  </script>

//In pivot.js

pivot.js link in which can be used in which modification is already made
modified pivot js
After Inline No:955
result.setAttribute("id", "testTable");

Hope this will help you guys!

@f0rmig4
Copy link

f0rmig4 commented Apr 12, 2019

I would like to get the data output values from the Pivot component, which I believe is in json format. Has anyone done this?

@eusebiomarquesbenitez
Copy link

This export to Excel work fine.

Adding the Javascript function:

function TableToExcel(filename = ''){
        var downloadLink;
        var dataType = 'application/vnd.ms-excel';
        var tableHTML = $(".pvtTable")[0].outerHTML.replace(/ /g, '%20');
        
        filename = filename?filename+'.xls':'excel_data.xls';
        downloadLink = document.createElement("a");
        document.body.appendChild(downloadLink);
        
        if(navigator.msSaveOrOpenBlob){
            var blob = new Blob(['\ufeff', tableHTML], {
                type: dataType
            });
            navigator.msSaveOrOpenBlob( blob, filename);
        }else{
            downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
            downloadLink.download = filename;
            downloadLink.click();
        }
    }

And the Html Code

<input type="button" value="Exportar a Excel" onclick="TableToExcel('TablaDinamica');" id="Exportar" style="font-size: 14px;" />

@susandotmark
Copy link

susandotmark commented Dec 19, 2019 via email

@ichavchavadze
Copy link

ichavchavadze commented Dec 24, 2019

Hi!
Export to excel works, but my MS Office show me warning that it's not xls file. And it's true, there is html inside!

report.zip

@susandotmark
Copy link

susandotmark commented Dec 24, 2019 via email

@Suganya0207
Copy link

Suganya0207 commented Mar 19, 2020

Dear eusebiomarquesbenitez,
Export to Excel works. But i need to change the style in the excel. i.e Header portion should have different color and total portion should be different and rows/columns should be left aligned.

or somone guide me how to edit excel style? using eusebiomarquesbenitez code

@susandotmark
Copy link

susandotmark commented Mar 19, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests