Nov 25

In previous posts we have covered opening Excel files, and accessing sheets. Now we need to move onto reading the rows and cells in that sheet.

FileInputStream in = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(in);

int nsheets = workbook.getNumberOfSheets();
for (int i=0; i<nsheets; i++) {
	Sheet sheet = workbook.getSheetAt(i);
	for (Row row : sheet) {
		for (Cell cell : row) {
			
			// Do what you want with the cell value
		}
	}
}

written by objects \\ tags: , , , , ,

Nov 24

Once you have opened your Excel file you are probably going to want to access sheets within that file.

The Workbook class contains methods to access sheets by index or by name as shown in the following examples.

int nsheets = workbook.getNumberOfSheets();
for (int i=0; i<nsheets; i++) {
    Sheet sheet = workbook.getSheetAt(i);
				
    // Now you can access the sheet
}

And to access a sheet by name.

Sheet sheet = workbook.getSheet(sheetName);

Stay tuned as we continue to investigate using POI.

written by objects \\ tags: , , ,

Nov 23

POI supports the older OLE2 format Excel (XLS) files using HSSF, and using XSSF can also handle the newer OOXML format (XLSX) used by Microsoft Office 2007, 2008 and onwards.

If you want to open an Excel file in either format then you can use something like the following:

FileInputStream in = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(in);

written by objects \\ tags: , , , , , , ,