10

Ruby 中有没有将 CSV 文件转换为 Excel 的插件。我做了一点谷歌,但我发现的只是将 Excel 文件转换为 CSV。我知道一些我可以稍微调整并用于将 Excel 转换为 CSV 的宝石,但我需要知道是否有人以前做过。

4

4 回答 4

11

根据这篇文章电子表格宝石是一种可能性。看起来这是一个非常受欢迎的宝石。看看这个。例子:

book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet

header_format = Spreadsheet::Format.new(
  :weight => :bold,
  :horizontal_align => :center,
  :bottom => true,
  :locked => true
)

sheet1.row(0).default_format = header_format

FasterCSV.open(input_path, 'r') do |csv|
  csv.each_with_index do |row, i|
    sheet1.row(i).replace(row)
  end
end

book.write(output_path)

根据这篇文章write_xlsx是一种可能性。

我使用Apache POI 库和 JRuby 来导出 xls 文件。这是一个简单的例子。

require 'java'
require 'poi.jar'
# require 'poi-ooxml.jar'
require 'rubygems'
require 'fastercsv'

java_import org.apache.poi.hssf.usermodel.HSSFWorkbook;

wb = HSSFWorkbook.new # OR XSSFWorkbook, for xlsx
sheet = wb.create_sheet('Sheet 1')

FasterCSV.open(ARGV.first) do |csv|
  csv.each_with_index do |csv_row, line_no|
    row = sheet.createRow(line_no)
    csv_row.each_with_index do |csv_value, col_no|
      cell = row.createCell(col_no)
      cell.setCellValue(csv_value) unless csv_value.nil? # can't pass nil.
    end
  end
end


f = java.io.FileOutputStream.new("workbook.xls")
wb.write(f)
f.close

格式化 POI 电子表格的一些有用方法是

  • sheet.createFreezePane(0,1,0,1)
  • wb.setRepeatingRowsAndColumns(0, -1, -1, 0, 1)
  • sheet.setColumnWidth(i, 100 *256)
  • sheet.autoSizeColumn(i),但请注意,如果您在无头模式下运行,则必须调用java.lang.System.setProperty("java.awt.headless", "true")

如果您安装了 Excel,您也可以在 Windows 上使用 Win32ole

require 'win32ole'
require 'rubygems'
require 'fastercsv'

xl = WIN32OLE.new('Excel.Application')
xl.Visible = 0
wb = xl.Workbooks.Add
ws = wb.Worksheets(1)

FasterCSV.open(ARGV.first) do |csv|
  csv.each_with_index do |csv_row, line_no|
    csv_row.each_with_index do |value, col|
      ws.Cells(line_no + 1, col + 1).Value = value
    end
  end
end

wb.SaveAs("workbook.xls", 56) # 56 = xlExcel8 aka Excel 97-2003. i.e. xls
wb.SaveAs("workbook.xlsx", 51) # 51 = xlOpenXMLWorkbook
wb.SaveAs("workbook.xlsb", 50) # 50 = xlExcel12

wb.Close(2) #xlDoNotSaveChanges
xl.Quit

使用 Excel 格式化的一些有用方法是

  • xl.Rows(1).Font.Bold = true
  • ws.Cells.EntireColumn.AutoFit

另一种选择是直接写入 Microsoft 的XML 电子表格格式,正如 Railscasts.com 的 Ryan Bates 在他的导出 CSV 和 Excel章节的结尾处所做的那样。

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Sheet1">
    <Table>
      <Row>
        <Cell><Data ss:Type="String">ID</Data></Cell>
        <Cell><Data ss:Type="String">Name</Data></Cell>
        <Cell><Data ss:Type="String">Release Date</Data></Cell>
        <Cell><Data ss:Type="String">Price</Data></Cell>
      </Row>
    <% @products.each do |product| %>
      <Row>
        <Cell><Data ss:Type="Number"><%= product.id %></Data></Cell>
        <Cell><Data ss:Type="String"><%= product.name %></Data></Cell>
        <Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell>
        <Cell><Data ss:Type="Number"><%= product.price %></Data></Cell>
      </Row>
    <% end %>
    </Table>
  </Worksheet>
</Workbook>

这颗宝石看起来也很有前途

于 2012-04-12T06:47:08.507 回答
2

如果您没有找到任何将 CSV 转换为 EXCEL 的 gem,那么您可以尝试分别找到两个 gem

  1. 读取/写入 CSV(用于读取 CSV 文件)例如FasterCSV
  2. 读取/写入 EXCEL(用于写入 EXCEL 文件)例如SpreadSheet
于 2012-04-12T06:55:47.720 回答
2

对于那些目前看到这一点的人来说,这八年来语法发生了一些变化。根据先前的答案(为了您的复制和粘贴习惯而在此处重新复制),以下内容对我来说非常有效:

def convert_csv_to_xlsx
  book = Spreadsheet::Workbook.new
  sheet1 = book.create_worksheet

  header_format = Spreadsheet::Format.new(
    weight: :bold,
    horizontal_align: :center,
    bottom: :medium,
    locked: true
  )

  sheet1.row(0).default_format = header_format

  CSV.open(input_path, 'r') do |csv|
    csv.each_with_index do |row, i|
      sheet1.row(i).replace(row)
    end
  end

  book.write(output_path)
end

IE:FasterCSV 现在只是 CSV 并且不推荐使用 :bottom 为 true

于 2020-05-07T14:13:00.493 回答
-2

简单的方法是:

  1. 使用您喜欢的文本编辑器(如 Sublime Text)打开 CSV。记事本没问题
  2. 将所有,(逗号)替换为选项卡\t
  3. 使用扩展名另存为.xls
  4. 使用 Excel 和 TADA 打开文件!干得好!
于 2019-03-10T15:48:55.810 回答