List of usage examples for org.apache.poi.ss.usermodel Row getLastCellNum
short getLastCellNum();
From source file:it.greenvulcano.excel.reader.ToCSVReader.java
License:Open Source License
/** * Called to convert a row of cells into a line of data that can be output * to the CSV file./*ww w . j a v a2s.c o m*/ * * @param row * An instance of either the HSSFRow or XSSFRow classes that * encapsulates information about a row of cells recovered from an * Excel workbook. */ @Override protected void processRow(Row row, int sNum, int rNum) throws ExcelException { List<String> csvLine = new ArrayList<String>(); // Check to ensure that a row was recovered from the sheet as it is // possible that one or more rows between other populated rows could be // missing - blank. If the row does contain cells then... if (row != null) { // Get the index for the right most cell on the row and then // step along the row from left to right recovering the contents // of each cell, converting that into a formatted String and // then storing the String into the csvLine ArrayList. int lastCellNum = row.getLastCellNum(); int skipped = 0; for (int i = 0; i <= lastCellNum; i++) { if (colSkipper.skip(sNum, i)) { skipped++; continue; } Cell cell = row.getCell(i); if (cell == null) { csvLine.add(""); } else { if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) { csvLine.add(formatter.formatCellValue(cell)); } else { csvLine.add(formatter.formatCellValue(cell, evaluator)); } } } // Make a note of the index number of the right most cell. This value // will later be used to ensure that the matrix of data in the CSV // file is square. if (lastCellNum > maxRowWidth) { maxRowWidth = lastCellNum - skipped; } } csvRows.add(csvLine); }
From source file:it.greenvulcano.excel.reader.ToXMLReader.java
License:Open Source License
@Override protected void processRow(Row row, int sNum, int rNum) throws ExcelException { try {//from www. j a va2 s . c o m if (row != null) { Element rE = parser.createElement(doc, "r"); parser.setAttribute(rE, "n", String.valueOf(rNum)); shE.appendChild(rE); int lastCellNum = row.getLastCellNum(); for (int i = 0; i <= lastCellNum; i++) { if (colSkipper.skip(sNum, i)) { continue; } Cell cell = row.getCell(i); String value = ""; if (cell != null) { if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) { value = formatter.formatCellValue(cell); } else { value = formatter.formatCellValue(cell, evaluator); } } Element cE = parser.createElement(doc, "c"); parser.setAttribute(cE, "n", String.valueOf(i)); rE.appendChild(cE); Node data = cE.appendChild(parser.createElement(doc, "v")); data.appendChild(doc.createTextNode(value)); } } } catch (Exception exc) { throw new ExcelException("Error parsing Excel", exc); } }
From source file:it.smartcommunitylab.riciclo.app.importer.converter.DataImporter.java
License:Apache License
private List<Map<String, String>> getSheetMap(Sheet sheet) { Row row = sheet.getRow(0); List<String> keys = new ArrayList<String>(); int firstRow = 1; if (row.getLastCellNum() != 1) { for (int j = 0; j < row.getLastCellNum(); j++) { String key = WordUtils .capitalizeFully(" " + getCellValue(row.getCell(j)).replace(' ', '_'), new char[] { '_' }) .replace("_", "").trim(); keys.add(key);// w w w .j av a 2s . com } } else { firstRow = 1; keys.add("valore"); } List<Map<String, String>> result = new ArrayList<Map<String, String>>(); for (int i = firstRow; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); if (row == null) { continue; } Map<String, String> map = new TreeMap<String, String>(); boolean add = false; for (int j = 0; j < row.getLastCellNum(); j++) { if (j >= keys.size()) { continue; } if (row.getCell(j) != null) { String value = getCellValue(row.getCell(j)).replace("_", " ").trim(); if (!value.isEmpty()) { add = true; } try { map.put(keys.get(j), value); } catch (Exception e) { e.printStackTrace(); } } else { map.put(keys.get(j), ""); } } if (add) { result.add(map); } } return result; }
From source file:it.smartcommunitylab.ungiorno.importer.Importer.java
License:Apache License
private List<Map<String, String>> getSheetMap(Sheet sheet) { System.err.println(sheet.getSheetName()); Row row = sheet.getRow(0); List<String> keys = new ArrayList<String>(); int firstRow = 2; if (row.getLastCellNum() != 1) { for (int j = 0; j < row.getLastCellNum(); j++) { String key = getCellValue(row.getCell(j), null).toUpperCase().replace(' ', '_').trim(); keys.add(key);// w w w. j a v a 2s.c om } } else { keys.add("valore"); } List<Map<String, String>> result = new ArrayList<Map<String, String>>(); for (int i = firstRow; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); if (row == null) { continue; } Map<String, String> map = new TreeMap<String, String>(); boolean add = false; for (int j = 0; j < row.getLastCellNum(); j++) { if (j >= keys.size()) { continue; } if (row.getCell(j) != null) { String value = getCellValue(row.getCell(j), keys.get(j)).replace("_", " ").trim(); if (!value.isEmpty()) { add = true; } try { map.put(keys.get(j), value); } catch (Exception e) { e.printStackTrace(); } } else { map.put(keys.get(j), ""); } } if (add) { result.add(map); } } return result; }
From source file:it.vige.greenarea.file.ImportaXLSFile.java
License:Apache License
@Override public List<RichiestaXML> prelevaDati(InputStream inputStream, List<Filtro> filtri) throws Exception { if (filtri != null) acceptedRoundCodes.addAll(filtri); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); int rowsCount = sheet.getPhysicalNumberOfRows(); List<RichiestaXML> richiesteXML = new ArrayList<RichiestaXML>(); for (int i = 1; i < rowsCount; i++) { Row row = sheet.getRow(i); int colCounts = row.getLastCellNum(); RichiestaXML richiestaXML = new RichiestaXML(); for (int j = 0; j < colCounts; j++) { Cell cell = row.getCell(j);/*from w ww . j av a 2 s . com*/ if (cell != null) aggiungiCampoARichiestaXML(richiestaXML, cell, j); } String roundCode = richiestaXML.getRoundCode(); if (acceptRoundCode(roundCode)) richiesteXML.add(richiestaXML); } workbook.close(); return richiesteXML; }
From source file:javacommon.excel.ExcelReader.java
/** * ?//from ww w . j a v a 2s . c o m * * @param sheetIndex * @return */ public int getMaxColumn(int sheetIndex) { if (sheetIndex >= sheets) { return 0; } int maxCol = 0; Sheet sheet = workbook.getSheetAt(sheetIndex); int maxRows = sheet.getLastRowNum(); int cols = 0; Row row = null; for (int i = 0; i < maxRows; i++) { row = sheet.getRow(i); if (row != null) { cols = row.getLastCellNum(); if (cols > maxCol) { maxCol = cols; } } } return maxCol + 1; }
From source file:javacommon.excel.ExcelReader.java
/** * ?/*from w w w. j ava 2 s . c o m*/ * * @param sheetIndex * @param rowIndex * @return */ public boolean isBlankRow(int sheetIndex, int rowIndex) { Row row = getRow(sheetIndex, rowIndex); if (row == null) { return true; } int cols = row.getLastCellNum(); // boolean isBlank = true;//? String str = null; for (int i = 0; i <= cols; i++) { str = getCellStringValue(row.getCell(i)); if (str != null && str.trim().length() > 0) { isBlank = false; break; } } return isBlank; }
From source file:javacommon.excel.ExcelReader.java
/** * ?/* w w w.j a va 2 s . co m*/ * @param sheetIndex * @return */ public List<Integer> getColumnWidths(int sheetIndex) { int rowCount = getTotalRows(sheetIndex); if (rowCount > 0) { Row row = workbook.getSheetAt(sheetIndex).getRow(0); int cols = row.getLastCellNum(); // List<Integer> result = new ArrayList<Integer>(cols); for (int i = 0; i < cols; i++) { result.add(getColumnWidth(sheetIndex, i)); } return result; } return null; }
From source file:javacommon.excel.ExcelReader.java
/** * ?Excel? Null/* w w w . j a v a 2s .c o m*/ * * @param sheetIndex 0 * @param rowIndex 0 * @return ? */ public List<String> getRowStingData(int sheetIndex, int rowIndex) { List<String> result = new Vector<String>(); Row row = getRow(sheetIndex, rowIndex); if (row == null) { return null; } int cols = row.getLastCellNum(); // boolean isBlank = true;//? String str = null; for (int i = 0; i < cols; i++) { str = getCellStringValue(row.getCell(i)); if (str != null && str.trim().length() > 0) { isBlank = false; } result.add(str); } return isBlank ? null : result; }
From source file:javacommon.excel.ExcelReader.java
/** * ?Excel?/* w w w . j a v a2s .co m*/ * * @param sheetIndex 0 * @param rowIndex 0 * @param colBegin 0 * @param colEnd 0 * @return ? */ public List<String> getRowStingData(int sheetIndex, int rowIndex, int colBegin, int colEnd) { List<String> result = new Vector<String>(); Row row = getRow(sheetIndex, rowIndex); if (row == null) { return null; } int cols = row.getLastCellNum(); // if (colEnd > cols) colEnd = cols; String str = null; int i = colBegin; int emptyCount = 0; for (i = 0; i <= colEnd; i++) { Cell cell = row.getCell(i); if (cell != null) { str = getCellStringValue(cell); if ("".equals(str)) { emptyCount++; } } else { emptyCount++; str = ""; } result.add(str); } if (emptyCount == colEnd + 1) { return null; } return result; }