package de.kneitzel.apachepoi;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.util.Iterator;
public class XlsxTest {
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook("c:/Projects/javatests/test.xlsx");
Sheet datatypeSheet = workbook.getSheetAt(0);
// Decide which rows to process
int rowStart = datatypeSheet.getFirstRowNum();
int rowEnd = datatypeSheet.getLastRowNum();
System.out.println("Rows: " + rowStart + " - " + rowEnd);
// Handle empty cells with null
System.out.println("Using RETURN_BLANK_AND_NULL");
for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
Row r = datatypeSheet.getRow(rowNum);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}
int lastColumn = Math.max(r.getLastCellNum(), 1);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
if (c == null) {
System.out.println("Empty Cell!");
} else {
System.out.println(c.getAddress().toString() + ": " + c.getCellType().toString());
}
}
}
// Handle empty cells als BLANK cell.
System.out.println("Using CREATE_NULL_AS_BLANK");
for (int rowNum = rowStart; rowNum <= rowEnd; rowNum++) {
Row r = datatypeSheet.getRow(rowNum);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}
int lastColumn = Math.max(r.getLastCellNum(), 1);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
System.out.println(c.getAddress().toString() + ": " + c.getCellType().toString());
}
}
}
}