8 ruby-on-rails spreadsheet ruby-on-rails-3 ruby-on-rails-3.1
是的,所以我检查了Roo.伟大的宝石和所有,并有一个真正的基本应用程序,没有没有模型.基本的控制器,类和视图,我似乎无法上传电子表格,因为我收到OLE2 signature is invalid
错误.我有以下基本设置
调节器
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
Run Code Online (Sandbox Code Playgroud)
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, nil, :ignore)
when ".xlsx" then Excelx.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
Run Code Online (Sandbox Code Playgroud)
视图
<%= form_tag spreadsheetupload_url, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这篇文章非常有用:
http://atomicules.co.uk/2009/07/17/roo-and-ole2-signature-is-invalid.html
全文:
如果您在使用 Roo 读取 Excel 电子表格时收到“Ole::Storage::FormatError: OLE2 签名无效”错误,则可能可以通过重新保存电子表格(不幸的是使用 Excel)并确保将其另存为“Microsoft Office Excel 工作簿(*.xls)”等,这并不奇怪。
我有两个扩展名为 .xls 的电子表格,但它们似乎是伪装的;当对它们进行另存为时,其中一个实际上是“文本(制表符分隔)”格式,另一个是“网页”HTML 格式。这些是系统生成的文件,所以我想这解释了它们奇怪的形式。