有没有 Ruby 的插件可以将 CSV 文件转换成 Excel 文件?我在 Google 上搜索了一下,但是所有的结果都是将 Excel 文件转换成 CSV。我知道一些 Gem 工具可以稍加修改并用于将 Excel 转换成 CSV,但我需要知道是否有人以前做过这样的转换。
有没有 Ruby 的插件可以将 CSV 文件转换成 Excel 文件?我在 Google 上搜索了一下,但是所有的结果都是将 Excel 文件转换成 CSV。我知道一些 Gem 工具可以稍加修改并用于将 Excel 转换成 CSV,但我需要知道是否有人以前做过这样的转换。
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)
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")
如果你在Windows系统上安装了Excel,你还可以使用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 Spreadsheet 格式中,正如Railscasts.com上的Ryan Bates在其Exporting CSV and 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>
require "csv"
内置于Ruby 1.9中。 - Phrogz对于当前看到这篇文章的读者,这些八年来语法有所改变。以下是基于先前答案的代码(为了方便复制粘贴已经重新复制在下面),对我而言完美运行:
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
I.E.: FasterCSV现在只是CSV,:bottom已经弃用
简单的方法是:
,
(逗号)替换为制表符\t
.xls
扩展名