Performance comparison between JXL and POI FOR excel file generations - java

Here is the code that generates .xls file using JXL:
public void generateXls(String fileName, int sheets, int cols, int rows) {
if (cols > 256) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
}
if (rows > 65536) {
throw new IllegalArgumentException("Error: number of rows per sheet must be < 65536");
}
String fullName = fileName + ".xls";
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(new File(fullName));
} catch (IOException e) {
e.printStackTrace();
}
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
WritableSheet sheet = workbook.createSheet("Sheet" + s, 0);
for (int i = 0; i < cols ; i++) {
for (int j = 0; j < rows; j++) {
Number number = new Number(i, j, random.nextDouble()*1000);
try {
sheet.addCell(number);
} catch (RowsExceededException e) {
throw new RuntimeException("Error: too many rows in a sheet");
} catch (WriteException e) {
throw new RuntimeException("Error occured while adding cell to sheet", e);
}
}
}
}
try {
workbook.write();
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the code that generates .xls and .xlsx files using POI:
public void generateXlsx(String fileName, int sheets, int cols, int rows) {
if (cols > 16383) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 16383");
}
XSSFWorkbook workbook = new XSSFWorkbook();
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
XSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < rows ; i++) {
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < cols; j++) {
XSSFCell cell = row.createCell(j);
cell.setCellValue(random.nextDouble()*1000);
}
}
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void generateXls(String fileName, int sheets, int cols, int rows) {
if (cols > 256) {
throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
}
HSSFWorkbook workbook = new HSSFWorkbook();
Random random = new Random();
for (int s = 0; s < sheets; s++ ) {
HSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < rows ; i++) {
HSSFRow row = sheet.createRow(i);
for (int j = 0; j < cols; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(random.nextDouble()*1000);
}
}
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
workbook.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I wrote a performance test:
public static void main(String[] args) {
int sheets =1;
int cols = 255;
int rows = 20000;
long a1 = System.currentTimeMillis();
ExcelFileGenerator generator = new ExcelFileGenerator();
generator.generateXls("xlsJXL.xls", sheets, cols, rows);
long xls = System.currentTimeMillis()-a1;
System.out.println("xlsJXL: " + xls);
ExcelFileGeneratorPOI generatorPOI = new ExcelFileGeneratorPOI();
long a2 = System.currentTimeMillis();
generatorPOI.generateXls("xlsPOI.xls", sheets, cols, rows);
long xlsPoi = System.currentTimeMillis()-a2;
System.out.println("xlsPOI: " + xlsPoi);
long a3 = System.currentTimeMillis();
generatorPOI.generateXlsx("xlsxPOI.xlsx", sheets, cols, rows);
long xlsx = System.currentTimeMillis()-a3;
System.out.println("xlsxPOI: " + xlsx);
}
The results are:
sheets =1
cols = 255
rows = 10
xlsJXL: 133
xlsPOI: 162
xlsxPOI: 645
sheets =1
cols = 10
rows = 255
xlsJXL: 130
xlsPOI: 140
xlsxPOI: 650
sheets =10
cols = 255
rows = 255
xlsJXL: 611
xlsPOI: 784
xlsxPOI: 16228
sheets =2
cols = 100
rows = 10000
xlsJXL: 2755
xlsPOI: 3270
xlsxPOI: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
any reason by creating .xlsx with POI is much slower than creating .xls?

xls is a binary based format, xlsx is an XML based format and requires more work to read/write.
xlsx might also require an in memory document model to parse/build the XML which could be more complex.
Finally, xls could be optimised better because it has been supported longer.

Related

How to speed up the formation of excel file?

I have such a code that generates using the POI library. The list contains 250,000 lines. The formation of an Excel file takes 30-40 minutes. Reading about POI, I realized that this is not normal. How to speed up the process?
private void create(List<User> list) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Statistic");
var rowCount = 0;
for (int i = 0; i < list.size(); i++) {
sheet.autoSizeColumn(i);
XSSFRow row = sheet.createRow(++rowCount);
XSSFCell loginCell = row.createCell(0);
loginCell.setCellValue(list.get(i).getLogin());
//...
XSSFCell amountSpecCell = row.createCell(9);
amountSpecCell.setCellValue(list.get(i).getLevel());
}
try {
FileOutputStream fileOutputStream = new FileOutputStream("myfilenew.xlsx");
workbook.write(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
workbook.close();
}
}

Exporting to excel using Apache poi within IDE works but when i create runnable jar it doesn't

I'm using Netbeans 8.2 and i create jar by clicking on clean and build icon, runnable jar works except when i want data to export to excel.
This is logic for exporting to excel:
public static void exportToExcel(TableView<T> tableView, Stage stage) {
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet("Sheet1");
HSSFRow firstRow = hssfSheet.createRow(0);
//Getting column width
ExcelPropertiesCustomization.getColumnWidth(hssfSheet);
// Getting title properties
CellStyle naslov = ExcelPropertiesCustomization.getTitleProperties(hssfWorkbook);
//set titles of columns
for (int i = 0; i < tableView.getColumns().size(); i++) {
firstRow.createCell(i).setCellValue(tableView.getColumns().get(i).getText());
firstRow.getCell(i).setCellStyle(naslov);
}
// set cells for rest of the table
for (int i = 0; i < tableView.getItems().size(); i++) {
HSSFRow hssfRow = hssfSheet.createRow(i + 1);
for (int col = 0; col < tableView.getColumns().size(); col++) {
Object celValue = tableView.getColumns().get(col).getCellObservableValue(i).getValue();
try {
if (celValue != null) {
hssfRow.createCell(col).setCellValue(Double.parseDouble(celValue.toString()));
}
} catch (NumberFormatException e) {
hssfRow.createCell(col).setCellValue(celValue.toString());
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
//save excel file and close the workbook
try {
File file = new File("FXdatabase.xls");
FileOutputStream out = new FileOutputStream(file);
hssfWorkbook.write(out);
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XLS files (*.xls)", "*.xls");
fileChooser.getExtensionFilters().add(extFilter);
File dest = fileChooser.showSaveDialog(stage);
if (dest != null) {
try {
Files.copy(file.toPath(), dest.toPath());
} catch (IOException ex) {
System.err.println(ex);
}
}
out.close();
} catch (IOException e) {
System.out.prinln(e);
}
}
It's not my first time that i export to excel using apache poi, but i never encouter this problem(i also have new lap top, don't know if that has something to do with problem), i suspect it's IOException. What can i do?

Incomplete .xls to .txt conversion

Hello I am making a program to convert from .xls to .txt. My program works almost to perfection. It inserts into text well but the thing is it does not insert the xls file completely. It does not insert the last 20 rows. The xls I need to convert contains 1548 rows, of which 1528 are inserted correctly in the format I want. It seems weird to me that only 20 rows don't get inserted. Any ideas?
public class create_text_doc {
public void writing() {
try {
File textgen = new File("C:\\Users\\darroyo\\Desktop\\expru.txt");
FileOutputStream fos = new FileOutputStream(textgen);
OutputStreamWriter osw = new OutputStreamWriter(fos);
Writer w = new BufferedWriter(osw);
String FilePath = "C:\\Users\\darroyo\\Desktop\\saldos_to_conv.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
Sheet hoja = wb.getSheet("Contratos");
int totalNoOfRows = hoja.getRows();
int totalNoOfCols = hoja.getColumns();
w.write("\n");
for (int row = 1; row < totalNoOfRows; row++) {
for (int col = 0; col < totalNoOfCols; col++) {
System.out.print(hoja.getCell(col,row).getContents() + "\t");
w.write("Deposito por Saldo Inicial|"+hoja.getCell(0,row).getContents()+"|EFECTIVO| | |0|0|0|0|0|0|0|"+hoja.getCell(1,row).getContents()+"|"+
new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime())+"|0|"+
new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime())+"|0|0|"+hoja.getCell(3,row).getContents()+"|"+
new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime())+"|0|0\n");
}
System.out.println();
}
} catch (IOException e) {
System.err.println("Problem writing txt file");
} catch (BiffException e) {
e.printStackTrace();
}
}
public static void main(String[]args) {
create_text_doc write = new create_text_doc();
write.writing();
}
}
The system.out.println in my for loop prints out all the rows correctly, the problem is when I put them in the text document. I thought it could be the problem but after removing it, I am still missing 20 rows.

