[英]rails - attach xls file to email
我創建了一個生成xls文件的代碼,然后我將它傳遞給Mailer以將其作為附件發送。 但是,我一次又一次地得到同樣的錯誤:
TypeError: no implicit conversion of Spreadsheet::Workbook into String
要么
NoMethodError: undefined method `length' for #<Spreadsheet::Workbook:0x007fe937e4fe80>
我的代碼是:
def xls_mailer (data)
attachments['HelloWorld.xlsx'] = data
mail(subject: "Hi", to: @gmail.email)
end
*** data - 是我傳遞給這個方法的xls文件。
謝謝你們,
好的伙計們,我找到了答案。 代碼應該是這樣的:
spreadsheet_file = StringIO.new
data.write(spreadsheet_file)
attachments['HelloWorld.xls'] = spreadsheet_file.read
下面的代碼塊就像一個魅力。 我試過這個,用在一個工作的應用程序中。
def send_excel_report(file_name, emails, subject, email_content, file_path, bcc_emails = [])
attachments["#{file_name}.xls"] = File.read(file_path)
mail(to: emails, subject: subject, from: "my_email@gmail.com", bcc: bcc_emails) do |format|
format.html { render :partial => "users/mail_body"}
end
end
僅供參考:我使用了電子表格gem來創建excel
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.