【发布时间】:2014-02-04 21:12:08
【问题描述】:
我正在尝试使用 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> 部分。
我想知道这是否是生成电子表格的好方法。有谁知道这个任务的教程:
- 做所有这些压缩的东西
- ods 文件中简单 content.xml 的结构
关于“1. 做所有这些 zip 的东西”:
我在这里所做的是生成 content.xml 并将其添加到 .ods 结构的 root
Configurations2 // Folder
META-INF // Folder
Thumbnails // Folder
meta.xml
mimetype
settings.xml
styles.xml
通过做
$zip->addFromString('content.xml',$content_xml);
但是我怎样才能将文件添加到结构的根目录而不是(比如说)Configurations2/images/Bitmamps?
【问题讨论】: