List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem
public POIFSFileSystem(InputStream stream) throws IOException
From source file:projet_muticriteres.Fenetre.java
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed final JFileChooser fc = new JFileChooser(new File(".")); FileNameExtensionFilter filtre = new FileNameExtensionFilter("Fichier Excel", "xlsx", "xls"); fc.setFileFilter(filtre);//ww w.j a va 2s . c o m File fichier; if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { fichier = fc.getSelectedFile(); try { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fichier.getCanonicalPath())); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = null; HSSFCell cell = null; double totalLigne = 0.0; double totalGeneral = 0.0; int numLigne = 1; int ligne = 0; int colonne = 0; int k; row = sheet.getRow(0); while (sheet.getRow(ligne) != null) { row = sheet.getRow(ligne); colonne = 0; k = 0; if (ligne == 0) { colonne = 1; while (row.getCell(colonne) != null) { cell = row.getCell(colonne); tabCriteres.add(cell.getStringCellValue()); colonne++; } } else { while (row.getCell(colonne) != null) { cell = row.getCell(colonne); if (colonne == 0) { action = new Action(cell.getStringCellValue(), tabCriteres.size()); } else { action.ajouterNote((int) cell.getNumericCellValue(), colonne - 1); } colonne++; } tabActions.add(action); } ligne++; } fs.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /*for (int i=0; i<tabCriteres.size(); i++){ System.out.println("critere "+i+" : "+tabCriteres.get(i)); } for (int i=0; i<tabActions.size(); i++){ System.out.println("action "+i+" : "+tabActions.get(i).getNom()); for (int j=0; j<tabCriteres.size();j++){ System.out.println("note"+j+":"+tabActions.get(i).getNote(j)); } }*/ titreColonne = new String[tabCriteres.size() + 1]; donnees = new Object[tabActions.size()][tabCriteres.size() + 1]; titreColonne[0] = ""; for (int i = 0; i < tabCriteres.size(); i++) { titreColonne[i + 1] = tabCriteres.get(i); } for (int i = 0; i < tabActions.size(); i++) { donnees[i][0] = tabActions.get(i).getNom(); } for (int j = 0; j < tabActions.size(); j++) { for (int k = 1; k < tabCriteres.size() + 1; k++) { donnees[j][k] = tabActions.get(j).getNote(k - 1); } } model = new DefaultTableModel(donnees, titreColonne); jTable1.setModel(model); // this.getContentPane().add(new JScrollPane(tab)); //this.add(tab); //jPanel2.add(label10); //jButton6.setLabel("buton"); //jPanel2.add(jButton6); jTable1.setVisible(true); //jPanel2.repaint(); for (int i = 0; i < tabCriteres.size() + 1; i++) { System.out.println("critere " + i + " : " + titreColonne[i]); } for (int i = 0; i < tabActions.size(); i++) { System.out.println("action " + i + " : " + donnees[i][0]); for (int j = 1; j < tabCriteres.size() + 1; j++) { System.out.println("note" + j + ":" + donnees[i][j]); } } } }
From source file:qa.dataset.java
public void readfile() { try {/*from w w w . j ava2 s. c o m*/ InputStream input = new BufferedInputStream(new FileInputStream("E:/Tugas Akhir Ali/dataset.xls")); POIFSFileSystem filesystem = new POIFSFileSystem(input); HSSFWorkbook workbook = new HSSFWorkbook(filesystem); HSSFSheet sheet = workbook.getSheetAt(0); String a; String b; MaxentTagger tagger = new MaxentTagger( "E:/Tugas Akhir Ali/stanford-postagger-2011-04-20/models/left3words-wsj-0-18.tagger"); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); System.out.println("\n"); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) System.out.print(cell.getNumericCellValue() + " "); else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) System.out.print(cell.getStringCellValue() + " "); else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType()) System.out.print("Blank"); else System.out.print("Unknown"); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:QuickExcel.ExcelData.java
public void loadData() { try {/*www . ja va2s . c om*/ File file = new File("excel.xls"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row; HSSFCell cell; int rows; // No of rows rows = sheet.getPhysicalNumberOfRows(); int cols = 0; // No of columns int tmp = 0; // This trick ensures that we get the data properly even if it doesn't start from first few rows for (int i = 0; i < 10 || i < rows; i++) { row = sheet.getRow(i); if (row != null) { tmp = sheet.getRow(i).getPhysicalNumberOfCells(); if (tmp > cols) { cols = tmp; } } } for (int r = 0; r < rows; r++) { row = sheet.getRow(r); if (row != null) { for (int c = 0; c < 2; c++) { cell = row.getCell(c); if (c == 0) { if (cell != null) { addQuestion(cell.toString()); } } else { if (cell != null) { addAnswer(cell.toString()); } } } } } } catch (Exception ioe) { ioe.printStackTrace(); } }
From source file:ro.nextreports.engine.exporter.XlsExporter.java
License:Apache License
public static void createSummaryInformation(String filePath, String title) { if (filePath == null) { return;/* w ww . j av a 2 s .c om*/ } try { File poiFilesystem = new File(filePath); InputStream is = new FileInputStream(poiFilesystem); POIFSFileSystem poifs = new POIFSFileSystem(is); is.close(); DirectoryEntry dir = poifs.getRoot(); SummaryInformation si = PropertySetFactory.newSummaryInformation(); si.setTitle(title); si.setAuthor(ReleaseInfoAdapter.getCompany()); si.setApplicationName("NextReports " + ReleaseInfoAdapter.getVersionNumber()); si.setSubject("Created by NextReports Designer" + ReleaseInfoAdapter.getVersionNumber()); si.setCreateDateTime(new Date()); si.setKeywords(ReleaseInfoAdapter.getHome()); si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME); OutputStream out = new FileOutputStream(poiFilesystem); poifs.writeFilesystem(out); out.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:ru.green.report.Report.java
License:Open Source License
/** * ./* www.j a va2s . co m*/ * * @throws IOException * ? */ public void readTemplate() throws IOException { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("etc/grid.xlt")); wb = new HSSFWorkbook(fs); sheet = wb.getSheetAt(0); cellStyle = wb.createCellStyle(); captionStyle = wb.createCellStyle(); font = wb.createFont(); createCaptionStyle(); createCellStyle(); }
From source file:servlets.servlets_resources.Analysis_XLS_parser.java
License:Open Source License
/** * * @param file//w ww . j av a2s.c o m * @param owners * @return * @throws Exception */ public static RAWdata parse_GCMS_XLSfile(File file, String[] owners) throws Exception { InputStream input = new BufferedInputStream(new FileInputStream(file)); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); int sheetNumber = wb.getNumberOfSheets(); if (sheetNumber < 2) { throw new Exception( "Error trying to insert the GC-MS information: Invalid File, template file must have 2 sheets."); } RAWdata rawDataInstance = new RAWdata("STxxxxx.1"); rawDataInstance.setRawDataType("Proteomics"); ColumnChromatography columnChromatography = parse_ColumnChromatography_XLSfile(wb.getSheetAt(0), rawDataInstance); MassSpectrometry massSpectrometry = parse_MassSpectrometry_XLSfile(wb.getSheetAt(1), rawDataInstance); massSpectrometry.setSeparationMethod(columnChromatography); rawDataInstance.setExtractionMethod(massSpectrometry); return rawDataInstance; }
From source file:servlets.servlets_resources.BioCondition_XLS_parser.java
License:Open Source License
public static Object[] parseXLSfile(File file, String owner) throws Exception { HashMap<String, Batch> batchesTable = new HashMap<String, Batch>(); HashMap<String, Protocol> protocolTable = new HashMap<String, Protocol>(); HashMap<String, Bioreplicate> bioreplicatesTable = new HashMap<String, Bioreplicate>(); ArrayList<BioCondition> biocondition_list = new ArrayList<BioCondition>(); InputStream input = new BufferedInputStream(new FileInputStream(file)); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); int sheetNumber = wb.getNumberOfSheets(); if (sheetNumber < 3) { throw new Exception( "Error trying to insert the Sample information: Invalid File, template file must have 3 sheets."); }// www . j a v a 2s .c om //PARSE THE COMMON BIOLOGICAL CONDITION INFORMATION HSSFSheet sheet = wb.getSheetAt(0); if (!"COMMON BIOLOGICAL CONDITION INF".equals(sheet.getSheetName())) { throw new Exception( "Error trying to insert the Sample information: Invalid File, expected COMMON BIOLOGICAL CONDITION INF sheet not found."); } BioCondition biocondition_common = new BioCondition(); biocondition_common.setTitle(sheet.getRow(3).getCell(1).getStringCellValue()); biocondition_common.setName(sheet.getRow(4).getCell(1).getStringCellValue()); biocondition_common.setOrganism(sheet.getRow(5).getCell(1).getStringCellValue()); biocondition_common.setTissueType(sheet.getRow(6).getCell(1).getStringCellValue()); biocondition_common.setCellType(sheet.getRow(7).getCell(1).getStringCellValue()); biocondition_common.setCellLine(sheet.getRow(8).getCell(1).getStringCellValue()); biocondition_common.setGender(sheet.getRow(9).getCell(1).getStringCellValue()); biocondition_common.setGenotype(sheet.getRow(10).getCell(1).getStringCellValue()); biocondition_common.setOtherBiomat(sheet.getRow(11).getCell(1).getStringCellValue()); biocondition_common.setTreatment(sheet.getRow(13).getCell(1).getStringCellValue()); biocondition_common.setDosis(sheet.getRow(14).getCell(1).getStringCellValue()); biocondition_common.setTime(sheet.getRow(15).getCell(1).getStringCellValue()); biocondition_common.setOtherExpCond(sheet.getRow(16).getCell(1).getStringCellValue()); biocondition_common.setProtocolDescription(sheet.getRow(17).getCell(1).getStringCellValue()); biocondition_common.setExternalLinks(sheet.getRow(19).getCell(1).getStringCellValue()); Date date_aux = new Date(); String today = String.format("%02d", date_aux.getYear() + 1900) + String.format("%02d", date_aux.getMonth() + 1) + String.format("%02d", date_aux.getDate()); biocondition_common.setLastEditionDate(today); biocondition_common.setSubmissionDate(today); biocondition_common.setBioConditionID("BC" + (biocondition_list.size() + 1)); //TODO: CAMBIAR ESTO!! biocondition_common.addOwner(new User(owner, "")); //************************************************************************************************************************************** //**BATCHES PARSING********************************************************************************************************************* //************************************************************************************************************************************** sheet = wb.getSheetAt(1); if (!"BATCHES INFO".equals(sheet.getSheetName())) { throw new Exception( "Error trying to insert the Sample information: Invalid File, expected BATCHES INFO sheet not found."); } Iterator rows = sheet.rowIterator(); //IGNORE THE FIRST 4 ROWS for (int i = 0; i < 4; i++) { rows.next(); } Batch batch; while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); String batch_id = row.getCell(0).getStringCellValue(); String batch_name = row.getCell(1).getStringCellValue(); if (batch_name.isEmpty()) { break; } Date batch_date = row.getCell(2).getDateCellValue(); String batch_date_string = String.format("%02d", batch_date.getYear() + 1900) + String.format("%02d", batch_date.getMonth() + 1) + String.format("%02d", batch_date.getDate()); String batch_description = row.getCell(3).getStringCellValue(); batch = new Batch(); batch.setBatchID(batch_id); batch.setBatchName(batch_name); batch.setBatchCreationDate(batch_date_string); batch.setDescription(batch_description); batch.addOwner(new User(owner, "")); batchesTable.put(batch_id, batch); } //************************************************************************************************************************************** //**PROTOCOL PARSING********************************************************************************************************************* //************************************************************************************************************************************** sheet = wb.getSheetAt(3); if (!"PROTOCOLS INFO".equals(sheet.getSheetName())) { throw new Exception( "Error trying to insert the Sample information: Invalid File, expected PROTOCOLS INFO sheet not found."); } rows = sheet.rowIterator(); //IGNORE THE FIRST 4 ROWS for (int i = 0; i < 4; i++) { rows.next(); } Protocol protocol; while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); String protocol_id = row.getCell(0).getStringCellValue(); String protocol_name = row.getCell(1).getStringCellValue(); if (protocol_name.isEmpty()) { break; } String extracted_molecule = row.getCell(2).getStringCellValue(); String protocol_description = row.getCell(3).getStringCellValue(); protocol = new Protocol(); protocol.setProtocolID(protocol_id); protocol.setProtocolName(protocol_name); protocol.setBiomolecule(extracted_molecule); protocol.setDescription(protocol_description); protocol.addOwner(new User(owner, "")); protocolTable.put(protocol_id, protocol); } //PARSE THE BIOLOGICAL CONDITION INFORMATION sheet = wb.getSheetAt(2); if (!"BIOLOGICAL REPLICATES INFO".equals(sheet.getSheetName())) { throw new Exception( "Error trying to insert the Sample information: Invalid File, expected BIOLOGICAL REPLICATES INFO sheet not found."); } Bioreplicate bioreplicateInstance = null; rows = sheet.rowIterator(); //IGNORE THE FIRST 6 ROWS for (int i = 0; i < 6; i++) { rows.next(); } while (rows.hasNext()) { //GET THE ROW HSSFRow row = (HSSFRow) rows.next(); if (row.getCell(0) == null) { break; } //GET THE FIRST 3 FIELDS String bioreplicate_id = row.getCell(0).getStringCellValue(); String bioreplicate_name = row.getCell(1).getStringCellValue(); if (bioreplicate_name.isEmpty()) { break; } bioreplicateInstance = new Bioreplicate("", "", bioreplicate_name); String batch_id = row.getCell(2).getStringCellValue(); if (!batch_id.isEmpty()) { Batch associatedBatch = batchesTable.get(batch_id); //If the specified batch is not a to-be-added batch it should be added previously in the db if (associatedBatch == null) { associatedBatch = new Batch(); associatedBatch.setBatchID(batch_id); } bioreplicateInstance.setAssociatedBatch(associatedBatch); } bioreplicatesTable.put(bioreplicate_id, bioreplicateInstance); //PARSE THE VARITIONS IN BIOLOGICAL CONDITION (IF EXISTS) BioCondition biocondition_tmp = (BioCondition) biocondition_common.clone(); if (!row.getCell(3).getStringCellValue().isEmpty()) { biocondition_tmp.setTitle(row.getCell(3).getStringCellValue()); } if (!row.getCell(4).getStringCellValue().isEmpty()) { biocondition_tmp.setName(row.getCell(4).getStringCellValue()); } if (!row.getCell(5).getStringCellValue().isEmpty()) { biocondition_tmp.setOrganism(row.getCell(5).getStringCellValue()); } if (!row.getCell(6).getStringCellValue().isEmpty()) { biocondition_tmp.setTissueType(row.getCell(6).getStringCellValue()); } if (!row.getCell(7).getStringCellValue().isEmpty()) { biocondition_tmp.setCellType(row.getCell(7).getStringCellValue()); } if (!row.getCell(8).getStringCellValue().isEmpty()) { biocondition_tmp.setCellLine(row.getCell(8).getStringCellValue()); } if (!row.getCell(9).getStringCellValue().isEmpty()) { biocondition_tmp.setGender(row.getCell(9).getStringCellValue()); } if (!row.getCell(10).getStringCellValue().isEmpty()) { biocondition_tmp.setGenotype(row.getCell(10).getStringCellValue()); } if (!row.getCell(11).getStringCellValue().isEmpty()) { biocondition_tmp.setOtherBiomat(row.getCell(11).getStringCellValue()); } if (!row.getCell(12).getStringCellValue().isEmpty()) { biocondition_tmp.setTreatment(row.getCell(12).getStringCellValue()); } if (!row.getCell(13).getStringCellValue().isEmpty()) { biocondition_tmp.setDosis(row.getCell(13).getStringCellValue()); } if (!row.getCell(14).getStringCellValue().isEmpty()) { biocondition_tmp.setTime(row.getCell(14).getStringCellValue()); } if (!row.getCell(15).getStringCellValue().isEmpty()) { biocondition_tmp.setOtherExpCond(row.getCell(15).getStringCellValue()); } if (!row.getCell(16).getStringCellValue().isEmpty()) { biocondition_tmp.setProtocolDescription(row.getCell(16).getStringCellValue()); } //CHECK IF NO ONE FIELD WAS FILLED, IF SO THE BIOREPLICATE SHOULD BE ADDED TO THE //COMMON BIOLOGICAL CONDITION's BIOREPLICATE LIST int i = 0; for (i = 0; i < biocondition_list.size(); i++) { if (biocondition_list.get(i).hasSameValues(biocondition_tmp)) { biocondition_list.get(i).addAssociatedBioreplicate(bioreplicateInstance); break; } } //IF NO SIMILAR biocondition WAS FOUND, WE SHOULD ADD A NEW ONE if (i == biocondition_list.size()) { biocondition_tmp.addAssociatedBioreplicate(bioreplicateInstance); biocondition_tmp.setBioConditionID("BS" + (biocondition_list.size() + 1)); biocondition_list.add(biocondition_tmp); } } //************************************************************************************************************************************** //**PROTOCOL PARSING********************************************************************************************************************* //************************************************************************************************************************************** sheet = wb.getSheetAt(4); if (!"ANALYTICAL SAMPLES INFO".equals(sheet.getSheetName())) { throw new Exception( "Error trying to insert the Sample information: Invalid File, expected PROTOCOLS INFO sheet not found."); } rows = sheet.rowIterator(); //IGNORE THE FIRST 4 ROWS for (int i = 0; i < 4; i++) { rows.next(); } AnalyticalReplicate analyticalSampleInstance; while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); String bioreplicateID = row.getCell(0).getStringCellValue(); String protocolID = row.getCell(1).getStringCellValue(); String analyticalSampleName = row.getCell(2).getStringCellValue(); if (analyticalSampleName.isEmpty()) { break; } bioreplicateInstance = bioreplicatesTable.get(bioreplicateID); analyticalSampleInstance = new AnalyticalReplicate(); analyticalSampleInstance.setBioreplicateID(bioreplicateInstance.getBioreplicateID()); analyticalSampleInstance.setProtocolID(protocolID); analyticalSampleInstance.setAnalyticalReplicateName(analyticalSampleName); bioreplicateInstance.addAssociatedAnalyticalReplicate(analyticalSampleInstance); } Object[] data = new Object[3]; data[0] = biocondition_list; data[1] = batchesTable; data[2] = protocolTable; return data; }
From source file:servlets.UploadAccountingServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TEST"); InputStream inputStream = null; Part filePart = request.getPart("file"); inputStream = filePart.getInputStream(); POIFSFileSystem fs = new POIFSFileSystem(inputStream); // FileInputStream fichier = new FileInputStream(new File("countries.xls")); //create workbook instance that refers to xlsx file HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0);/*from www. j a v a 2 s.co m*/ FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); ArrayList<Accounting> arrAccounting = new ArrayList<>(); ReportDB reportDB = new ReportDB(); reportDB.addReport(new Report("Accounting", "Pending")); ArrayList<Report> reportList = new ArrayList(); reportList = reportDB.getAllReports(); int ReportRef = reportList.size(); for (Row ligne : sheet) {//iterate rows Accounting accounting = new Accounting(); for (Cell cell : ligne) {//iterate columns //cell type /* Sales sales; (lagay sa sys out) sales.setSalesID(cell.getNumericCellValue()); --- WALA TO */ switch (formulaEvaluator.evaluateInCell(cell).getCellType()) { case Cell.CELL_TYPE_NUMERIC: // sales.setName(cell.getNumericCellValue()); System.out.print("cell 1 " + cell.getNumericCellValue() + " \t"); if (cell.getColumnIndex() == 1) { accounting.setPoNo((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 4) { accounting.setAgingDays((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 5) { accounting.setCustomerID((int) cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print("cell 2 " + cell.getStringCellValue() + " \t"); if (cell.getColumnIndex() == 0) { accounting.setStartDate(cell.getStringCellValue()); } if (cell.getColumnIndex() == 2) { accounting.setTerms(cell.getStringCellValue()); } if (cell.getColumnIndex() == 3) { accounting.setDueDate(cell.getStringCellValue()); } break; } } accounting.setReportRef(ReportRef); arrAccounting.add(accounting); System.out.println(); } request.setAttribute("arrAccounting", arrAccounting); AccountingDB accountingDB = new AccountingDB(); request.setAttribute("arrAccounting", arrAccounting); for (int j = 0; j < arrAccounting.size(); j++) { accountingDB.addAccounting(arrAccounting.get(j)); } ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher("/isrAfteruploadAccounting.jsp"); rd.forward(request, response); processRequest(request, response); }
From source file:servlets.UploadInventoryServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TEST"); InputStream inputStream = null; Part filePart = request.getPart("file"); inputStream = filePart.getInputStream(); POIFSFileSystem fs = new POIFSFileSystem(inputStream); // FileInputStream fichier = new FileInputStream(new File("countries.xls")); //create workbook instance that refers to xlsx file HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0);//from w w w. j a v a2 s . c o m FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); ArrayList<Inventory> arrInventory = new ArrayList<>(); ReportDB reportDB = new ReportDB(); reportDB.addReport(new Report("Inventory", "Pending")); ArrayList<Report> reportList = new ArrayList(); reportList = reportDB.getAllReports(); int ReportRef = reportList.size(); for (Row ligne : sheet) {//iterate rows Inventory inventory = new Inventory(); for (Cell cell : ligne) {//iterate columns //cell type /* Sales sales; (lagay sa sys out) sales.setSalesID(cell.getNumericCellValue()); --- WALA TO */ switch (formulaEvaluator.evaluateInCell(cell).getCellType()) { case Cell.CELL_TYPE_NUMERIC: // sales.setName(cell.getNumericCellValue()); System.out.print("cell 1 " + cell.getNumericCellValue() + " \t"); if (cell.getColumnIndex() == 1) { inventory.setQuantityOnHand((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 2) { inventory.setGrandTotal((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 3) { inventory.setBatchNo((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 6) { inventory.setAvemonTO(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print("cell 2 " + cell.getStringCellValue() + " \t"); if (cell.getColumnIndex() == 0) { inventory.setBrandName(cell.getStringCellValue()); } if (cell.getColumnIndex() == 4) { inventory.setExpDate(cell.getStringCellValue()); } if (cell.getColumnIndex() == 5) { inventory.setShelfLife(cell.getStringCellValue()); } if (cell.getColumnIndex() == 7) { inventory.setInventoryMonths(cell.getStringCellValue()); } break; } } inventory.setReportRef(ReportRef); arrInventory.add(inventory); System.out.println(); } request.setAttribute("arrInventory", arrInventory); InventoryDB inventoryDB = new InventoryDB(); request.setAttribute("arrInventory", arrInventory); for (int j = 0; j < arrInventory.size(); j++) { inventoryDB.addInventory(arrInventory.get(j)); } ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher("/isrAfteruploadInventory.jsp"); rd.forward(request, response); processRequest(request, response); }
From source file:servlets.UploadSalesServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from www. ja v a 2 s. c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TEST"); InputStream inputStream = null; Part filePart = request.getPart("file"); inputStream = filePart.getInputStream(); POIFSFileSystem fs = new POIFSFileSystem(inputStream); // FileInputStream fichier = new FileInputStream(new File("countries.xls")); //create workbook instance that refers to xlsx file HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); ArrayList<Sales> arrSales = new ArrayList<>(); ReportDB reportDB = new ReportDB(); reportDB.addReport(new Report("Sales", "Pending")); ArrayList<Report> reportList = new ArrayList(); reportList = reportDB.getAllReports(); int ReportRef = reportList.size(); for (Row ligne : sheet) {//iterate rows Sales sales = new Sales(); for (Cell cell : ligne) {//iterate columns //cell type switch (formulaEvaluator.evaluateInCell(cell).getCellType()) { case Cell.CELL_TYPE_NUMERIC: // sales.setName(cell.getNumericCellValue()); System.out.print("cell 1 " + cell.getNumericCellValue() + " \t"); if (cell.getColumnIndex() == 0) { sales.setSalesAmmount(cell.getNumericCellValue()); } if (cell.getColumnIndex() == 1) { sales.setCreatedBy((int) cell.getNumericCellValue()); } if (cell.getColumnIndex() == 2) { sales.setLocation((int) cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print("cell 2 " + cell.getStringCellValue() + " \t"); break; } } sales.setReportRef(ReportRef); arrSales.add(sales); System.out.println(); } System.out.println("\t"); System.out.println("Size: " + arrSales.size()); System.out.println("\t"); for (int i = 0; i < arrSales.size(); i++) { System.out.println("Amount: " + arrSales.get(i).getSalesAmmount()); System.out.println("Created By: " + arrSales.get(i).getCreatedBy()); System.out.println("Location: " + arrSales.get(i).getLocation()); System.out.println("ReportRef: " + arrSales.get(i).getReportRef()); System.out.println("\t"); } request.setAttribute("arrSales", arrSales); SalesDB salesdb = new SalesDB(); request.setAttribute("arrSales", arrSales); for (int j = 0; j < arrSales.size(); j++) { salesdb.addSales(arrSales.get(j)); } ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher("/isrAfteruploadSales.jsp"); rd.forward(request, response); processRequest(request, response); }