2012-04-12 38 views
8

Có plugin nào trong Ruby chuyển đổi tệp CSV sang Excel không. Tôi đã làm rất ít Google nhưng tất cả những gì tôi thấy là chuyển đổi tệp Excel thành CSV. Tôi biết vài đá quý mà tôi có thể tinh chỉnh một chút và sử dụng để chuyển đổi Excel sang CSV nhưng tôi cần phải biết nếu có ai đã làm điều đó trước đây.Làm thế nào để chuyển đổi CSV sang Excel?

+3

Thông thường, chúng tôi chỉ cho phép Excel nhập CSV. Thông thường nó chỉ hoạt động. –

+0

Một giải pháp thay thế gần như tự nhiên khác: Nhập XML có thể tạo ra các tài liệu Excel "đẹp". Tôi cung cấp dữ liệu đầu ra từ #to_xml của Rails vào XSLT để làm điều này. –

+0

Câu hỏi có thể trùng lặp: http://stackoverflow.com/questions/6646430/whats-the-easiest-way-to-export-a-csv-to-excel-with-ruby –

Trả lời

10

Theo this post, đá quý spreadsheet là một khả năng. Có vẻ như đây là một viên ngọc rất phổ biến. Kiểm tra nó ra. Ví dụ:

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) 

Theo this post, write_xlsx là một khả năng.

Tôi đã sử dụng Apache POI library với JRuby để xuất tệp xls. Đây là một ví dụ nhanh.

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 

Một số phương pháp hữu ích để định dạng bảng tính POI là

  • sheet.createFreezePane(0,1,0,1)
  • wb.setRepeatingRowsAndColumns(0, -1, -1, 0, 1)
  • sheet.setColumnWidth(i, 100 *256)
  • sheet.autoSizeColumn(i), nhưng hãy cẩn thận, nếu bạn đang chạy trong chế độ không đầu, bạn phải gọi java.lang.System.setProperty("java.awt.headless", "true")

Bạn cũng có thể sử dụng Win32ole trên Windows, nếu bạn có Excel cài đặt

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 

Một số phương pháp hữu ích để định dạng với Excel là

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

Tuy nhiên, một tùy chọn khác là viết trực tiếp vào XML Spreadsheet của Microsoft định dạng, như Ryan Bates tại Railscasts.com hiện 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

Nó có vẻ như nếu bạn đang sử dụng win32ole anyway bạn chỉ có thể 'mở' các tập tin csv trong excel và lưu nó như xls. Tôi không chắc chắn những gì mã sẽ được mặc dù. – pguardiario

+0

Điểm tốt. Tôi hy vọng chỉ làm cho ví dụ trông giống như ví dụ trên, nhưng việc mở CSV trực tiếp vào Excel là một ý tưởng thông minh hơn. –

+0

Có thêm một gem tôi tìm thấy writeexcel đã làm công việc rất dễ dàng .. Cảm ơn một lần nữa. –

2

Nếu bạn không tìm thấy bất kỳ viên ngọc cho chuyển đổi CSV để EXCEL sau đó bạn có thể thử để tìm thấy hai viên ngọc riêng

  1. Read/Write CSV (Để đọc tập tin CSV) ví dụ FasterCSV
  2. Đọc/ghi EXCEL (Để viết tệp EXCEL), ví dụ: SpreadSheet
+1

Lưu ý rằng FasterCSV được tích hợp vào Ruby 1.9 ngay bây giờ dưới dạng 'require 'csv" 'trong thư viện chuẩn. – Phrogz

Các vấn đề liên quan