0

I'm trying to solve how to write PHP in order to execute a report with multiple sheets on OpenOffice spreadsheet file (AKA ods). Now I used this code for generate the OpenOffice spreadsheet report but it can display only one sheet:

<?php
// Export Calc SpreadSheet
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="Report.ods"');
?>

How I can solve this problem?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Ponomy
  • 13
  • 6
  • How are you actually building the ods file? Or are you simply generating HTML markup and assuming that PHP automagically converts it to an ods format file? – Mark Baker Mar 02 '12 at 10:25
  • I create the form for sending some parameters to PHP that automatically generate the ods file. Now this can be generate only one sheet in each time in one ods file but my point is to generate multiple sheets in one ods file in each time. How I can do this ? – Ponomy Mar 05 '12 at 04:01
  • 1
    OK! Guessing from your answer, you're assuming that the ods file is created by magic from your HTML markup. You really need to start looking at libraries that can write a "real" ods file, not simply setting the headers and trusting to Calc's generosity that it can read HTML as well as ods, and doesn't complain about your trying to trick it. I'm not aware of any ods libraries, but would an Excel library be acceptable instead? - http://stackoverflow.com/questions/3269345/how-to-generate-an-excel-document-with-multiple-worksheets-from-php/3269351#3269351 – Mark Baker Mar 05 '12 at 07:28

1 Answers1

0

There are many libraries out there that are able to be used within PHP to create, edit, and serve up spreadsheet files, or Workbooks (which are a collection of sheets...actually the spreadsheet file IS a workbook, even if it is just a single sheet). There are very well known ones, and some not-so-well known ones out there.

Most people will point to these:

there are a few more answers on questions like this one, but there is a daunting number of libraries and options.

My personal choice for a simple, small, openDocument format editor in PHP was ods-php, which is a single php file you include in your application, and instantiate. My use is not going to be creating ODS documents, but rather editing template files and serving up the edited document. You will have to write your own headers and echo the file contents in your own function in your PHP application, but that is not hard at all.

There is a [very] basic example php file included in the ods-php download that shows some of the functions, but if you can follow basic PHP logic, you can look through the library source and figure out its available functions. I'd say it would do just fine for what you need.

On the other hand, if you would rather have a bigger API at your disposal, and your server is decent enough to handle it, I'd recommend any of the other three. Keep in mind, the other three are rather large by comparison, and each has it's own strong and weak points:

PHPExcel is probably the most used by the free community, and is maintained on github constantly (last messed with 6 days ago), but is quite large. Documentation is available on the github site (I provided their old link, which links to their github).

Spreadsheet_Excel_Writer is tool from Pear, and is just as large, although it is no longer maintained, so what is there is 'as-is'. The Pear team is looking for someone to take over its maintenance, but what is there is as far as I can tell working.

openTBS is a single class library extension of the TBS engine (TinyButStrong). You have to install TBS in order to use openTBS, and it is a very good idea to enable zlib for compression capabilities of your files. If you go with openTBS, you not only get the ability to make xml based documents, but you get the functionality of the entire TBS engine at your disposal, which is quite nice if you would like to merge html source templates with your php scripts (check out the site, it might open some new frontiers for you).

there are definitely more libraries and tools out there, but these are the most notable ones I found in my search. My choice was guided by the driving force to keep my server tiny and standalone (it operates on a raspberry pi). If I were to choose a bulky, production environment API, I would probably choose PHPExcel because it has the support it needs to keep being up-to-date with M$' ever-adapting formats.

user253780
  • 104
  • 1
  • 7