//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;
import java.util.*;
public class ExcelTest {
static public int NUM_ROWS = 10000;
static public int NUM_COLS = 25;
private Workbook workbook;
private Sheet cSheet;
private boolean useHSSF;
public ExcelTest(boolean useHSSF) {
this.useHSSF = useHSSF;
}
public boolean Test() {
boolean tbr = true;
try {
//Open template xlsx file. This is a single workbook with a single, empty sheet:
this.workbook = WorkbookFactory.create(new FileInputStream((this.useHSSF ? "data/test.xls" : "data/test.xlsx")));
//generate a new sheet:
this.cSheet = this.workbook.createSheet();
//write NUM_ROWS rows
for (int i = 0; i < NUM_ROWS; i++) {
Row r = this.cSheet.createRow(i);
//write NUM_COLS columns
for (int j = 0; j < NUM_COLS; j++) {
Cell c = r.getCell(j);
if (c == null) {
c = r.createCell(j);
}
c.setCellValue("TEST " + i + ":" + j);
}
}
} catch (Exception e) {
tbr = false;
e.printStackTrace(System.out);
}
return tbr;
}
public boolean Export(String filename) {
boolean tbr = true;
try {
FileOutputStream out = new FileOutputStream(filename);
this.workbook.write(out);
out.close();
} catch (Exception e) {
tbr = false;
e.printStackTrace(System.out);
}
return tbr;
}
static public void main(String[] args) {
long start;
boolean didTest;
ExcelTest et;
System.out.println("Starting test...");
start = System.currentTimeMillis();
et = new ExcelTest(true);
didTest = et.Test();
if (didTest) {
et.Export("TEST.xls");
}
System.out.println("XLS (HSSF) version took " + (System.currentTimeMillis() - start) + " ms.");
start = System.currentTimeMillis();
et = new ExcelTest(false);
didTest = et.Test();
if (didTest) {
et.Export("TEST.xlsx");
}
System.out.println("XLSX (XSSF) version took " + (System.currentTimeMillis() - start) + " ms.");
}
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE