我正在尝试使用 PHP 生成 OpenDocument 电子表格。
这是我的代码:
$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);
如果我用 OpenOffice 打开生成的文件 - 一切看起来都很好。但 MS Excel 出于某种原因无法打开该文件。我认为
$content_xml
中的 XML 字符串腐败的。我不想用那个字符串打扰任何人 - 它很大!我从一个简单的 ods 电子表格中得到它。我刚刚编辑了<office:body>...</office:body>
部分。我想知道这是否是生成电子表格的好方法。有谁知道这个任务的教程:
关于“1.做所有那些 zipper 的东西”:
我在这里所做的是生成
content.xml
并将其添加到 .ods 结构的根目录Configurations2 // Folder
META-INF // Folder
Thumbnails // Folder
meta.xml
mimetype
settings.xml
styles.xml
通过做
$zip->addFromString('content.xml',$content_xml);
但是我怎样才能将文件添加到结构的根目录而不是(比如说)到
Configurations2/images/Bitmamps
?
最佳答案
您可以在 OASIS technical pages 上找到 OpenOffice (OASIS) 格式的完整详细信息。而 The Open Office 网站提供 DTDs and some tutorials
关于php - 使用 PHP 生成 OpenDocument 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21563505/