Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="https://stackoverflow.com/questions/7428997/whats-gem-can-operate-excel-on-linux">this post</a>, the <a href="https://rubygems.org/gems/spreadsheet" rel="nofollow noreferrer">spreadsheet</a> gem is a possibility. It looks like this is a very popular gem. Check it out. Example: </p> <pre><code>book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet header_format = Spreadsheet::Format.new( :weight =&gt; :bold, :horizontal_align =&gt; :center, :bottom =&gt; true, :locked =&gt; 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) </code></pre> <p>According to <a href="https://stackoverflow.com/questions/9886361/use-different-color-for-different-values-in-drop-down-for-excel-files-created">this post</a>, <a href="http://rubygems.org/gems/write_xlsx" rel="nofollow noreferrer">write_xlsx</a> is a possibility.</p> <p>I've used the <a href="http://poi.apache.org/spreadsheet/index.html" rel="nofollow noreferrer">Apache POI library</a> with JRuby to export xls files. Here's a quick example.</p> <pre><code>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 </code></pre> <p>Some useful methods for formatting POI spreadsheets are </p> <ul> <li><code>sheet.createFreezePane(0,1,0,1)</code> </li> <li><code>wb.setRepeatingRowsAndColumns(0, -1, -1, 0, 1)</code></li> <li><code>sheet.setColumnWidth(i, 100 *256)</code></li> <li><code>sheet.autoSizeColumn(i)</code>, but beware, if you're running in headless mode, you have to call <code>java.lang.System.setProperty("java.awt.headless", "true")</code></li> </ul> <p>You can also use Win32ole on Windows, if you have Excel installed</p> <pre><code>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 </code></pre> <p>Some useful methods for formatting with Excel are</p> <ul> <li><code>xl.Rows(1).Font.Bold = true</code></li> <li><code>ws.Cells.EntireColumn.AutoFit</code></li> </ul> <p>Yet another option is to write directly to Microsoft's <a href="http://msdn.microsoft.com/en-us/library/aa140066%28v=office.10%29" rel="nofollow noreferrer">XML Spreadsheet</a> format, as Ryan Bates at Railscasts.com does <a href="http://railscasts.com/episodes/362-exporting-csv-and-excel" rel="nofollow noreferrer">at the end of his <em>Exporting CSV and Excel</em> episode</a>.</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt; &lt;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"&gt; &lt;Worksheet ss:Name="Sheet1"&gt; &lt;Table&gt; &lt;Row&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;ID&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;Name&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;Release Date&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;Price&lt;/Data&gt;&lt;/Cell&gt; &lt;/Row&gt; &lt;% @products.each do |product| %&gt; &lt;Row&gt; &lt;Cell&gt;&lt;Data ss:Type="Number"&gt;&lt;%= product.id %&gt;&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;&lt;%= product.name %&gt;&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="String"&gt;&lt;%= product.released_on %&gt;&lt;/Data&gt;&lt;/Cell&gt; &lt;Cell&gt;&lt;Data ss:Type="Number"&gt;&lt;%= product.price %&gt;&lt;/Data&gt;&lt;/Cell&gt; &lt;/Row&gt; &lt;% end %&gt; &lt;/Table&gt; &lt;/Worksheet&gt; &lt;/Workbook&gt; </code></pre> <p><a href="https://github.com/randym/axlsx" rel="nofollow noreferrer">This gem looks promising, too</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload