Ruby中是否有插件將CSV文件轉換爲Excel。我做了一點谷歌,但我發現只是將Excel文件轉換爲CSV。我知道一些我可以稍微調整一些並用來將Excel轉換爲CSV的寶石,但我需要知道以前是否有人這樣做過。如何將CSV轉換爲Excel?
回答
據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 post,write_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>
看來,如果你使用win32ole,你可以在Excel中'打開'csv文件並將其保存爲xls。我不確定代碼會是什麼。 – pguardiario 2012-04-12 07:22:27
好點。我希望只是讓示例看起來與上面的示例相似,但將CSV直接打開到Excel是一個更聰明的想法。 – 2012-04-12 07:40:47
還有一個寶石,我發現writeexcel很容易做這件工作..再次感謝。 – 2012-04-12 12:03:10
如果沒有找到轉換CSV到EXCEL任何寶石,那麼你可以嘗試找到兩種寶石分別
- 讀/寫CSV(用於讀取CSV文件),例如FasterCSV
- 讀/寫EXCEL(對於寫EXCEL文件)例如SpreadSheet
請注意,FasterCSV現在作爲標準庫中的「require」csv「'構建到Ruby 1.9中。 – Phrogz 2012-04-12 12:44:43
- 1. openpyxl將CSV轉換爲EXCEL
- 2. 將CSV轉換爲Excel表
- 3. Python將Excel轉換爲CSV
- 4. 將csv轉換爲excel PHP
- 5. 將csv/excel轉換爲xml的工具
- 6. 使用Python將CSV轉換爲Excel
- 7. 無法將CSV轉換爲Excel(XLSX)
- 8. 將CSV轉換爲Excel Windows Phone 8
- 9. 如何使用PHP將Excel XLS轉換爲CSV
- 10. 如何使用Perl的Spreadsheet :: WriteExcel將Excel文件轉換爲CSV?
- 11. 如何將excel文件轉換爲.csv文件?
- 12. 如何將大型XML數據轉換爲Excel或CSV
- 13. 如何使用OpenXML SDK將Excel轉換爲CSV?
- 14. 如何將Excel XML文件讀取/轉換爲R/CSV?
- 15. CSV至Excel轉換
- 16. c#如何將csv轉換爲xml
- 17. 如何將這個XML轉換爲CSV
- 18. 如何將HSSFWorkbook轉換爲CSV文件..?
- 19. 如何將CSV轉換爲JSON?
- 20. 如何將csv轉換爲xls in informatica
- 21. C#如何將CSV轉換爲數組?
- 22. 如何將JSON轉換爲CSV?
- 23. 如何將json轉換爲csv
- 24. 如何將JSon數組轉換爲csv?
- 25. 如何將csv轉換爲數組
- 26. 如何將CSV轉換爲圖形
- 27. 如何將csv數據轉換爲netsuite?
- 28. 如何將.rpt文件轉換爲csv
- 29. 如何將JSON轉換爲CSV?
- 30. 如何將JSON轉換爲CSV?
通常我們只是讓Excel中導入CSV。通常它只是起作用。 – 2012-04-12 06:24:21
另一個幾乎原生的選擇:XML導入也可以生成「漂亮」的Excel文檔。我將Rails的#to_xml輸出提供給XSLT來執行此操作。 – 2012-04-12 07:18:01
可能的重複問題:http://stackoverflow.com/questions/6646430/whats-the-easiest-way-to-export-a-csv-to-excel-with-ruby – 2012-04-12 12:20:02