This tutorial will help you integrate the CloudXLS API in a Ruby/Ruby on Rails application.

The first step is adding the cloudXLS gem to your application’s Gemfile and run bundle install.

gem 'cloudxls-ruby'
Configuration
Setup the cloudXLS API key which you can find on the cloudXLS dashboard.

CloudXLS.api_key = "YOUR-CLOUDXLS-API-KEY"
Generate and download xls file
Now we’re creating an Excel file with some sample CSV data. The cloudXLS API accepts data as plain text, multi-part file uploads or from URLs. For small data plain text is recommended.

CloudXLS.api_key = "YOUR-CLOUDXLS-API-KEY"

response = CloudXLS.inline({
data: {text: "Greeting,Receiver\nhello,world\nhello,excel"},
sheet: {name: "Greetings"},
doc: {filename: "hello_excel.xls"}
})

File.open("out.xls", "wb") { |f| f.write response }
Use cloudXLS async mode
Instead of downloading the Excel file to your server first and serving it from there, you can also choose to redirect the user to download the file directly from cloudXLS server, the URL is time-limited.

CloudXLS.api_key = "YOUR-CLOUDXLS-API-KEY"

response = CloudXLS.async({
data: {file: File.new("/path/to/data.csv")},
sheet: {name: "Greetings"},
doc: {filename: "hello_excel.xls"}
})

puts response.url
# in a Rails action:
# redirect_to response.url

Merge data into Templates
To copy data into an Excel template send a xls or xlsx file with the doc[template] parameter.

CloudXLS.async({
data: {text: "Greeting,Receiver\nhello,world"},
sheet: {name: "Greetings"},
doc: {template: File.new("/path/to/template.xls")}
})

More Examples
Check the examples folder in the github.com/cloudxls/cloudxls-ruby repository.

Congratulations
You've setup Excel exports for your Ruby app.

Next steps:
If you haven't already Sign up now

Copyright © 2021 cloudxls.com | All rights reserved