Skip to content

Instantly share code, notes, and snippets.

@taketo1113
Last active November 9, 2021 07:03
Show Gist options
  • Save taketo1113/ab2733461fb3f48427ec17f4a450af5e to your computer and use it in GitHub Desktop.
Save taketo1113/ab2733461fb3f48427ec17f4a450af5e to your computer and use it in GitHub Desktop.
Output csv file with Shift JIS using csb gem
# app/controllers/reports_controller.rb
require 'nkf'
class ReportsController < ApplicationController
def index
@reports = Report.preload(:categories)
# for csv
@version = Time.now.strftime("%Y%m%d%H%M%S")
@filename = "reports-#{@version}.csv"
respond_to do |format|
format.csv do
csv = Csb::Builder.new(items: @reports)
csv.cols.copy!(Report.csb_cols)
csv_data = csv.build
send_data(NKF::nkf('-Ws', csv_data), filename: @filename, type: 'application/x-csv;charset=Shift_JIS')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment