写入有效 Excel 文件

Writing valid Excel file

我有一个端点可以接收带有 .xlsx 文件作为附件的电子邮件。我想将这些文件保存在我的应用程序中,以便以后访问数据。

收到邮件及其附件后 - mime_type 为 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - 我打电话给

path = "data/emails/#{attachment.filename}"
File.write(path, attachment.body.decoded)

但是我得到这个错误:

Encoding::UndefinedConversionError: "\x85" from ASCII-8BIT to UTF-8

当我对解码体使用add .force_encoding('utf-8')时,它确实成功了,但是它写入的文件变得无效。我无法正常打开它,也无法访问它的数据。

如何编写普通的 Excel 文件?

这个有用吗?

File.open( path, "w+b", 0644 ) { |f| f.write attachment.body.decoded }

摘自此处: https://cbpowell.wordpress.com/2011/01/17/saving-attachments-with-ruby-1-9-2-rails-3-and-the-mail-gem/