Export HTML tables to CSV files with ease. A lightweight Rails gem with a view helper and vanilla JavaScript.
- Ruby >= 3.0
- Rails >= 6.0
- Vanilla ES6+ JavaScript with no jQuery dependency
table_export_taghelper for export buttons- Customizable button styling
- Cross-browser support on modern browsers
Add this line to your application's Gemfile:
gem 'table2csv'And then execute:
bundle installOr install it yourself:
gem install table2csvFor Rails 6.x / 7.x with Sprockets:
In your app/assets/javascripts/application.js:
//= require table2csvOptionally 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.
<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 %><%= table_export_tag("my-table") %><%= table_export_tag("data-table", {
export_link_text: "Download CSV",
width: "150px",
height: "40px",
color: "#ffffff",
bgcolor: "#007bff"
}) %>| 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 |
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.
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.
The gem is available as open source under the terms of the MIT License.