How to generate an Excel file with Rails?
Bit late to the game, but there you go.
You should use the axlsx gem
On Github:
https://github.com/randym/axlsx
On Rubygems:
https://rubygems.org/gems/axlsx
On Rubytookbox:
https://www.ruby-toolbox.com/projects/axlsx
From the README
p = Axlsx::Package.new
p.workbook do |wb|
wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
img = File.expand_path('../image1.jpeg', __FILE__)
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
image.width=720
image.height=666
image.hyperlink.tooltip = "Labeled Link"
image.start_at 2, 2
end
end
end
Generating Excel documents with Ruby
How about opening a spreadsheet (Spreadsheet::Excel) with the formulae already populated. Entering data into the referenced cells will cause them to display the result.
Create Excel file in Ruby on Rails with Axlsx
Looks like you're using an example from the Axlsx example page. Drop the if statement. And use code similar to this: https://github.com/straydogstudio/axlsx_rails/blob/master/README.md#template
Basically you need to use the xlsx_package variable in your template, obtain the workbook, then get a sheet.
If you're not using axlsx_rails, add it to your gem file according to the same readme above.
Ruby Gem to write to excel?
Have you tried The Ruby Spreadsheet?
#!/usr/bin/env ruby
require 'spreadsheet'
# Begin Test
print "Spreadsheet Test\n"
# Create the rows to be inserted
row_1 = ['A1', 'B1']
row_2 = ['A2', 'B2']
# Create a new Workbook
new_book = Spreadsheet::Workbook.new
# Create the worksheet
new_book.create_worksheet :name => 'Sheet Name'
# Add row_1
new_book.worksheet(0).insert_row(0, row_1)
# Write the file
new_book.write('test.xls')
rails http response to Donwload excel file
In your config/initializers/mime_types.rb register the xlsx mime_type(It is not available in Rails by default) :
Mime::Type.register "application/xlsx", :xlsx
Assuming your code that does the excel generation works and is in a controller method(private) named excel_file
(I think its better to extract to a service/lib class):
def excel_file
csv_str = CSV.generate do |csv|
csv << ["awesome", "csv"]
end
IO.popen("secure-spreadsheet --password secret", "r+") do |io|
io.write(csv_str)
io.close_write
io.read
end
end
In your controller action you should be able to do something like this
def download_excel
respond_to do |format|
format.xlsx { send_data excel_file, type: 'application/xlsx; header=present', disposition: "attachment", filename: "output.xlsx" }
end
end
( ActionController#send_data "sends the given binary data to the browser". Read more via that link)
If you have a view, you can have a download link
<%= link_to "Download", your_download_path(format: "xlsx") %>
Users should be able to download the excel file via the link
export excel 2003(xls) file in ruby with date column(cell formatting = date)
I had this same problem. Axlsx_rails helped me. It has various examples.
To format a cell to a Date cell, you have to do the next,
wb = xlsx_package.workbook
wb.styles do |s|
date = s.add_style(:format_code => "dd/mm/yyyy")
end
And then you pass the style you want to the cell, i.e:
wb = xlsx_package.workbook
wb.styles do |s|
date = s.add_style(:format_code => "dd/mm/yyyy")
wb.add_worksheet(name: "Example") do |sheet|
sheet.add_row['Date Column']
sheet.add_row[Date.today], :style => [date]
end
end
As for the format, you could change the name on save, like so:
respond_to do |format|
format.html
format.xlsx do
response.headers['Content-Disposition'] = 'attachment; filename="excel_sheet.xls"'
end
end
Related Topics
Ruby Minitest Assert_Output Syntax
How Would I Go About Converting This Time String to Epoch Time in Ruby
Authorizing Namespaced and Nested Controllers Using Cancan
Bundler Could Not Find Compatible Versions for Gem "Bundler": in Gemfile:
Encrypting/Decrypting 3Des in Ruby
Dynamically Create a Class Inherited from Activerecord
Rails Is Not Using My Global Ruby Version
Https Request Using Net::Http's Block Form -- Is It Possible
Group Users by Age Range in Ruby
Why #!/Usr/Bin/Env Ruby Doesn't Work in Crontab
Autoload Paths and Nested Services Classes Crash in Ruby
Callback for Active Storage File Upload
Why Won't Ruby Allow Me to Specify Self as a Receiver Inside a Private Method
Net-Ssh and Remote Environment
Find Keep Duplicates in Ruby Hashes
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
Can You 'Require' Ruby File in Irb Session, Automatically, on Every Command