如何将CSV转换为Excel?

问题描述 投票:9回答:3

Ruby中是否有任何将CSV文件转换为Excel的插件。我做了很少的谷歌,但我发现只是将Excel文件转换为CSV。我知道一些宝石,我可以稍微调整并用于将Excel转换为CSV但我需要知道是否有人之前已经这样做了。

ruby excel csv gem
3个回答
11
投票

根据this postspreadsheet宝石是一种可能性。看起来这是一个非常受欢迎的宝石。看看这个。例:

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)

根据this postwrite_xlsx是一种可能性。

我已经使用Apache POI library和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

另一种选择是直接写入微软的XML Spreadsheet格式,正如Railscasts.com的Ryan Bates做at the end of his Exporting CSV and Excel episode

<?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>

This gem looks promising, too


2
投票

如果您没有找到任何用于将CSV转换为EXCEL的宝石,那么您可以尝试单独找到两个宝石

  1. 读/写CSV(用于读取CSV文件),例如FasterCSV
  2. 读/写EXCEL(对于写EXCEL文件),例如SpreadSheet

-2
投票

简单的方法是:

  1. 使用您喜欢的文本编辑器(如Sublime Text)打开CSV。记事本没问题
  2. 将所有,(逗号)替换为选项卡\t
  3. 保存为扩展名.xls
  4. 使用Excel和TADA打开文件!干得好!
© www.soinside.com 2019 - 2024. All rights reserved.