Created
May 23, 2012 09:25
-
-
Save arkadiyk/2774202 to your computer and use it in GitHub Desktop.
How do I write to an Excel file using JRuby and POI?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# translated from Java example here: http://www.avajava.com/tutorials/lessons/how-do-i-write-to-an-excel-file-using-poi.html | |
# and upgraded to POI 3.8 | |
require 'java' | |
def write_test_file | |
file_out = java.io.FileOutputStream.new('test1.xlsx') | |
workbook = org.apache.poi.xssf.usermodel.XSSFWorkbook.new | |
worksheet = workbook.create_sheet("POI Worksheet") | |
# index from 0,0... cell A1 is cell(0,0) | |
row1 = worksheet.createRow(0) | |
cell_a1 = row1.createCell(0) | |
cell_a1.cell_value = "Hello" | |
cell_style = workbook.create_cell_style | |
cell_style.fill_foreground_color = org.apache.poi.hssf.util.HSSFColor::GOLD.index | |
cell_style.fill_pattern = org.apache.poi.ss.usermodel.CellStyle::SOLID_FOREGROUND | |
cell_a1.cell_style = cell_style | |
cell_b1 = row1.createCell(1) | |
cell_b1.cell_value = "Goodbye" | |
cell_style = workbook.create_cell_style | |
cell_style.fill_foreground_color = org.apache.poi.hssf.util.HSSFColor::LIGHT_CORNFLOWER_BLUE.index | |
cell_style.fill_pattern = org.apache.poi.ss.usermodel.CellStyle::SOLID_FOREGROUND | |
cell_b1.cell_style = cell_style | |
cell_c1 = row1.createCell(2) | |
cell_c1.cell_value = true | |
cell_d1 = row1.createCell(3) | |
cell_d1.cell_value = java.util.Date.new | |
cell_style = workbook.create_cell_style | |
cell_format = workbook.create_data_format | |
cell_style.data_format = cell_format.get_format("m/d/yy h:mm") | |
cell_d1.cell_style = cell_style | |
workbook.write(file_out) | |
file_out.flush | |
file_out.close | |
end | |
write_test_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: http://www.avajava.com/tutorials/lessons/how-do-i-write-to-an-excel-file-using-poi.html | |
package test; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Date; | |
import org.apache.poi.hssf.usermodel.HSSFCell; | |
import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
import org.apache.poi.hssf.usermodel.HSSFDataFormat; | |
import org.apache.poi.hssf.usermodel.HSSFRow; | |
import org.apache.poi.hssf.usermodel.HSSFSheet; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
import org.apache.poi.hssf.util.HSSFColor; | |
public class PoiWriteExcelFile { | |
public static void main(String[] args) { | |
try { | |
FileOutputStream fileOut = new FileOutputStream("poi-test.xls"); | |
HSSFWorkbook workbook = new HSSFWorkbook(); | |
HSSFSheet worksheet = workbook.createSheet("POI Worksheet"); | |
// index from 0,0... cell A1 is cell(0,0) | |
HSSFRow row1 = worksheet.createRow((short) 0); | |
HSSFCell cellA1 = row1.createCell((short) 0); | |
cellA1.setCellValue("Hello"); | |
HSSFCellStyle cellStyle = workbook.createCellStyle(); | |
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); | |
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); | |
cellA1.setCellStyle(cellStyle); | |
HSSFCell cellB1 = row1.createCell((short) 1); | |
cellB1.setCellValue("Goodbye"); | |
cellStyle = workbook.createCellStyle(); | |
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index); | |
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); | |
cellB1.setCellStyle(cellStyle); | |
HSSFCell cellC1 = row1.createCell((short) 2); | |
cellC1.setCellValue(true); | |
HSSFCell cellD1 = row1.createCell((short) 3); | |
cellD1.setCellValue(new Date()); | |
cellStyle = workbook.createCellStyle(); | |
cellStyle.setDataFormat(HSSFDataFormat | |
.getBuiltinFormat("m/d/yy h:mm")); | |
cellD1.setCellStyle(cellStyle); | |
workbook.write(fileOut); | |
fileOut.flush(); | |
fileOut.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment