|
|
 |
OfficeWriter Home > officewriter-306.aspx |
| How
POI Stacks Up Against OfficeWriter |
 |
| |
To
Create The Same Spreadsheet, POI Requires Almost 100 Lines of Code
Compared to OfficeWriter's 25
Lines |
| |
| In just under 100 lines of actual
code (comments & spacing not included), you can create a plain Excel
spreadsheet with POI. OfficeWriter requires only 25
lines to create the same spreadsheet. |
| |
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
/**
* Creates a simple POI file that we insert data into.
*/
public class POITest {
public static void main(String[] args) throws Exception {
long startTime, stopTime;
startTime = System.currentTimeMillis();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("POIOut.xls");
// Create 3 sheets
HSSFSheet sheet1 = wb.createSheet("Sheet 1");
HSSFSheet sheet2 = wb.createSheet("Sheet 2");
HSSFSheet sheet3 = wb.createSheet("Sheet 3");
// Create a header style
HSSFCellStyle styleHeader = wb.createCellStyle();
HSSFFont fontHeader = wb.createFont();
fontHeader.setBoldweight((short) 2);
fontHeader.setFontHeightInPoints((short) 14);
fontHeader.setFontName("Arial");
styleHeader.setFont(fontHeader);
// Create a style used for the first column
HSSFCellStyle style0 = wb.createCellStyle();
HSSFFont font0 = wb.createFont();
font0.setColor(HSSFColor.RED.index);
style0.setFont(font0);
// Create the style used for dates.
HSSFCellStyle styleDates = wb.createCellStyle();
styleDates
.setDataFormat(
HSSFDataFormat.getBuiltinFormat(
"m/d/yy h:mm"));
// create the headers
HSSFRow rowHeader = sheet1.createRow(1);
// String value
HSSFCell cell0 = rowHeader.createCell((short) 0);
cell0.setCellStyle(styleHeader);
cell0.setCellValue("Name");
// numbers
for (int i = 0; i < 8; i++) {
HSSFCell cell = rowHeader.createCell((short) (i + 1));
cell.setCellStyle(styleHeader);
cell.setCellValue("Data " + (i + 1));
}
// Date
HSSFCell cell10 = rowHeader.createCell((short) 9);
cell10.setCellValue("Date");
cell10.setCellStyle(styleHeader);
for (int i = 0; i < 100; i++) {
// create a new row
HSSFRow row = sheet1.createRow(i + 2);
for (int j = 0; j < 10; j++) {
// create each cell
HSSFCell cell = row.createCell((short) j);
// Fill the first column with strings
if (j == 0) {
cell.setCellValue("Product " + i);
cell.setCellStyle(style0);
}
// Fill the next 8 columns with numbers.
else if (j < 9) {
cell.setCellValue((int) (Math.random() * 100));
}
// Fill the last column with dates.
else {
cell.setCellValue(new Date());
cell.setCellStyle(styleDates);
}
}
}
// Summary row
HSSFRow rowSummary = sheet1.createRow(102);
HSSFCellStyle sumStyle = wb.createCellStyle();
HSSFFont sumFont = wb.createFont();
sumFont.setBoldweight((short) 5);
sumFont.setFontHeightInPoints((short) 12);
sumStyle.setFont(sumFont);
sumStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
sumStyle.setFillForegroundColor(HSSFColor.GREEN.index);
HSSFCell cellSum0 = rowSummary.createCell((short) 0);
cellSum0.setCellValue("TOTALS:");
cellSum0.setCellStyle(sumStyle);
// numbers
// B
HSSFCell cellB = rowSummary.createCell((short) 1);
cellB.setCellStyle(sumStyle);
cellB.setCellFormula("SUM(B3:B102)");
// C
HSSFCell cellC = rowSummary.createCell((short) 2);
cellC.setCellStyle(sumStyle);
cellC.setCellFormula("SUM(B3:B102)");
// D
HSSFCell cellD = rowSummary.createCell((short) 3);
cellD.setCellStyle(sumStyle);
cellD.setCellFormula("SUM(B3:B102)");
// E
HSSFCell cellE = rowSummary.createCell((short) 4);
cellE.setCellStyle(sumStyle);
cellE.setCellFormula("SUM(B3:B102)");
// F
HSSFCell cellF = rowSummary.createCell((short) 5);
cellF.setCellStyle(sumStyle);
cellF.setCellFormula("SUM(B3:B102)");
// G
HSSFCell cellG = rowSummary.createCell((short) 6);
cellG.setCellStyle(sumStyle);
cellG.setCellFormula("SUM(B3:B102)");
// H
HSSFCell cellH = rowSummary.createCell((short) 7);
cellH.setCellStyle(sumStyle);
cellH.setCellFormula("SUM(B3:B102)");
// I
HSSFCell cellI = rowSummary.createCell((short) 8);
cellI.setCellStyle(sumStyle);
cellI.setCellFormula("SUM(B3:B102)");
// Write the output to a file
wb.write(fileOut);
fileOut.close();
stopTime = System.currentTimeMillis();
System.out.println("POI generation took "
+(stopTime-startTime)+"ms");
}
}
|
|
|
|
|
|