2

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 文字列が壊れていると思います。私はそのひもで誰にも迷惑をかけたくありません - それは巨大です! 簡単なオッズのスプレッドシートから取得しました。その部分を編集しただけ<office:body>...</office:body>です。

これがスプレッドシートを生成する良い方法かどうか疑問に思っています。このタスクのチュートリアルを知っている人はいますか:

  1. すべてのzip処理を行います
  2. ods ファイル内の単純な content.xml の構造

「1.すべてのzip処理を行う」について:ここで行うことは、.ods構造のルートcontent.xmlに生成して追加することです

Configurations2 // Folder
META-INF // Folder
Thumbnails // Folder
meta.xml
mimetype
settings.xml
styles.xml

することで

$zip->addFromString('content.xml',$content_xml);

しかし、どうすればファイルを構造のルートではなく (たとえば) に追加できますConfigurations2/images/Bitmampsか?

4

1 に答える 1