XlsxWriter导出的文件有时无法用Numbers打开

XlsxWriter exported file sometimes can't be opened by Numbers

这是一个服务器呈现的文件,因此它被保存到一个 Django HttpResponse {"in_memory": True}

在QA测试中,有时无法用Numbers打开文件。它说

"calendar.xlsx" can't be opened right now.
The file format is invalid.

但它在 LibreOffice 中打开得很好。这里没有人使用 Windows 所以我还没有测试 Excel。我可以从文件中获取哪些信息来提供更多相关信息?

ZipFile 确实不是罪魁祸首。结果证明是使用 Django HttpResponse 实例作为 XlsxWriter Workbook 将写入的类文件对象。它期望处理文本,因此它对所有内容进行编码。如果你先写文件,然后用文件的最后字节串构造HttpResponse,就可以了。任何二进制文件都是如此。