9

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 %>

2 Answers2

6

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

For me it's working.

Jenorish
  • 1,694
  • 14
  • 19
4

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.

Anconia
  • 3,888
  • 6
  • 36
  • 65
  • 3
    Link-only answers are discouraged on Stack Overflow, because links can rot. – Andrew Grimm Oct 16 '15 at 04:06
  • Link suggests resaving the file as XLS, for certain, could be the fix. Worked for me. At the Save As... prompt, Excel was suggesting that my file was actually XML. – David Krider Jul 05 '17 at 20:47