使用PHP生成OpenDocument Spreadsheets

我正在尝试使用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的XMLstring已损坏。 我不想打扰任何人 – 这是巨大的! 我从一个简单的ods电子表格中得到它。 我刚刚编辑了<office:body>...</office:body>部分。

我想知道这是否是生成电子表格的好方法。 有没有人知道这个任务的教程:

  1. 做所有的压缩东西
  2. ods文件中一个简单的content.xml结构

关于“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

您可以在OASIS技术页面上findOpenOffice(OASIS)格式的详细信息,而Open Office网站提供了DTD和一些教程

Interesting Posts