编写有效的Excel文件

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

在收到邮件及其附件application/vnd.openxmlformats-officedocument.spreadsheetml.sheet – 我的电话是mime_type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

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

但我得到这个错误:

Encoding :: UndefinedConversionError:从ASCII-8BIT到UTF-8的“\ x85”

当我使用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/