具有多个选项卡的 Excel 文件

发布于 2024-07-19 20:48:40 字数 129 浏览 3 评论 0原文

我需要创建一个包含多个选项卡的 Excel 文件。 我需要在 Ruby on Rails 中执行此操作。 我已经检查过 Apache POI,但我不确定它是否提供该功能。 有谁知道它是否可以或者是否有其他替代方案可以做到这一点? 谢谢。

I need to create an excel file with multiple tabs. I need to do this in Ruby on Rails. I have checked out Apache POI but I'm not sure if it provides that functionality. Does anyone know if it does or if there are other alternatives that can do this?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你又不是我 2024-07-26 20:48:40

我发现,如果您使用 Apache POI,则可以在 Workbook 实例上调用 createSheet() 。

I figured it out, you can call createSheet() on an instance of Workbook if you're using Apache POI.

梦魇绽荼蘼 2024-07-26 20:48:40

我使用 ruby​​ gem“spreadsheet-excel”来实现这种功能,

#!/usr/bin/env ruby
RAILS_ENV = 'production'

require File.dirname(__FILE__) + '/../config/environment'
require "spreadsheet/excel" 

file = "name_of_your_excel_file.xls" 
workbook = Spreadsheet::Excel.new("#{RAILS_ROOT}/#{file}")

# First Sheet
worksheet = workbook.add_worksheet("Sheet No. 1")
worksheet.write(0, 0, "Timestamp")
worksheet.write(0, 1, "Type")
worksheet.write(0, 2, "Text")
# ...and whatever you want to do here
# Second Sheet
worksheet_2 = workbook.add_worksheet("Sheet No. 2")
#... and so on

这在 Ruby 和 Ruby-on-Rails 中都非常有效。

要安装电子表格/Excel,只需输入

ruby gem install "spreadsheet-excel"

希望这可以帮助您

I use the ruby gem "spreadsheet-excel" for this kind of functionality

#!/usr/bin/env ruby
RAILS_ENV = 'production'

require File.dirname(__FILE__) + '/../config/environment'
require "spreadsheet/excel" 

file = "name_of_your_excel_file.xls" 
workbook = Spreadsheet::Excel.new("#{RAILS_ROOT}/#{file}")

# First Sheet
worksheet = workbook.add_worksheet("Sheet No. 1")
worksheet.write(0, 0, "Timestamp")
worksheet.write(0, 1, "Type")
worksheet.write(0, 2, "Text")
# ...and whatever you want to do here
# Second Sheet
worksheet_2 = workbook.add_worksheet("Sheet No. 2")
#... and so on

This works great both in Ruby and Ruby-on-Rails.

To install spreadsheet/excel just type

ruby gem install "spreadsheet-excel"

Hope this can help you

浮华 2024-07-26 20:48:40

如果我理解正确的话 - 多个选项卡意味着工作簿中的多个“工作表”。 Apache POI 确实提供了此功能(查看下面的链接)。 请注意,我不是 Ruby 人(至少现在还不是),这些链接适用于 Java 中的用法,但我很确定像 YAJB 这样的桥梁将帮助您完成它:

创建新工作簿
创建新工作表

If I understand you correctly - by multiple tabs you mean multiple 'worksheets' in a workbook. Apache POI does provide this functionality (check the links below). Please note that I am not a Ruby person (atleast not yet) and these links are for the usages in Java but I am pretty sure the bridges like YAJB will help you in getting it done:

Creating a New WorkBook
Creating a New WorkSheet

青衫儰鉨ミ守葔 2024-07-26 20:48:40

使用电子表格 gem 的多选项卡 excel 工作表

要在单个 excel 工作表中创建多个工作表,最好使用电子表格 gem。 在您的控制器中(假设卡是您的控制器)添加以下内容:

def export

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => 'Sheet1'
sheet2 = book.create_worksheet :name => 'Sheet2'

 sheet1.row(0).push "some content in Column1" //to push content in 1st row 1st column of 1st spreadsheet

 //keep pushing on row(0) if you want to push contents other columns of 1st row

  spreadsheet = StringIO.new
  book.write spreadsheet
  file = "Excelsheet"
  send_data spreadsheet.string, :filename => "#{file}", :type =>  "application/vnd.ms-excel"

end

在您的视图中
<%=link_to("将数据库导出到 Excel", cards_url + "/export") %>强文本

Multiple tab excel sheet using spreadsheet gem

To create multiple sheets in a single excel sheet , its good to use spreadsheet gem. In your controller (suppose card is your controller) add the following:

def export

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => 'Sheet1'
sheet2 = book.create_worksheet :name => 'Sheet2'

 sheet1.row(0).push "some content in Column1" //to push content in 1st row 1st column of 1st spreadsheet

 //keep pushing on row(0) if you want to push contents other columns of 1st row

  spreadsheet = StringIO.new
  book.write spreadsheet
  file = "Excelsheet"
  send_data spreadsheet.string, :filename => "#{file}", :type =>  "application/vnd.ms-excel"

end

In you view
<%=link_to("Export Database to Excel", cards_url + "/export") %>strong text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文