如何从其内部子组件正确组装有效的 xlsx 文件? [英] How to properly assemble a valid xlsx file from its internal sub-components?

查看:27
本文介绍了如何从其内部子组件正确组装有效的 xlsx 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 上以编程方式创建一个 xlsx 文件.由于 xlsx 文件的内部数据基本上存储在单独的 xml 文件中,我尝试重新创建 xlsx 结构及其所有文件和子目录,将它们压缩为 zip 文件并将其扩展名设置为 xlsx.我使用 GDataXML 解析器/编写器来创建所有必要的 xml 文件.但是,我得到的文件无法作为 xlsx 文件打开.即使我从有效的 xlsx 文件中提取所有数据,通过从原始 xml 文件中复制数据手动创建所有 xml 文件并手动压缩它们,我也无法重新创建有效的 xlsx 文件.

I'm trying to create an xlsx file programmatically on iOS. Since the internal data of xlsx files is basically stored in separate xml files, I tried to recreate xlsx structure with all its files and subdirectories, compress them into a zip file and set its extension to xlsx. I use GDataXML parser/writer for creating all the necessary xml files. However, the file I get can't be opened as xlsx file. Even if I rip all the data from a valid xlsx file, create all the xml files manually by copying data from the original xml files and compress them manually, I can't recreate a valid xlsx file.

问题是:

  • xlsx 真的只是一个包含 xml 文件的存档吗?
  • 如果我不能将 xml 文件压缩为 zip 文件并将其扩展名设置为 xlsx,我该如何以编程方式创建有效的 xlsx 文件?

推荐答案

回答您的问题:

  1. XLSX 只是 zip 容器中的一组 XML 文件.没有其他魔法.
  2. 如果您解压缩/解压缩有效的 XLSX 文件,然后重新压缩/压缩它,但无法读取结果输出,则问题出在压缩软件或您重新压缩的文件上.尝试不同的库/实用程序或检查它使用的默认压缩类型和级别,并尝试将其与 Excel 使用的任何内容相匹配.或者检查 zip 文件以确保目录结构得到维护.

xlsx 文件内容示例:

Example of the contents of an xlsx file:

unzip -l example.xlsx
Archive:  example.xlsx
  Length     Date   Time    Name
 --------    ----   ----    ----
      769  10-15-14 09:23   xl/worksheets/sheet1.xml
      550  10-15-14 09:22   xl/workbook.xml
      201  10-15-14 09:22   xl/sharedStrings.xml
      ...

我经常解压 XLSX 文件,为测试做一些小改动,然后重新压缩它们,没有任何问题.

I regularly unzip XLSX files, make minor changes for testing and re-zip them without any issue.

更新:重要的是避免压缩父目录.以下是在 Linux 或 OS X 上使用 zip 系统实用程序的示例:

Update: The important thing is to avoid zipping the parent directory. Here is an example using the zip system utility on Linux or the OS X:

# Unzip an xlsx file into a directory.
unzip example.xlsx -d newdir

# Make some valid changes to the files.
cd newdir/
vi xl/worksheets/sheet1.xml

# Rezip the files *FROM* the unzipped directory.
# Note: you could also re-zip to the original file if required.
find . -type f | xargs zip ../newfile.xlsx

# Check the file looks okay.
cd ..
unzip -l newfile.xlsx
xdg-open newfile.xlsx

这篇关于如何从其内部子组件正确组装有效的 xlsx 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