是的,所以我检查了Roo.伟大的宝石和所有,并有一个真正的基本应用程序,没有没有模型.和基本的控制器,类和视图,我似乎无法上传电子表格,因为我得到OLE2签名是无效的错误.我有以下基本设置
调节器
class SpreadsheetServiceController < ApplicationController def new end def create parser = SpreadsheetTagService.new(params[:spreadsheet][:file]) respond_to do |format| format.all {render :json => 'Done'} end end end
SpreadsheetTagService
class SpreadsheetTagService include Roo def initialize(uploaded_file) @tmp_destination = "#{Rails.root}/tmp/tag-import.xls" @file_path = save_file_to_tmp(uploaded_file) @file = File.new(@file_path) read_file(@file) end private def save_file_to_tmp(uploaded_file) FileUtils.mv(uploaded_file.tempfile.path,@tmp_destination ) @tmp_destination end def read_file(file) @spreadsheet = open_spreadsheet(file) @spreadsheet.each_with_pagename do |name,sheet| Rails.logger.debug( sheet ) end end def open_spreadsheet(file) case File.extname(file.path) when ".csv" then Csv.new(file.path,nil,:ignore) when ".xls" then Excel.new(file.path,:ignore) when ".xlsx" then Excelx.new(file.path,:ignore) else raise "Unknown file type: #{file.original_filename}" end end end
视图
<%= form_tag spreadsheetupload_url,multipart: true do %> <%= file_field_tag :file %> <%= submit_tag "Import" %> <% end %>
解决方法
而不是.xls格式使用.xlsx格式.然后它将工作.
对我而言,这是有效的.