i have table like the following
now, i want to export this to excel, so i can open it in ms excel
You can use FasterCSV gem.
You can either use to_csv method.
def index
#records = ....
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #records }
format.csv { #records.to_csv }
end
end
or customize the output and use send_data method in the controller.
format.csv do
csv_string = FasterCSV.generate do |csv|
# header row
csv << ["id", "Column1", "Column1"]
# data rows
#records.each do |r|
csv << [r.id, r.column1, r.column2]
end
# send it to the browser
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=records.csv"
end
I would advice to use Spreadsheet which is mature. I'm using it with Rails 3 with no problems.
The overall process would be:
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet :name => 'Customers'
sheet.row(0).concat %w{Name Country Acknowlegement}
book.write '/path/to/output/excel-file.xls'
Ruby 1.9 has a built in CSV library with very similar API as the FasterCSV gem (actually the gem got integrated into Ruby!).
Related
I'm exporting data to a CSV file in rails and in some of my fields, I'm getting character encoding issues like this when I open in Excel:
I borrowed this code from an example and I'm assuming the encoding is off. Any idea what it should be?
Controller
#students = Student.page(params[:page])
respond_to do |format|
format.html
format.csv { send_data(#students.to_csv).encode(Encoding::SJIS),encoding: "iso-8859-1",type: 'text/csv; charset=shift_jis', filename: "学生一覧.csv" }
end
Model
def self.to_csv
attributes = %w{ID 本登録日時 郵便番号 郵便番号 電話番号}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |student|
csv << [student.id,
student.full_name,student.email] end
Remove encoding: "iso-8859-1" from your code.
Here is how I download Japanese csv in my Rails code:
def index
respond_to do |format|
format.json do
render json: paginate(Employee.includes(:skills).order(created_at: :desc, id: :desc).all)
end
format.csv do
send_data Employee.to_csv.encode(Encoding::SJIS),
type: 'text/csv; charset=shift_jis; header=present',
filename: Employee.csv_filename,
disposition: 'attachment'
end
end
end
How to convert ruby file in word file i.e (docx file). For pdf, we prawn gem. But is there any gem for word file. I am trying to convert my html file in word file so that it can be editable for user too. What should do in that case ? I was planning to convert that file in word file. Will it be possible or not.
If you are using Rails:
in initializers/mime_types.rb:
Mime::Type.register 'application/vnd.ms-word', :msword
in your controller:
say you want to export show action:
def show
#item = Item.find params[:id]
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #item }
format.msword { set_header('msword', "#{#item.title}.doc") }
format.pdf do
render :pdf => 'Coming soon...', :layout => false
end
end
end
define set_header in application_controller.rb:
def set_header(p_type, filename)
case p_type
when 'xls'
headers['Content-Type'] = "application/vnd.ms-excel; charset=UTF-8'"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
when 'msword'
headers['Content-Type'] = "application/vnd.ms-word; charset=UTF-8"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
end
end
now define a show.msword.erb #you can use any template handler like haml etc.
YOUR HTML HERE TO EXPORT TO DOC
AS LIKE NORMAL ERB TEMPLATE
Use htmltoword gem.
https://github.com/nickfrandsen/htmltoword
it hasn't been updated since November 2015, but works well.
I need to render my #manufacturers array to pdf, but do it only via click on some link in view...
Now i have such code
def index
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf { render :layout => false }
end
end
I see a lot of examples in web, but i didn't found clear and actual example... Just how simple do in a4 pdf table with my array #manufacturers ?
In addition to prawn, use the prawnto rails plugin to help with rendering the PDF as a template.
See https://github.com/prior/prawnto for the plugin and http://railscasts.com/episodes/153-pdfs-with-prawn for how to use it.
[Note: the Report gem currently only generates on letter-size paper, patch for A4 would be welcome!]
You can use the Report gem, which generates PDF using Prawn but also XLSX and CSV.
# a fake Manufacturer class - you probably have an ActiveRecord model
Manufacturer = Struct.new(:name, :gsa)
require 'report'
class ManufacturerReport < Report
table 'Manufacturers' do
head do
row 'Manufacturer report'
end
body do
rows :manufacturers
column 'Name', :name
column 'GSA?', :gsa
end
end
# you would want this so that you can pass in an array
# attr_reader :manufacturers
# def initialize(manufacturers)
# #manufacturers = manufacturers
# end
def manufacturers
[
Manufacturer.new('Ford', true),
Manufacturer.new('Fischer', false),
Manufacturer.new('Tesla', nil),
]
end
end
When you call report.pdf.path, a PDF is generating in the tmp directory:
report = ManufacturerReport.new
puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
You can do it in your controller like:
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf do
report = ManufacturerReport.new(#manufacturers) # using the commented-out code
send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
# tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
# report.cleanup
# but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
end
end
End result:
PDF
XLSX
Note that the XLSX has an autofilter added for you automatically.
I have configured a custom mime type:
ActionController::Renderers.add :csv do |csv, options|
self.content_type ||= Mime::CSV
self.response_body = csv.respond_to?(:to_csv) ? csv.to_csv : csv
end
and a respond_to block in my controller:
respond_to do |format|
format.html
format.csv { render :csv => csv_code}
end
Using Firefox and Chrome, the .csv renders to a file which is downloaded. Using Safari the .csv is rendered as a view: How can I change this and force it to download as a file?
See a screen shot of the problem:
Try
respond_to do |format|
format.html
format.csv do
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=thefile.csv'
render :csv => csv_code
end
end
if this doesn't work, try using
send_file "path/to/file.csv", :disposition => "attachment"
The way I have this working in an old Rails 2 app is using send_data instead of render in the controller. E.g.:
def csv
... # build data
send_data csv_data_as_string, :filename => "#{filename}.csv", :type => 'text/csv'
end
I want to export results generated from my rails app to excel format. distribution_sheet, results and specimens are all models. This is my script in my controller:
def first
#distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last
#results = #distribution_sheet.results
#specimens = #distribution_sheet.specimens
end
include DisplayResultHelper
def show
respond_to do |format|
format.html
format.csv {
#specimens.each do |sp|
send_data(generate_csv([["Lab No","Assay","Batch","Cuttoff"],[sp.id]]),
:filename => "my_data-#{Time.now.to_date.to_s}.csv",
:type => 'text/csv')
end
}
format.xls{
send_data(generate_xls([["Lab No","Assay","Batch","Cuttoff"],[]]),
:filename => 'my_date.xls',
:type => 'application/vnd.ms-excel')
}
end
end
end
This works if I add any words inside the arrays, but once I add sp.id it fails. I want to add the data in sp.id. Any clues?
Mark
I'm not sure which library you're using - but perhaps it requires real strings in those arrays, so try passing in sp.id.to_s instead of just sp.id