Skip to main content

Ruby roo gem uploading excels

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 %>
 
Another Method
 
require 'spreadsheet'
require 'fileutils'
require 'iconv'

def excel_import

 tmp = params[:dump][:excel_file].tempfile

 Spreadsheet.client_encoding = 'UTF-8'

 book = Spreadsheet.open tmp.path

 sheet1 = book.worksheet 0
 sheet1.each do |row|
 TimeSheet.new(:ac_no => row[0]).save
 end
end

FileUtils.rm tmp.path 

Comments

Popular posts from this blog

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

--------------------
request.original_url
request.url
request.host_with_port
<link rel="canonical" href="<%= request.url %>" />
<%=request.path%>
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
request.env['REQUEST_URI']

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end