Export spreadsheet data in ODS/CSV format with PHP5

Filed under: SpreadsheetExport — Matthias Burtscher @ 20. February 2009 10:09 | Comments (0)

OpenDocument SpreadsheetThe second LGPL library this week is a PHP5 class to export spreadsheet data in different formats. Currently supported are CSV (Comma Separated Values) and ODS (OpenDocument Spreadsheet). While CSV exports are quite ordinary, ODS functionality is something special for your project.

Library’s objectives

We want to provide a library which allows exporting spreadsheet data in a much more comfortable and easier way than it was before. In advanced products we always need different data exports – sometimes also in different formats. While CSV export functionality isn’t challenging for most web developers, ODS export isn’t very common in web products. SpreadsheetExport allows you to export either as CSV or ODS using the same code.

Example

This example shows a simple export of two columns (string and float values).

  1. $export = new SpreadsheetExport();
  2. $export->filename = "sampleExport"; // extension will be added automatically
  3. // Add 3 columns
  4. $export->AddColumn("date", 5);
  5. $export->AddColumn("string");
  6. $export->AddColumn("float");
  7. // Now we add 3 rows
  8. $export->AddRow(array("1987-01-01", "world population reached 5 billion", 5));
  9. $export->AddRow(array("1999-01-01", "world population reached 6 billion", 6));
  10. $export->AddRow(array("2012-01-01", "world population reaches 12 billion", 12));
  11. // Start download as CSV …
  12. $export->DownloadCSV();
  13. // … or as ODS
  14. $export->DownloadODS();

Requirements

Because files in OpenDocument format are simple Zip files holding one or more XML files you need to have installed the ziplib and appropriate PHP extension on your webserver.

Licencing

SpreadsheetExport is published under the GNU Lesser General Public Licence.

Download

You can download SpreadsheetExport from Sourceforge.

Tags: , , ,

No Comments »

No comments yet.

Leave a comment