2012-04-12 120 views
8

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

+3

通常我们只是让Excel中导入CSV。通常它只是起作用。 – 2012-04-12 06:24:21

+0

另一个几乎原生的选择:XML导入也可以生成“漂亮”的Excel文档。我将Rails的#to_xml输出提供给XSLT来执行此操作。 – 2012-04-12 07:18:01

+0

可能的重复问题:http://stackoverflow.com/questions/6646430/whats-the-easiest-way-to-export-a-csv-to-excel-with-ruby – 2012-04-12 12:20:02

回答

10

this post,该spreadsheet宝石是一种可能性。看起来这是一个非常受欢迎的宝石。一探究竟。例如:

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")

您也可以使用WIN32OLE在Windows上,如果您有Excel安装

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格式,Ryan Bates在Railscasts.com上的格式为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

看来,如果你使用win32ole,你可以在Excel中'打开'csv文件并将其保存为xls。我不确定代码会是什么。 – pguardiario 2012-04-12 07:22:27

+0

好点。我希望只是让示例看起来与上面的示例相似,但将CSV直接打开到Excel是一个更聪明的想法。 – 2012-04-12 07:40:47

+0

还有一个宝石,我发现writeexcel很容易做这件工作..再次感谢。 – 2012-04-12 12:03:10

2

如果没有找到转换CSV到EXCEL任何宝石,那么你可以尝试找到两种宝石分别

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

请注意,FasterCSV现在作为标准库中的“require”csv“'构建到Ruby 1.9中。 – Phrogz 2012-04-12 12:44:43