3

I currently have an excel file (.htm) that I use as a template for my invoice reports. Once a user chooses an invoice and hit submit the template is parsed with invoice information and a prompt allows the user to save the file to their computer as an excel file (.xls).

I am now in need of allowing the user to select multiple invoices. Each selected invoice will need to parse to a different sheet.

What I've done so far: I've edited the template and created sheet2 from the data copied in sheet1, but I receive an error.

Is there a way to select which worksheet I would like information to parse? Any advice on perhaps a simpler way to do this?

This prepares my output file

//Prepare the output file
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
PlatiNUM
  • 99
  • 1
  • 9

1 Answers1

1

You can't automagically turn HTML markup into an Excel file simply by sending the appropriate headers to the browser. What you're doing is still sending an HTML file to the user, and MS Excel will be forgiving and open it (but it's MS Excel that's parsing/converting the html markup to a format that it can understand and display), not the headers that you send... note that the most recent versions of MS Excel may well issue a warning message when doing so, not the greatest user experience.

When MS Excel parses/converts an HTML file, it only ever processes the markup into a single worksheet; so unless you change the way you are working to create a real Excel file, you will not be able to create a file that will be read as two or more worksheets by MS Excel.

A list of libraries capable of creating "real" Excel files can be found in the response to this question. My own preferred choice would be to use PHPExcel (I am biased, I'm the lead developer). However, you would need to rewrite your template as an Excel template rather than HTML... the roadmap for the coming year includes being able to parse HTML to Excel, and that would allow the creation of multiple worksheets from several "pages" of html, but that option isn't available yet (probably not till about June).

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Alright, thanks. I found PHPExcel after writing this. Seems easier to use than what we are using now. – PlatiNUM Mar 15 '12 at 13:13