List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum
int getLastRowNum();
From source file:das.pf.io.IOExcel.java
License:Open Source License
private List<Row> getRows(Sheet source, int start) { List<Row> rows = new ArrayList<>(source.getLastRowNum()); for (int index = start; index < source.getLastRowNum(); index++) if (source.getRow(index) != null) rows.add(source.getRow(index)); return rows;/*from w w w.j a v a 2 s. com*/ }
From source file:das.pf.io.IOExcel.java
License:Open Source License
/** * Este metodo se encarga de escribir o llenar las columnas en la hoja objetivo o la hoja * en la que se desea trabajar, con los datos que se encuentran dentro de la hoja fuente. * Los datos que seran toamdos encuenta son todas las columnas menos: "Productos", "Clases Terapeuticas", * "Key Competitors".//from w w w.j av a2 s . c om * * @param sheet * Instancia que modela la hoja objetivo o de destino. * * @param source * Instancia que modela la hoja fuente o que contiene los datos que sedesea * procesar. * * @param start * Indica el indice de la primer fila donde se encuentran los datos. */ private void writerOthersValues(Sheet sheet, Sheet source, int start) { int indexTarget = 3; for (int indexRow = start; indexRow < (source.getLastRowNum() - 1); indexRow++) { Row rowSource = source.getRow(indexRow); if (rowSource != null) { Cell type = rowSource.getCell(2); if (type != null && type.getRichStringCellValue().getString().equals("Packs")) { Row row = sheet.getRow(indexTarget++); if (row != null) { Cell sku = row.createCell(7); Cell laboratory = row.createCell(8); Cell typeOfMark = row.createCell(9); Cell molecules = row.createCell(10); sku.setCellType(Cell.CELL_TYPE_STRING); sku.setCellValue(rowSource.getCell(6).getRichStringCellValue().getString()); laboratory.setCellType(Cell.CELL_TYPE_STRING); laboratory.setCellValue(rowSource.getCell(8).getRichStringCellValue().getString()); typeOfMark.setCellType(Cell.CELL_TYPE_STRING); typeOfMark.setCellValue(rowSource.getCell(9).getRichStringCellValue().getString()); molecules.setCellType(Cell.CELL_TYPE_STRING); molecules.setCellValue(rowSource.getCell(10).getRichStringCellValue().getString()); writeUnitValues(row, rowSource, 11, 155, 14); sku = null; laboratory = null; typeOfMark = null; molecules = null; } row = null; } type = null; } rowSource = null; } }
From source file:das.pf.io.IOExcel.java
License:Open Source License
private int getLasRow(Sheet source) { int count = 0; for (int index = 13; index < source.getLastRowNum(); index++) { try {//from ww w . jav a2 s. c om if (source.getRow(index) != null) { Row r = source.getRow(index); Cell cell = r.getCell(2); if (cell != null && cell.getRichStringCellValue().getString().equalsIgnoreCase("Packs") || cell != null && cell.getRichStringCellValue().getString().equalsIgnoreCase("Prds.") || cell != null && cell.getRichStringCellValue().getString().equalsIgnoreCase("ATC IV")) count++; r = null; cell = null; } } catch (NullPointerException ex) { Logger.getLogger(IOExcel.class.getName()).log(Level.SEVERE, String.format("Index: %d", index), ex); Util.showException("Ocurrio un error opteniendo la ultima fila del archivo", ex); } } return count; }
From source file:das.pf.io.IOExcel.java
License:Open Source License
private XmlContry writeContries(Sheet sheet, int startRow, int column, Path pathFile) { CreationHelper helper = sheet.getWorkbook().getCreationHelper(); XmlContry contry = Util.getContryByAcronym(getAcromynName(pathFile)); if (contry != null) { try {//w ww. java 2s . c o m for (int index = startRow; index < sheet.getLastRowNum() + 1; index++) { Row row = sheet.getRow(index); if (row != null) { Cell cellContry = row.createCell(column); cellContry.setCellType(Cell.CELL_TYPE_STRING); cellContry.setCellValue(helper.createRichTextString(contry.getName())); updateMessages(String.format("Escribiendo el pais: %s en la fila: %d", contry.getName(), index + 1)); } } } catch (Exception ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Error al escribir la columna Pais", ex); Util.showException("Error al escribir la columna Pais", ex); } } return contry; }
From source file:das.pf.io.IOExcel.java
License:Open Source License
private void writeRegions(XmlContry contry, Sheet sheet, int startRow, int column) { CreationHelper helper = sheet.getWorkbook().getCreationHelper(); XmlRegion region = Util.getRegionByContry(contry); if (region != null) { for (int index = startRow; index < sheet.getLastRowNum() + 1; index++) { Row r = sheet.getRow(index); try { if (r != null) { Cell cellRegion = r.createCell(column); cellRegion.setCellType(Cell.CELL_TYPE_STRING); cellRegion.setCellValue(helper.createRichTextString(region.getName())); updateMessages(String.format("Escribiendo la region: %s en la fila: %d", region.getName(), r.getRowNum() + 1)); }/*w w w .j av a 2 s. co m*/ } catch (Exception ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Error al escribir la columna Region", ex); Util.showException("Error al escribir la columna Region", ex); } } } }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceImporter.java
License:Open Source License
private void checkStationsFirst(List<Exception> exceptions, Sheet businessSheet) { HashSet<String> stationIDs = new HashSet<>(); int numRows = businessSheet.getLastRowNum() + 1; for (int i = 1; i < numRows; i++) { Row row = businessSheet.getRow(i); if (row != null) { Cell cell = row.getCell(0); // ID Cell cell1 = row.getCell(1); // Name if ((cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) && (cell1 == null || cell1.getCellType() == Cell.CELL_TYPE_BLANK)) return; if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) exceptions.add(new Exception("Station has no ID -> Row " + (i + 1))); cell.setCellType(Cell.CELL_TYPE_STRING); String val = cell.getStringCellValue(); if (stationIDs.contains(val)) exceptions.add(/* www . ja va 2s . co m*/ new Exception("Station ID '" + val + "' is defined more than once -> Row " + (i + 1))); stationIDs.add(val); } } }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceImporter.java
License:Open Source License
private void checkDeliveriesFirst(List<Exception> exceptions, Sheet deliverySheet) { HashSet<String> deliveryIDs = new HashSet<>(); int numRows = deliverySheet.getLastRowNum() + 1; for (int i = 2; i < numRows; i++) { Row row = deliverySheet.getRow(i); if (row != null) { Cell cell = row.getCell(0); // ID Cell cell1 = row.getCell(1); // Station if ((cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) && (cell1 == null || cell1.getCellType() == Cell.CELL_TYPE_BLANK)) return; if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) exceptions.add(new Exception("Delivery has no ID -> Row " + (i + 1))); cell.setCellType(Cell.CELL_TYPE_STRING); String val = cell.getStringCellValue(); if (deliveryIDs.contains(val)) exceptions.add(//from w w w . j a v a 2 s .c om new Exception("Delivery ID '" + val + "' is defined more than once -> Row " + (i + 1))); deliveryIDs.add(val); } } }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceImporter.java
License:Open Source License
private void checkTraceDeliveries(List<Exception> exceptions, Sheet deliverySheet, int borderRowBetweenTopAndBottom, boolean isForTracing, boolean isNewFormat_151105) { HashMap<String, HashSet<Row>> deliveryIDs = new HashMap<>(); int numRows = deliverySheet.getLastRowNum() + 1; for (int i = 2; i < numRows; i++) { Row row = deliverySheet.getRow(i); if (row != null) { Cell cellM = row.getCell(12); // DeliveryID in DB //if ((cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) && (cell1 == null || cell1.getCellType() == Cell.CELL_TYPE_BLANK)) return; if (isCellEmpty(cellM)) { //exceptions.add(new Exception("Delivery has no ID -> Row " + (i+1))); } else { cellM.setCellType(Cell.CELL_TYPE_STRING); String val = cellM.getStringCellValue().trim(); if (val.isEmpty() || val.equals("DeliveryID in DB")) { } else { if (!deliveryIDs.containsKey(val)) deliveryIDs.put(val, new HashSet<Row>()); HashSet<Row> hs = deliveryIDs.get(val); hs.add(row);/*from w w w. ja v a 2 s.c o m*/ } } /* String key = getRowKey(row, borderRowBetweenTopAndBottom, isForTracing); if (!duplicateRows.containsKey(key)) duplicateRows.put(key, new HashSet<Row>()); HashSet<Row> hs = duplicateRows.get(key); hs.add(row); */ } } for (String val : deliveryIDs.keySet()) { HashSet<Row> hs = deliveryIDs.get(val); if (hs.size() > 1) { String rows = "", key = null; boolean different = false; for (Row tmp : hs) { if (isNewFormat_151105 || tmp.getRowNum() < borderRowBetweenTopAndBottom) { String tkey = getRowKey(tmp, borderRowBetweenTopAndBottom, isForTracing); if (key == null) key = tkey; else if (!key.equals(tkey)) different = true; rows += ";" + (tmp.getRowNum() + 1); } } if (different) exceptions.add(new Exception("Delivery ID '" + val + "' is defined more than once -> Rows: " + rows.substring(1) + ". If you have copy/pasted a new row, please clear the cell for the DeliveryID of the new Row in Column 'M' (expand it firstly to be able to see it).")); } } /* for (String val : duplicateRows.keySet()) { HashSet<Integer> hs = duplicateRows.get(val); if (hs.size() > 1) { String rows = ""; for (Integer tmp : hs) { rows += ";" + tmp; } exceptions.add(new Exception("Rows are identical, but Delivery IDs are different -> Rows: " + rows.substring(1) + ". You may want to give them the same ID in Column 'M'.")); } } */ }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceImporter.java
License:Open Source License
private void loadLookupSheet(Sheet lookupSheet) { LookUp lu = new LookUp(); int numRows = lookupSheet.getLastRowNum() + 1; for (int i = 1; i < numRows; i++) { Row row = lookupSheet.getRow(i); if (row != null) { Cell cell = row.getCell(0); // Sampling if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { cell.setCellType(Cell.CELL_TYPE_STRING); lu.addSampling(cell.getStringCellValue()); }//w w w . j a v a 2s.c o m cell = row.getCell(1); // TypeOfBusiness if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { cell.setCellType(Cell.CELL_TYPE_STRING); lu.addTypeOfBusiness(cell.getStringCellValue()); } cell = row.getCell(2); // Treatment if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { cell.setCellType(Cell.CELL_TYPE_STRING); lu.addTreatment(cell.getStringCellValue()); } cell = row.getCell(3); // Units if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { cell.setCellType(Cell.CELL_TYPE_STRING); lu.addUnit(cell.getStringCellValue()); } } } lu.intoDb(mydbi); }
From source file:de.bund.bfr.knime.openkrise.db.imports.custom.bfrnewformat.TraceImporter.java
License:Open Source License
private List<Exception> doTheImport(Workbook wb, String filename) { // throws Exception List<Exception> exceptions = new ArrayList<>(); Sheet stationSheet = wb.getSheet("Stations"); Sheet deliverySheet = wb.getSheet("Deliveries"); Sheet d2dSheet = wb.getSheet("Deliveries2Deliveries"); Sheet transactionSheet = wb.getSheet("BackTracing"); Sheet lookupSheet = wb.getSheet("LookUp"); Sheet forwardSheet = wb.getSheet("Opt_ForwardTracing"); Sheet forwardSheetNew = wb.getSheet("ForwardTracing_Opt"); Sheet forSheet = wb.getSheet("ForTracing"); Sheet fwdSheet = wb.getSheet("FwdTracing"); if (forSheet == null) forSheet = fwdSheet;/*from w ww . j a v a2 s. c om*/ boolean isForTracing = forSheet != null; if (isForTracing) transactionSheet = forSheet; if (stationSheet == null || transactionSheet == null && deliverySheet == null) { exceptions.add(new Exception("Wrong template format!")); return exceptions; } checkStationsFirst(exceptions, stationSheet); if (deliverySheet != null) { checkDeliveriesFirst(exceptions, deliverySheet); // load all Stations HashMap<String, Station> stations = new HashMap<>(); int numRows = stationSheet.getLastRowNum() + 1; Row titleRow = stationSheet.getRow(0); for (classRowIndex = 1; classRowIndex < numRows; classRowIndex++) { Station s = getStation(titleRow, stationSheet.getRow(classRowIndex)); if (s == null) break; if (stations.containsKey(s.getId())) exceptions.add(new Exception("Station defined twice -> Row " + (classRowIndex + 1) + "; Station Id: '" + s.getId() + "'")); stations.put(s.getId(), s); } // load all Deliveries HashMap<String, Delivery> deliveries = new HashMap<>(); numRows = deliverySheet.getLastRowNum() + 1; titleRow = deliverySheet.getRow(0); HashMap<String, String> definedLots = new HashMap<>(); HashMap<String, Integer> deliveryRows = new HashMap<>(); for (classRowIndex = 2; classRowIndex < numRows; classRowIndex++) { Delivery d = getMultiOutDelivery(exceptions, stations, titleRow, deliverySheet.getRow(classRowIndex), definedLots, classRowIndex, filename, d2dSheet != null); if (d == null) break; if (deliveries.containsKey(d.getId())) exceptions.add(new Exception("Delivery defined twice -> in Row " + (classRowIndex + 1) + " and in Row " + deliveryRows.get(d.getId()) + "; Delivery Id: '" + d.getId() + "'")); else deliveryRows.put(d.getId(), classRowIndex + 1); deliveries.put(d.getId(), d); } // load Recipes HashSet<D2D> recipes = new HashSet<>(); if (d2dSheet != null) { numRows = d2dSheet.getLastRowNum() + 1; titleRow = d2dSheet.getRow(0); for (classRowIndex = 1; classRowIndex < numRows; classRowIndex++) { D2D dl = getD2D(exceptions, deliveries, titleRow, d2dSheet.getRow(classRowIndex), classRowIndex); if (dl == null) break; recipes.add(dl); } } MetaInfo mi = new MetaInfo(); mi.setFilename(filename); if (lookupSheet != null) loadLookupSheet(lookupSheet); Integer miDbId = null; try { miDbId = mi.getID(mydbi); } catch (Exception e) { exceptions.add(e); } if (miDbId == null) exceptions.add(new Exception("File already imported")); for (Delivery d : deliveries.values()) { try { d.getID(miDbId, false, mydbi); } catch (Exception e) { exceptions.add(e); } //if (!d.getLogMessages().isEmpty()) logMessages += d.getLogMessages() + "\n"; if (d.getExceptions().size() > 0) exceptions.addAll(d.getExceptions()); } HashMap<Delivery, HashSet<Integer>> ingredients = new HashMap<>(); for (D2D dl : recipes) { try { dl.getId(miDbId, mydbi); } catch (Exception e) { exceptions.add(e); } // collect data for checks if data is missing... Delivery d = dl.getTargetDelivery(); if (!ingredients.containsKey(d)) ingredients.put(d, new HashSet<Integer>()); HashSet<Integer> hd = ingredients.get(d); if (dl.getIngredient() != null) hd.add(dl.getIngredient().getDbId()); } return exceptions; } int borderRowLotStart = 0; Row row = transactionSheet.getRow(0); Row titleRow; Cell cell; HashMap<String, Delivery> outDeliveries = new HashMap<>(); HashMap<String, Lot> outLots = new HashMap<>(); Station sif; MetaInfo mi; boolean isNewFormat_151105 = false; if (forwardSheet != null) { // Station in focus cell = row.getCell(1); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) exceptions.add(new Exception("Station in Focus not defined")); cell.setCellType(Cell.CELL_TYPE_STRING); sif = getStation(exceptions, stationSheet, cell.getStringCellValue(), row); // Delivery(s) Outbound classRowIndex = 5; titleRow = transactionSheet.getRow(classRowIndex - 2); for (;; classRowIndex++) { row = transactionSheet.getRow(classRowIndex); if (row == null) continue; if (isBlockEnd(row, 13, "Reporter Information")) break; Delivery d = getDelivery(exceptions, stationSheet, sif, row, true, titleRow, filename, false, null, outDeliveries, false, isNewFormat_151105); if (d == null) continue; outDeliveries.put(d.getId(), d); outLots.put(d.getLot().getNumber(), d.getLot()); } // Metadata on Reporter classRowIndex = getNextBlockRowIndex(transactionSheet, classRowIndex, "Reporter Information") + 2; row = transactionSheet.getRow(classRowIndex); mi = getMetaInfo(exceptions, row, transactionSheet.getRow(classRowIndex - 1)); mi.setFilename(filename); } else { // Reporter shifted to the top // Metadata on Reporter classRowIndex = getNextBlockRowIndex(transactionSheet, 0, "Reporter Information") + 2; row = transactionSheet.getRow(classRowIndex); mi = getMetaInfo(exceptions, row, transactionSheet.getRow(classRowIndex - 1)); mi.setFilename(filename); // Station in focus classRowIndex = getNextBlockRowIndex(transactionSheet, classRowIndex, "Station in Focus:"); row = transactionSheet.getRow(classRowIndex); cell = row.getCell(1); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) exceptions.add(new Exception("Station in Focus not defined")); cell.setCellType(Cell.CELL_TYPE_STRING); sif = getStation(exceptions, stationSheet, cell.getStringCellValue(), row); String label = "Products Out"; if (isForTracing) label = "Ingredients In for Lot(s)"; // Delivery(s) Outbound classRowIndex = getNextBlockRowIndex(transactionSheet, classRowIndex, label) + 3; titleRow = transactionSheet.getRow(classRowIndex - 2); cell = titleRow.getCell(0); isNewFormat_151105 = cell.getStringCellValue().equals("Product Lot Number"); for (;; classRowIndex++) { row = transactionSheet.getRow(classRowIndex); if (row == null) continue; if (isBlockEnd(row, 13, "Lot Information")) break; Delivery d = getDelivery(exceptions, stationSheet, sif, row, !isForTracing, titleRow, filename, isForTracing, outLots, outDeliveries, false, isNewFormat_151105); if (d == null) continue; outDeliveries.put(d.getId(), d); if (!isForTracing) outLots.put(d.getLot().getNumber(), d.getLot()); } } String label = "Ingredients In for Lot(s)"; if (isForTracing) label = "Products Out"; // Lot(s) classRowIndex = getNextBlockRowIndex(transactionSheet, classRowIndex, "Lot Information") + 3; borderRowLotStart = classRowIndex; titleRow = transactionSheet.getRow(classRowIndex - 2); for (;; classRowIndex++) { row = transactionSheet.getRow(classRowIndex); if (row == null) continue; if (isBlockEnd(row, 13, label)) break; if (!fillLot(exceptions, row, sif, outLots, titleRow, isForTracing ? outDeliveries : null, classRowIndex + 1, isNewFormat_151105)) { exceptions.add(new Exception("Lot number unknown in Row number " + (classRowIndex + 1))); } } checkTraceDeliveries(exceptions, transactionSheet, borderRowLotStart, isForTracing, isNewFormat_151105); // Deliveries/Recipe Inbound boolean hasIngredients = false; label = "Ingredients for Lot(s)"; if (isForTracing) label = "Products Out"; classRowIndex = getNextBlockRowIndex(transactionSheet, classRowIndex, label) + 3; HashMap<String, Delivery> inDeliveries = new HashMap<>(); int numRows = transactionSheet.getLastRowNum() + 1; titleRow = transactionSheet.getRow(classRowIndex - 2); for (; classRowIndex < numRows; classRowIndex++) { row = transactionSheet.getRow(classRowIndex); if (row == null) continue; if (isBlockEnd(row, 13, null)) break; Delivery d = getDelivery(exceptions, stationSheet, sif, row, isForTracing, titleRow, filename, isForTracing, outLots, inDeliveries, false, isNewFormat_151105); if (d == null) continue; if (!isForTracing && d.getTargetLotIds().size() == 0) exceptions.add(new Exception("Lot number unknown in Row number " + (classRowIndex + 1))); inDeliveries.put(d.getId(), d); hasIngredients = true; } if (!hasIngredients) { warns.put("No " + (isForTracing ? "Products Out" : "ingredients") + " defined...", null); } // Opt_ForwardTracing HashSet<Delivery> forwDeliveries = new HashSet<>(); if (!isForTracing) { if (forwardSheet == null) forwardSheet = forwardSheetNew; numRows = forwardSheet.getLastRowNum() + 1; titleRow = forwardSheet.getRow(0); for (classRowIndex = 2; classRowIndex < numRows; classRowIndex++) { row = transactionSheet.getRow(classRowIndex); if (row == null) continue; Delivery d = getForwardDelivery(exceptions, stationSheet, outLots, titleRow, forwardSheet.getRow(classRowIndex), isNewFormat_151105); if (d == null) continue; forwDeliveries.add(d); } } if (lookupSheet != null) loadLookupSheet(lookupSheet); Integer miDbId = null; try { miDbId = mi.getID(mydbi); } catch (Exception e) { exceptions.add(e); } if (miDbId == null) exceptions.add(new Exception("File already imported")); if (isForTracing) try { insertForIntoDb(exceptions, miDbId, inDeliveries, outDeliveries); } catch (Exception e) { exceptions.add(e); } else try { insertIntoDb(exceptions, miDbId, inDeliveries, outDeliveries, forwDeliveries); } catch (Exception e) { exceptions.add(e); } return exceptions; }