php - 使用 PHP 生成 OpenDocument 电子表格

标签 php excel zip ods

我正在尝试使用 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>部分。

我想知道这是否是生成电子表格的好方法。有谁知道这个任务的教程:
  • 做所有那些 zipper 的东西
  • ods 文件中简单 content.xml 的结构

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

    相关文章:

    php - 如何在单个代码中使用两个 mysql_query 和两个 mysql_fetch_assoc

    java - 如何在 Java 中使用 FTPClient 将文件夹压缩到 FTP 位置

    php - 创建一个 jquery/php 系统来识别一个字符串是否包含来自另一个数据库的用户的用户名

    java - 日本时代 "Reiwa"支持 Microsoft Excel 日期格式

    sql - 在 Excel 中如何从单个数据行创建多行

    javascript - VBA 抓取 JavaScript 生成的内容

    java - 使用 java.util.zip.* 将带有日语字符的文件名写入 zip 文件时变成垃圾

    node.js 中的 JSON Zip 响应

    php - 获取包含字母和非字母的字符串中最后出现的字母

    php - URL 重写 OpenCart 产品 SEO