Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I export tables to Excel from a webpage [closed]

How can I export tables to Excel from a webpage. I want the export to contain all the formatting and colours.

like image 651
code511788465541441 Avatar asked Apr 02 '11 15:04

code511788465541441


People also ask

How do you extract a table from a website into Excel?

Select Data > Get & Transform > From Web. Press CTRL+V to paste the URL into the text box, and then select OK. In the Navigator pane, under Display Options, select the Results table.


2 Answers

Far and away, the cleanest, easiest export from tables to Excel is Jquery DataTables Table Tools plugin. You get a grid that sorts, filters, orders, and pages your data, and with just a few extra lines of code and two small files included, you get export to Excel, PDF, CSV, to clipboard and to the printer.

This is all the code that's required:

  $(document).ready( function () {     $('#example').dataTable( {         "sDom": 'T<"clear">lfrtip',         "oTableTools": {             "sSwfPath": "/swf/copy_cvs_xls_pdf.swf"         }     } ); } ); 

So, quick to deploy, no browser limitations, no server-side language required, and most of all very EASY to understand. It's a win-win. The one thing it does have limits on, though, is strict formatting of columns.

If formatting and colors are absolute dealbreakers, the only 100% reliable, cross browser method I've found is to use a server-side language to process proper Excel files from your code. My solution of choice is PHPExcel It is the only one I've found so far that positively handles export with formatting to a MODERN version of Excel from any browser when you give it nothing but HTML. Let me clarify though, it's definitely not as easy as the first solution, and also is a bit of a resource hog. However, on the plus side it also can output direct to PDF as well. And, once you get it configured, it just works, every time.

UPDATE - September 15, 2016: TableTools has been discontinued in favor of a new plugin called "buttons" These tools perform the same functions as the old TableTools extension, but are FAR easier to install and they make use of HTML5 downloads for modern browsers, with the capability to fallback to the original Flash download for browsers that don't support the HTML5 standard. As you can see from the many comments since I posted this response in 2011, the main weakness of TableTools has been addressed. I still can't recommend DataTables enough for handling large amounts of data simply, both for the developer and the user.

like image 163
bpeterson76 Avatar answered Oct 21 '22 06:10

bpeterson76


A long time ago, I discovered that Excel would open an HTML file with a table if we send it with Excel content type. Consider the document above:

<!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>Java Friends</title> </head> <body>   <table style="font-weight: bold">     <tr style="background-color:red"><td>a</td><td>b</td></tr>     <tr><td>1</td><td>2</td></tr>   </table>     </body> </html> 

I ran the following bookmarklet on it:

javascript:window.open('data:application/vnd.ms-excel,'+document.documentElement.innerHTML); 

and in fact I got it downloadable as a Excel file. However, I did not get the expected result - the file was open in OpenOffice.org Writer. That is my problem: I do not have Excel in this machine so I cannot try it better. Also, this trick worked more or less six years ago with older browsers and an antique version of MS Office, so I really cannot say if it will work today.

Anyway, in the document above I added a button which would download the entire document as an Excel file, in theory:

<!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>Java Friends</title> </head> <body>   <table style="font-weight: bold">     <tr style="background-color:red"><td>a</td><td>b</td></tr>     <tr><td>1</td><td>2</td></tr>     <tr>       <td colspan="2">         <button onclick="window.open('data:application/vnd.ms-excel,'+document.documentElement.innerHTML);">             Get as Excel spreadsheet         </button>       </td>     </tr>   </table>     </body> </html> 

Save it in a file and click on the button. I'd love to know if it worked or not, so I ask you to comment even for saying that it did not work.

like image 29
brandizzi Avatar answered Oct 21 '22 06:10

brandizzi