Roo spreadsheet uploading OLE2 signature is invali

2019-06-20 10:31发布

问题:

Right so I've checked out Roo. Great gem and all and have a really basic application that doesn't have no models. And basic controller, class and view and I can't seem to get a spreadsheet to upload as I am getting OLE2 signature is invalid error. I have the following basic setup

Controller

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, nil, :ignore)
        when ".xlsx" then Excelx.new(file.path, nil, :ignore)
        else raise "Unknown file type: #{file.original_filename}"
      end
    end

end  

View

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

回答1:

Instead of .xls format use .xlsx format.then it will work.

For me it's working.



回答2:

This article was quite useful:

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

FullText:

If you get an "Ole::Storage::FormatError: OLE2 signature is invalid" error when reading an Excel spreadsheet using Roo it can probably be solved by resaving the spreadsheet (unfortunately using Excel) and making sure it is saving it as "Microsoft Office Excel Workbook (*.xls)", etc and not something odd.

I had two spreadsheets that had the .xls extension, but seems they were masquerading; when doing a Save As on them one was actually in a "Text (tab delimited)" format and the other in a "Web Page" HTML format. These were system generated files, so I guess that explains their odd form.