使用PHP生成OpenDocument Spreadsheets

时间:2014-02-04 21:12:08

标签: php excel zip ods

我正在尝试使用PHP生成OpenDocument Streadsheets。 这是我的代码:

$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. 做所有拉链的东西
  2. ods文件中简单content.xml的结构
  3. 关于“1.做所有拉链的东西”: 我在这里做的是生成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

1 个答案:

答案 0 :(得分:1)

您可以在OASIS technical pages上找到OpenOffice(OASIS)格式的完整详细信息,而Open Office网站提供DTDs and some tutorials