How to recover formula cell cache result from excel file

I'm trying to retrieve the cached result of a formula cell in excel, the entire column is comprised of formula cells and i want to store the cached results of the columns' cells in an arraylist, but i get the error.
Apologies, i pasted the wrong code earlier, its fixed now.
public static void main(String[] args) throws Exception {
String filename = "C:/Users/L30902/Desktop/eclipse folder/FeaturesTest/student.xlsx";
FileInputStream fis = null;
int cellvalue = 0;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector<String> cellStoreVector = new Vector<String>();
while (cellIter.hasNext()) {
XSSFCell myCell = (XSSFCell) cellIter.next();
try {
cellvalue = myCell.getCachedFormulaResultType();
} catch (Exception e) {
}
cellStoreVector.addElement(Integer.toString(cellvalue));
}
String secondcolumnValue = null;
int i = 0;
secondcolumnValue = cellStoreVector.get(i).toString();
insertQuery(secondcolumnValue);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
// showExelData(sheetData);
}
private static void insertQuery(String secondcolumnvalue) {
System.out.println(secondcolumnvalue);
}
I should return values like 500, 33 but i only return 0, 0

Converting text into numeric in xls using Java

When I create excel sheet through java ,the column which has number datatype in the oracle table, get converted to text format in excel.I want it to remain in the number format.Below is my code snippet for excel creation.
FileWriter fw = new FileWriter(tempFile.getAbsoluteFile(),true);
//BufferedWriter bw = new BufferedWriter(fw);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
//Column Size of excel
for(int i=0;i<10;i++)
{
sheet.setColumnWidth((short) i, (short)8000);
}
String userSelectedValues=result;
HSSFCellStyle style = wb.createCellStyle();
///HSSFDataFormat df = wb.createDataFormat();
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//style.setDataFormat(df.getFormat("0"));
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight((short) 700);
style.setFont(font);
int selecteditems=userSelectedValues.split(",").length;
// HSSFRow rowhead = sheet.createRow((short)0);
//System.out.println("**************selecteditems************" +selecteditems);
for(int k=0; k<selecteditems;k++)
{
HSSFRow rowhead = sheet.createRow((short)k);
if(userSelectedValues.contains("O_UID"))
{
HSSFCell cell0 = rowhead.createCell((short) k);
cell0.setCellValue("O UID");
cell0.setCellStyle(style);
k=k+1;
}
///some columns here..
}
int index=1;
for (int i = 0; i<dataBeanList.size(); i++)
{
odb=(OppDataBean)dataBeanList.get(i);
HSSFRow row = sheet.createRow((short)index);
for(int j=0;j<selecteditems;j++)
{
if(userSelectedValues.contains("O_UID"))
{
HSSFCell row1=row.createCell((short)j);
row1.setCellType(row1.CELL_TYPE_NUMERIC);
row1.setCellValue(odb.getUID());
j=j+1;
}
}
index++;
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream(path.toString()+"/temp.xls");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
wb.write(fileOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this:
Cell cell = row.createCell(...);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html
In your example it will be:
Cell cell = row.createCell((short)j);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(odb.getUID());

Categories