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:
- do all that zip stuff
- 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
?