List of usage examples for org.apache.poi.ss.usermodel Workbook setMissingCellPolicy
void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
From source file:net.sf.taverna.t2.activities.spreadsheet.ExcelSpreadsheetReader.java
License:Open Source License
public void read(InputStream inputStream, Range rowRange, Range columnRange, boolean ignoreBlankRows, SpreadsheetRowProcessor rowProcessor) throws SpreadsheetReadException { Workbook workbook; try {//from w w w. ja v a 2s. c o m workbook = WorkbookFactory.create(inputStream); } catch (InvalidFormatException e) { throw new SpreadsheetReadException("The file does not have a compatible spreadsheet format", e); } catch (IOException e) { throw new SpreadsheetReadException("The spreadsheet stream could not be read", e); } catch (IllegalArgumentException e) { throw new SpreadsheetReadException("The spreadsheet stream could not be read", e); } DataFormatter dataFormatter = new DataFormatter(); workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); Sheet sheet = workbook.getSheetAt(0); if (rowRange.getEnd() < 0) { rowRange.setEnd(sheet.getLastRowNum()); logger.debug("No end of row range specified, setting to " + rowRange.getEnd()); } SortedMap<Integer, String> currentDataRow = new TreeMap<Integer, String>(); for (int rowIndex = rowRange.getStart(); rowIndex <= rowRange.getEnd(); rowIndex++) { boolean blankRow = true; if (rowRange.contains(rowIndex)) { Row row = sheet.getRow(rowIndex); for (int columnIndex = columnRange.getStart(); columnIndex <= columnRange.getEnd(); columnIndex++) { if (columnRange.contains(columnIndex)) { String value = null; if (row != null) { Cell cell = row.getCell(columnIndex); if (cell != null) { value = getCellValue(cell, dataFormatter); } } if (value != null) { blankRow = false; } currentDataRow.put(columnIndex, value); if (columnIndex == columnRange.getEnd()) { if (!ignoreBlankRows || !blankRow) { rowProcessor.processRow(rowIndex, currentDataRow); } currentDataRow = new TreeMap<Integer, String>(); } } } } } }
From source file:org.deidentifier.arx.io.ImportAdapterExcel.java
License:Open Source License
/** * Creates a new instance of this object with given configuration * /*from w ww. j av a 2s.c o m*/ * Depending upon the file type it either uses HSSF or XSSF to access the * file. In both cases {@link #iterator} will be assigned a reference to * an iterator, which can then be used to access the actual data on a row by * row basis. * * @param config * {@link #config} * * @throws IOException * In case file doesn't contain actual data */ protected ImportAdapterExcel(ImportConfigurationExcel config) throws IOException { super(config); this.config = config; /* Get row iterator */ input = new FileInputStream(config.getFileLocation()); Workbook workbook = null; if (config.getExcelFileType() == ExcelFileTypes.XLS) { workbook = new HSSFWorkbook(input); } else if (config.getExcelFileType() == ExcelFileTypes.XLSX) { workbook = new XSSFWorkbook(input); } else { input.close(); throw new IllegalArgumentException("File type not supported"); } workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); Sheet sheet = workbook.getSheetAt(config.getSheetIndex()); iterator = sheet.iterator(); /* Get total number of rows */ totalRows = sheet.getPhysicalNumberOfRows(); /* Check whether there is actual data within the file */ if (iterator.hasNext()) { row = iterator.next(); if (config.getContainsHeader()) { if (!iterator.hasNext()) { throw new IOException("File contains nothing but header"); } } } else { throw new IOException("File contains no data"); } // Create header header = createHeader(); }