Table2CSV
Export HTML tables to CSV files with ease. A lightweight Rails gem with a view helper and vanilla JavaScript.
Requirements
- Ruby >= 3.0
- Rails >= 6.0
Features
- Vanilla ES6+ JavaScript with no jQuery dependency
table_export_taghelper for export buttons- Customizable button styling
- Cross-browser support on modern browsers
Installation
Add this line to your application's Gemfile:
gem 'table2csv'
And then execute:
bundle install
Or install it yourself:
gem install table2csv
Asset Pipeline Setup
For Rails 6.x / 7.x with Sprockets:
In your app/assets/javascripts/application.js:
//= require table2csv
Optionally precompile in config/initializers/assets.rb:
Rails.application.config.assets.precompile += %w[table2csv.js]
For Rails 7+ with Import Maps:
Add to your config/importmap.rb:
pin "table2csv", to: "table2csv.js"
Then import in your application.js:
import "table2csv"
For Rails with Webpacker/Shakapacker:
The gem works with the asset pipeline. If using Webpacker, you may need to configure it to load from the gem's assets directory.
Quick Start
Basic Usage
<table id="my-table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john@example.com</td>
</tr>
</tbody>
</table>
<%= table_export_tag %>
With Table Selection
<%= table_export_tag("my-table") %>
With Styling Options
<%= table_export_tag("data-table", {
export_link_text: "Download CSV",
width: "150px",
height: "40px",
color: "#ffffff",
bgcolor: "#007bff"
}) %>
Available Options
| Option | Type | Default | Description |
|---|---|---|---|
export_link_text |
String | "Export" | Text displayed on the button |
width |
String | — | CSS width value |
height |
String | — | CSS height value |
color |
String | — | Text color |
bgcolor |
String | — | Background color |
Upgrading from 0.x to 1.0
Version 1.0.0 is the first modern release on RubyGems. It introduces breaking changes to support current Ruby and Rails versions:
- Ruby: Minimum version increased to 3.0
- Rails: Minimum version increased to 6.0
- JavaScript: jQuery is no longer required
See MIGRATION.md for detailed upgrade instructions.
Documentation
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ethirajsrinivasan/table2csv. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.