xml 如何从其内部子组件正确组装有效的 xlsx 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11082278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly assemble a valid xlsx file from its internal sub-components?
提问by nick130586
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.
我正在尝试在 iOS 上以编程方式创建一个 xlsx 文件。由于 xlsx 文件的内部数据基本上存储在单独的 xml 文件中,我尝试重新创建 xlsx 结构及其所有文件和子目录,将它们压缩为 zip 文件并将其扩展名设置为 xlsx。我使用 GDataXML 解析器/编写器来创建所有必要的 xml 文件。但是,我得到的文件无法作为 xlsx 文件打开。即使我从有效的 xlsx 文件中提取所有数据,通过从原始 xml 文件中复制数据手动创建所有 xml 文件并手动压缩它们,我也无法重新创建有效的 xlsx 文件。
The questions are:
问题是:
- is xlsx really just an archive containing xml files?
- how do I create a valid xlsx file programmatically if I can't just compress xml files into zip file and set its extension to xlsx?
- xlsx 真的只是一个包含 xml 文件的存档吗?
- 如果我不能将 xml 文件压缩为 zip 文件并将其扩展名设置为 xlsx,我该如何以编程方式创建有效的 xlsx 文件?
回答by jmcnamara
In answer to your questions:
回答您的问题:
- XLSX is just a collection of XML files in a zip container. There is no other magic.
- If you decompress/unzip a valid XLSX files and then recompress/zip it and you can't read the resulting output then the problem is with the zipping software or the files your rezipped. Try a different library/utility or check the default compression type and levels that it uses and try match it to whatever Excel uses. Or check the zip file to make sure the directory structure was maintained.
- XLSX 只是一个 zip 容器中的 XML 文件的集合。没有其他魔法。
- 如果您解压缩/解压缩有效的 XLSX 文件,然后重新压缩/压缩它,但无法读取结果输出,则问题出在压缩软件或您重新压缩的文件上。尝试不同的库/实用程序或检查它使用的默认压缩类型和级别,并尝试将其与 Excel 使用的任何内容相匹配。或者检查 zip 文件以确保目录结构得到维护。
Example of the contents of an xlsx file:
xlsx 文件内容示例:
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
...
I regularly unzip XLSX files, make minor changes for testing and re-zip them without any issue.
我经常解压缩 XLSX 文件,为测试做一些小改动,然后重新压缩它们,没有任何问题。
Update: The important thing is to avoid zipping the parent directory. Here is an example using the zipsystem utility on Linux or the OS X:
更新:重要的是避免压缩父目录。以下是zip在 Linux 或 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
回答by David
If I decompress an xlsx file into a folder and then I recompress it again, the xlsx becomes corrupt / not recognized. In my case, the cause is that my zip tool is using the folder name as the first level for the relative path of each file inside the zip.
如果我将 xlsx 文件解压缩到一个文件夹中,然后再次重新压缩它,则 xlsx 会损坏/无法识别。就我而言,原因是我的 zip 工具使用文件夹名称作为 zip 中每个文件的相对路径的第一级。
I have solved the problem by creating an empty zip file INSIDE the folder with the xlsx contents and then adding all the files and folders to it.
我通过在包含 xlsx 内容的文件夹中创建一个空的 zip 文件,然后将所有文件和文件夹添加到其中来解决了这个问题。
Actually, if you try to zip the folder itself, the file is not a valid xlsx. You should rather go inside the folder, select all the contents and then right-click & zip.
实际上,如果您尝试压缩文件夹本身,则该文件不是有效的 xlsx。您应该进入文件夹,选择所有内容,然后右键单击并压缩。
回答by Crozz
I was using WinZip 15.5 to rezip xlsx xml files. Different compression types produced different results.
我使用 WinZip 15.5 来重新压缩 xlsx xml 文件。不同的压缩类型产生不同的结果。
Note:The original file size was 555KB.
注意:原始文件大小为 555KB。
- .Zip: New filesize 3,279KB (!). Excel can open.
- .Zipx: New filesize 341KB. Excel couldn'topen.
- Zip SuperFast: New filesize 606KB. Excel can open.
- Zip Enh. Deflate: New filesize 429KB. Excel couldn'topen.
- Zipx bzip2: New filesize 333KB. Excel couldn'topen.
- Zipx LZMA: New filesize 328KB. Excel couldn'topen.
- Zipx PPMd: New filesize 317KB. Excel couldn'topen.
- .Zip:新文件大小为 3,279KB (!)。Excel 可以打开。
- .Zipx:新文件大小为 341KB。Excel无法打开。
- Zip SuperFast:新文件大小为 606KB。Excel 可以打开。
- 邮编恩。放气:新文件大小为 429KB。Excel无法打开。
- Zipx bzip2:新文件大小为 333KB。Excel无法打开。
- Zipx LZMA:新文件大小为 328KB。Excel无法打开。
- Zipx PPMd:新文件大小为 317KB。Excel无法打开。
Conclusion: Zip SuperFastis the only effective compression format.
结论:Zip SuperFast是唯一有效的压缩格式。
回答by Jo?o
I was having issues and found that was zipping on the wrong folder level. You need to navigate into folder created when you unziped the xlsx and zip the actual files, not the container folder. Dummy me, shared my story, maybe it can help others save time...
我遇到了问题,发现在错误的文件夹级别上压缩。您需要导航到解压缩 xlsx 并压缩实际文件时创建的文件夹,而不是容器文件夹。假我,分享我的故事,也许可以帮助别人节省时间......

