Convert CSV to Excel in Ruby
The Comma Separated Values (CSV) format is ideal for capturing complex data sets, but it is not the easiest on the eyes. If you regularly need to convert CSV files to the more presentable and easy-to-manipulate XLSX format, we have a tool that can help with that. In this brief tutorial, we will demonstrate how you can use an API to automatically convert a CSV file to XLSX, allowing you to increase productivity by saving time on manual conversions.
First, you will add the Ruby client to your Gemfile:
gem 'cloudmersive-convert-api-client', '~> 2.1.6'
After the Ruby client is installed, you can call the conversion function with the following code:
# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
endapi_instance = CloudmersiveConvertApiClient::ConvertDocumentApi.newinput_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.begin
#Convert CSV to Excel XLSX Spreadsheet
result = api_instance.convert_document_csv_to_xlsx(input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_csv_to_xlsx: #{e}"
end
With the return of your new Excel file, your process will be complete!