【问题标题】:Reading and writing Excel files using Ruby on a server without Excel installed在未安装 Excel 的服务器上使用 Ruby 读取和写入 Excel 文件
【发布时间】:2011-02-07 10:21:02
【问题描述】:

我需要在 Linux 服务器上读写(-> 转换) Excel 文件,当然它没有安装 Excel。对于 Python,存在 http://www.python-excel.org/。 Ruby有类似的东西吗?可能不需要处理最新的 Office 格式。只需要旧的 xls 文件就足够了。

【问题讨论】:

标签: ruby excel


【解决方案1】:

我同意 Gonzih 的观点,我经常使用 roo。它允许我使用模板文件进行读取、写入和写入。 该项目在他们的site 上有很好的记录。

我总是使用类似的东西:

input = Excel.new(path)
output = Array.new
input.default_sheet = input.sheets[sheet]
start.upto(input.last_row) do |row|
  output << input.row(row)
end

p output
=> a nested array representing the spreadsheat.

p output[0]
=> [row1_column_a, row1_column_b...]

阅读电子表格。请注意,如果您的文件是 .xlsx,roo gem 要求您使用 Excelx.new 而不是 Excel.new

你可以写:

book = Spreadsheet::Workbook.new
write_sheet = book.create_worksheet
row_num = 0
input.each do |row|
  write_sheet.row(row_num).replace row
  row_num +=1
end
book.write "/path/to/save/to.xls"

输入是一个数组,其结构与输出类似

【讨论】:

  • @Some_other_guy 他说“使用 roo gem”
  • -rf: 但是...我认为它不能写入excel!
  • Roo 实现了所有电子表格类型的读取权限和 Google 电子表格的读取/写入权限。它可以处理 * OpenOffice * Excel * Google 电子表格 * Excelx * LibreOffice * CSV
  • @Some_other_guy 也许它在版本中有所改变?我的示例代码在我编写它时有效,但那是三年前的事了。现在不行了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多