0


I would like to export an HTML table to a MS Excel document (and PDF ideally) as it is displayed on the HTML/CSS page. I read a lot of pages and topics about it (on stack mainly) but everyone seemed to be talking about exporting the table, and not formatting the final excel file.

I think it should work like this:

  1. click on export button: call to jQuery
  2. jQuery creates a pure HTML document from the HTML/CSS of the table
  3. jQuery calls a PHP function/class (http://phpexcel.codeplex.com maybe) to generate the EXCEL file from the HTML code reformatted
  4. With the headers sent by PHP, the browser asks the user to save/open the excel generated file

Would this work? If so do you know any jQuery plugins and PHP classes/functions to do so? If not, what is your ideas about it?
EDIT: Thanks to Matt, I came to the idea of using an XLS file template, is it possible? It will avoid me all the formatting as it would be done in the template file. Is it possible?


Cheers,
Nicolas.

10
  • i think all it needs is to convert the html table into a csv file
    – KJYe.Name
    Dec 10, 2010 at 17:24
  • hi, if I do so I will lose the CSS formatting of the HTML document.
    – Nicolas
    Dec 10, 2010 at 17:26
  • so you wish to preserve the CSS format of the table as well?
    – KJYe.Name
    Dec 10, 2010 at 17:27
  • Yes, exactly that's why I thought of using jQuery to format it as HTML before sending it to PHP. Cheers
    – Nicolas
    Dec 10, 2010 at 17:30
  • i am thinking in the line of using ASP.NET, but i could be wrong.
    – KJYe.Name
    Dec 10, 2010 at 17:32

3 Answers 3

1

It sounds like you want the PHP file to screen scrape the jQuery generated page? This is not impossible (you can do so with the cUrl functions, take a look at this article) however if you can obtain the table data in any other way, it would be preferable. How is the table data originated? Is the table being edited by the user? Unless the user is adding content/styles, you should be able to pull the data from another source, and it will be easier.

First you should know there are primarily two methods to generate an Excel file in PHP:

  1. generate a CSV
  2. generate an XML-compliant document for Excel consumption (via PHPExcel or similar library)

Option 1 offers no styles whatsoever, just data.

Option 2 can add styles (though it's terribly obfuscated due to Excel's cumbersome format). Additionally, you have no guarantee the styles will translate to other applications (OO.org, StarOffice, Lotus, WP Office, etc.) - the XML will not be rendered consistently and your document styles will likely be lost or degraded.

If you have to resort to screen scraping, getting the css styles is going to be the most difficult part. Take a look at some of the solutions offered in this article for some ideas on how to get started.

Good luck!

1
  • Hi Matt, I do understand the CSV solution looks the simplest and most reliable but I do need to format the XLS file. Maybe I could use a XLS template then instead? -updating my question as well- Cheers,
    – Nicolas
    Dec 10, 2010 at 17:53
0

This might help with the actual Excel spreadsheet writing... http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

I've used it before, but haven't added any additional formatting to it. I imagine it's possible, somehow...

2
  • That simplistic approach could be modified to support conditional formatting, but it would take a lot of effort and research into the xls format to work out how... better to find a library that already does support it.
    – Mark Baker
    Dec 12, 2010 at 11:34
  • I saw this solution, and Matt is right, I do need a more generic solution as Matt said. Thanks for your suggestion anyway.
    – Nicolas
    Dec 13, 2010 at 9:17
0

I finally choose to redo all the formatting with PHPexcel. The drawback is that if I change something in the HTML rendered I have to do the same in the PHPexcel helper I've created and vice versa.


Thank you for your suggestion everyone.
Nicolas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.