2

I'm trying to generate OpenDocument Streadsheets using PHP. This is my code:

$content_xml = '<?xml version="1.0" encoding="UTF-8"?><office:document-content ...';
$file = 'spreadsheet.ods'; // this one is beeing generated. It is not existent yet...
$zipfile = 'container.zip'; // this one already exists. It's in the same directory
// this script is in. I created the container.zip by unziping a usual .ods file,
// removing the content.xml and ziping again

$handle1 = fopen($zipfile, "r");
$handle2 = fopen($file, "w");

// writing the content of the `container.zip` to the `spreadsheet.ods`
fwrite($handle2, fread($handle1, filesize($zipfile)));
fclose($handle1);
fclose($handle2);

$zip = new ZipArchive();
$zip->open($file, ZIPARCHIVE::CREATE);

// adding the xml string to the zip file
$zip->addFromString('content.xml',$content_xml);
$zip->close();

header('Content-disposition: attachment; filename="'.$file.'"');
header('Content-Type: application/vnd.oasis.opendocument.spreadsheet');
header('Content-Length: ' . filesize($file));
readfile($file);
unlink($file);

If I open the generated file with OpenOffice - everything looks fine. But MS Excel for some reason is not able to open the file. I think the XML string in $content_xml is corrupt. I don't want to bother anybody with that string - it is huge! I got it from a simple ods spreadsheet. I just edited the <office:body>...</office:body> part.

I wondering whether this is a good way to generate spreadsheets. Does anybody know a tutorial for this task:

  1. do all that zip stuff
  2. structure of a simple content.xml in an ods file

Regarding "1. do all that zip stuff": what I do here is generating the content.xml and adding it to the root of the .ods structure

Configurations2 // Folder
META-INF // Folder
Thumbnails // Folder
meta.xml
mimetype
settings.xml
styles.xml

By doing

$zip->addFromString('content.xml',$content_xml);

But how can I add a file NOT to the root of the structure but (let' say) to Configurations2/images/Bitmamps?

esviko
  • 250
  • 1
  • 3
  • 15

1 Answers1

1

You can find full details of the OpenOffice (OASIS) format on the OASIS technical pages while The Open Office website provides DTDs and some tutorials

Mark Baker
  • 209,507
  • 32
  • 346
  • 385