【问题标题】:Roo spreadsheet uploading OLE2 signature is invalid上传 OLE2 签名的 Roo 电子表格无效
【发布时间】:2013-08-01 09:25:52
【问题描述】:

好的,所以我检查了 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 

电子表格标签服务

 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  

查看

<%= form_tag spreadsheetupload_url, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 spreadsheet


    【解决方案1】:

    而不是.xls 格式使用.xlsx 格式。然后它会工作。

    对我来说它正在工作。

    【讨论】:

      【解决方案2】:

      这篇文章很有用:

      http://atomicules.co.uk/2009/07/17/roo-and-ole2-signature-is-invalid.html

      全文:

      如果您收到“Ole::Storage::FormatError: OLE2 签名无效” 使用 Roo 读取 Excel 电子表格时出错,可能是 通过重新保存电子表格解决(不幸的是使用 Excel)和 确保将其保存为“Microsoft Office Excel 工作簿 (*.xls)”等,而不是奇怪的东西。

      我有两个扩展名为 .xls 的电子表格,但似乎它们 正在伪装;当对他们进行另存为时,实际上是在 “文本(制表符分隔)”格式和其他在“网页”HTML 格式。这些是系统生成的文件,所以我想这可以解释 他们奇怪的形式。

      【讨论】:

      • 在 Stack Overflow 上不鼓励仅链接的答案,因为链接可能会失效。
      • Link 建议将文件重新保存为 XLS,当然,这可能是解决方法。为我工作。在 Save As... 提示符下,Excel 提示我的文件实际上是 XML。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多