List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem
public POIFSFileSystem(InputStream stream) throws IOException
From source file:Excel.LeerExcel.java
public void leerExcel1(String fileName) throws SQLException { tra = new ConeccionLocal(); List cellDataList = new ArrayList(); try {// ww w .j a va 2 s .c o m /** * Create a new instance for FileInputStream class */ FileInputStream fileInputStream = new FileInputStream(fileName); /** * Create a new instance for POIFSFileSystem class */ POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream); /* * Create a new instance for HSSFWorkBook Class */ HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem); HSSFSheet hssfSheet = workBook.getSheetAt(0); /** * Iterate the rows and cells of the spreadsheet * to get all the datas. */ Iterator rowIterator = hssfSheet.rowIterator(); while (rowIterator.hasNext()) { HSSFRow hssfRow = (HSSFRow) rowIterator.next(); Iterator iterator = hssfRow.cellIterator(); List cellTempList = new ArrayList(); while (iterator.hasNext()) { HSSFCell hssfCell = (HSSFCell) iterator.next(); cellTempList.add(hssfCell); } cellDataList.add(cellTempList); } } catch (Exception e) { e.printStackTrace(); } /** * Call the printToConsole method to print the cell data in the * console. */ printToConsole(cellDataList); }
From source file:failedtiposting.AnalyzePostings.java
private void analyzeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analyzeBtnActionPerformed // TODO add your handling code here: String casaPath = casaBrowserText.getText(); String failedPath = failedTrxnBrowserText.getText(); FileInputStream casaStream = null; FileInputStream failedStream = null; if (casaPath.equals("") || failedPath.equals("")) { outputArea.append("Please specify both files if you need me to run!"); return;/* w w w .ja v a 2s . c o m*/ } try { casaStream = new FileInputStream(casaPath); failedStream = new FileInputStream(failedPath); POIFSFileSystem casaPoiFs = new POIFSFileSystem(casaStream); POIFSFileSystem failedPoiFs = new POIFSFileSystem(failedStream); HSSFWorkbook casaWb = new HSSFWorkbook(casaPoiFs); HSSFWorkbook failedWb = new HSSFWorkbook(failedPoiFs); HSSFSheet casaSh = casaWb.getSheetAt(0); HSSFSheet failedSh = failedWb.getSheetAt(0); for (Row row : failedSh) { int lastCol = row.getLastCellNum(); for (int col = 0; col < lastCol; col++) { if (col == NARRATION_COLUMN) { Cell cell = row.getCell(col, Row.RETURN_BLANK_AS_NULL); if (cell != null) { String narr = cell.toString(); String found = getNarrationToken(narr); outputArea.append(narr + "\n"); outputArea.append("found = " + found + "\n\n\n"); } } } } } catch (IOException ex) { outputArea.append(ex.getMessage()); } finally { if (casaStream != null) { try { casaStream.close(); } catch (IOException ex) { outputArea.append(ex.getMessage()); } } if (failedStream != null) { try { failedStream.close(); } catch (IOException ex) { outputArea.append(ex.getMessage()); } } } }
From source file:FeatureExtraction.FeatureExtractorDocStreamPaths.java
@Override public Map ExtractFeaturesFrequencyFromSingleElement(T element) { Map<String, Integer> streamPaths = new HashMap<>(); String filePath = (String) element; try {//www. ja v a 2s . co m InputStream inputStream = new FileInputStream(filePath); POIFSFileSystem poiFileSystem = new POIFSFileSystem(inputStream); DirectoryNode directoryNode = poiFileSystem.getRoot(); //HWPFDocument document = new HWPFDocument(directoryNode); GetStreamsPaths(directoryNode, "", streamPaths); } catch (FileNotFoundException ex) { Console.PrintException(String.format("Error extracting DOC features from file: %s", filePath), ex); } catch (IOException ex) { Console.PrintException(String.format("Error extracting DOC features from file: %s", filePath), ex); } return streamPaths; }
From source file:File.DOC.WriteDoc.java
/** * @param args the command line arguments *///from w ww. j a v a 2s.c o m public void Write(String path, String namafile, String content) { File file = new File("D:\\xyz.doc"); try { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); HWPFDocument doc = new HWPFDocument(fs); Range range = doc.getRange(); CharacterRun run = range.insertBefore(content.replace("\n", "\013")); run.setBold(true); OutputStream outa = new FileOutputStream(new File(path + namafile + ".doc")); doc.write(outa); out.close(); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:file.open.util.parse.XlsParser.java
License:Open Source License
/** * Reads each line of the csv file into a StringTokenizer and returns * the data as a two-dimensional Iterator object. * * @param data/* w w w . j a va2s . c om*/ * BufferedReader containing the file data. * @return Iterator[], the file data. */ @Override public Iterator readLinesCollection(String path) { HSSFWorkbook workBook = null; File file = new File(path); InputStream excelDocumentStream = null; try { excelDocumentStream = new FileInputStream(file); POIFSFileSystem fsPOI = new POIFSFileSystem(new BufferedInputStream(excelDocumentStream)); workBook = new HSSFWorkbook(fsPOI); initParser(workBook.getSheetAt(0)); String[] res; int count = 0; while ((res = this.splitLine()) != null) { //System.out.println("\t"); for (int i = 0; i < res.length; i++) { //System.out.print(res[i] + ","); list.Add(res[i], count); } count++; } excelDocumentStream.close(); } catch (Exception e) { e.printStackTrace(); } return createIteratorCollection(); }
From source file:fr.ens.transcriptome.aozan.io.CasavaDesignXLSReader.java
License:Open Source License
@Override public CasavaDesign read() throws IOException { // create a POIFSFileSystem object to read the data final POIFSFileSystem fs = new POIFSFileSystem(this.is); // Create a workbook out of the input stream final HSSFWorkbook wb = new HSSFWorkbook(fs); // Get a reference to the worksheet final HSSFSheet sheet = wb.getSheetAt(0); // When we have a sheet object in hand we can iterator on // each sheet's rows and on each row's cells. final Iterator<Row> rows = sheet.rowIterator(); final List<String> fields = new ArrayList<>(); while (rows.hasNext()) { final HSSFRow row = (HSSFRow) rows.next(); final Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()) { final HSSFCell cell = (HSSFCell) cells.next(); while (fields.size() != cell.getColumnIndex()) { fields.add(""); }/*w ww .j a va2s . c o m*/ // Convert cell value to String fields.add(parseCell(cell)); } // Parse the fields if (!isFieldsEmpty(fields)) { parseLine(fields); } fields.clear(); } this.is.close(); return getDesign(); }
From source file:fr.univrouen.poste.services.ExcelParser.java
License:Apache License
public List<List<String>> getCells(InputStream xslFileInput) { List<List<String>> cellVectorHolder = new Vector<List<String>>(); try {//from ww w .j a v a 2s. co m POIFSFileSystem fileSystem = new POIFSFileSystem(xslFileInput); HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); HSSFSheet sheet = workBook.getSheetAt(0); Iterator<Row> rowIter = sheet.rowIterator(); while (rowIter.hasNext()) { HSSFRow myRow = (HSSFRow) rowIter.next(); List<String> cellStoreVector = new Vector<String>(); // take care of blank cell ! // @see http://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache-poi int max = myRow.getLastCellNum(); for (int i = 0; i < max; i++) { HSSFCell myCell = (HSSFCell) myRow.getCell(i, Row.CREATE_NULL_AS_BLANK); if (Cell.CELL_TYPE_STRING == myCell.getCellType()) cellStoreVector.add(myCell.getStringCellValue()); else if ((Cell.CELL_TYPE_NUMERIC == myCell.getCellType())) cellStoreVector.add(Long.toString(new Double(myCell.getNumericCellValue()).longValue())); else if ((Cell.CELL_TYPE_BLANK == myCell.getCellType())) cellStoreVector.add(""); else { logger.debug("This cell is not numeric or string ... : " + myCell + " \n ... cellType : " + myCell.getCellType()); cellStoreVector.add(""); } } cellVectorHolder.add(cellStoreVector); } } catch (Exception e) { logger.error("Error during parsing the XSL File", e); throw new RuntimeException("Error during parsing the XSL File", e); } return cellVectorHolder; }
From source file:gatebass.utils.exel.POIExcelReader.java
/** * 41 This method is used to display the Excel content to command line. 42 * * * @param xlsPath//www. j a v a2 s . co m */ @SuppressWarnings("unchecked") public void displayFromExcel(String xlsPath) { // end_row = 2242; InputStream inputStream = null; try { inputStream = new FileInputStream(xlsPath); } catch (FileNotFoundException e) { System.out.println("File not found in the specified path."); e.printStackTrace(); } POIFSFileSystem fileSystem = null; int dd = 0; try { fileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); HSSFSheet sheet = workBook.getSheetAt(0); Iterator rows = sheet.rowIterator(); List<Individuals> individualses = new ArrayList<>(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); // if (row.getRowNum() >= end_row) { // break; // } if (row.getRowNum() <= start_row) { continue; } dd = row.getRowNum(); // if (row.getRowNum() == 0 // || row.getRowNum() < 195 || row.getRowNum() > 250 // ) { // continue; // } Individuals individuals = null; // display row number in the console. // System.out.println("Row No.: " + row.getRowNum()); // once get a row its time to iterate through cells. Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); // System.out.println("Cell No.: " + cell.getCellNum()); /* * Now we will get the cell type and display the values * accordingly. */ switch (cell.getCellNum()) { case 0: individuals = new Individuals(); // individuals = new Individuals(Integer.parseInt(cell.getRichStringCellValue().getString())); break; case 1: try { individuals.setCard_id(((long) cell.getNumericCellValue()) + ""); } catch (Exception e) { } try { individuals.setCard_id(cell.getRichStringCellValue().getString()); } catch (Exception e) { } break; // case 2: // if (!cell.getRichStringCellValue().getString().isEmpty()) { // } // break; case 3: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setFirst_name(cell.getRichStringCellValue().getString()); } break; case 4: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setLast_name(cell.getRichStringCellValue().getString()); } break; case 6: try { individuals.setNational_id(((long) cell.getNumericCellValue()) + ""); } catch (Exception e) { } try { individuals.setNational_id(cell.getRichStringCellValue().getString()); } catch (Exception e) { } break; case 10: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setFirst_name_ENG(cell.getRichStringCellValue().getString()); } break; case 16: try { individuals.setPostal_code(((long) cell.getNumericCellValue()) + ""); } catch (Exception e) { } try { individuals.setPostal_code(cell.getRichStringCellValue().getString()); } catch (Exception e) { } break; case 17: try { individuals.setId_number(((long) cell.getNumericCellValue()) + ""); } catch (Exception e) { } try { individuals.setId_number(cell.getRichStringCellValue().getString()); } catch (Exception e) { } break; case 18: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setBirth_day(databaseHelper.historyDao.getFirst("date", cell.getRichStringCellValue().getString().substring(2))); } break; case 19: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setFather_first_name(cell.getRichStringCellValue().getString()); } break; case 20: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setBirth_state(cell.getRichStringCellValue().getString()); } break; case 21: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setIssued(cell.getRichStringCellValue().getString()); } break; case 22: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setStreet_address(cell.getRichStringCellValue().getString()); } break; case 23: String ss = cell.getRichStringCellValue().getString(); individuals.setVeteran_status(BEDONE_KART); if (!ss.isEmpty()) { if (ss.contains("?")) { individuals.setVeteran_status(MOAF); } else if (ss.contains("")) { individuals.setVeteran_status(PAYAN_KHEDMAT); } } break; case 25: try { individuals.setMobile(((long) cell.getNumericCellValue()) + ""); } catch (Exception e) { } try { individuals.setMobile(cell.getRichStringCellValue().getString()); } catch (Exception e) { } break; case 26: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setAcademic_degree(cell.getRichStringCellValue().getString()); } break; case 27: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setField_of_study(cell.getRichStringCellValue().getString()); } break; case 28: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setReligion(cell.getRichStringCellValue().getString()); } break; case 34: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setHave_soe_pishine(true); } break; case 35: if (!cell.getRichStringCellValue().getString().isEmpty()) { individuals.setComments(cell.getRichStringCellValue().getString()); } break; } switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: { // cell type numeric. // System.out.println("Numeric value: " + cell.getNumericCellValue()); break; } case HSSFCell.CELL_TYPE_STRING: { // cell type string. HSSFRichTextString richTextString = cell.getRichStringCellValue(); // System.out.println("String value: " + richTextString.getString()); break; } default: { // types other than String and Numeric. // System.out.println("Type not supported."); break; } } } String split = FileSystems.getDefault().getSeparator(); individuals.setFilesPatch( "data" + split + "1394" + split + dd / 50 + split + individuals.getNational_id() + split); File imageFile = new File( "d://test//Images-Personal-Gatepass//" + individuals.getCard_id() + ".jpg"); if (imageFile.exists()) { individuals.setPicture_address(individuals.getNational_id() + "-pic"); copyImageFile(imageFile.getAbsolutePath(), server + individuals.getFilesPatch(), individuals.getPicture_address()); individuals.setPicture_address( individuals.getPicture_address() + getFileExtension(imageFile.getAbsolutePath())); } individualses.add(individuals); // databaseHelper.individualsDao.createOrUpdate(individuals, dd); } databaseHelper.individualsDao.insertList(individualses); } catch (Exception e) { e.printStackTrace(); } }
From source file:gatebass.utils.exel.POIExcelReader.java
@SuppressWarnings("unchecked") public void compnaiesFromExcel(String xlsPath) { InputStream inputStream = null; try {/*from www . j a v a 2s . co m*/ inputStream = new FileInputStream(xlsPath); } catch (FileNotFoundException e) { System.out.println("File not found in the specified path."); e.printStackTrace(); } POIFSFileSystem fileSystem = null; try { fileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); HSSFSheet sheet = workBook.getSheetAt(0); Iterator rows = sheet.rowIterator(); boolean check = false; while (rows.hasNext()) { check = false; HSSFRow row = (HSSFRow) rows.next(); // if (row.getRowNum() >= end_row) { // break; // } if (row.getRowNum() <= start_row) { continue; } Companies companies = new Companies(); // System.out.println("Row No.: " + row.getRowNum()); String companyName = row.getCell(2).getRichStringCellValue().getString(); if (!companyName.isEmpty()) { Companies companiesTEMP = databaseHelper.companiesDao.getFirst("company_fa", companyName); if (companiesTEMP == null) { check = true; companies.setCompany_fa(companyName); companies.setActive(true); } } else { continue; } try { String companyNameEn = row.getCell(9).getRichStringCellValue().getString(); if (!companyNameEn.isEmpty()) { Companies companiesTEMP = databaseHelper.companiesDao.getFirst("company_en", companyNameEn); if (companiesTEMP == null) { companies.setCompany_en(companyNameEn); } } } catch (Exception e) { } if (check) { // companieses.add(companies); Manage mm = databaseHelper.manageDao.getFirst("key", "company_folder_count"); int jj = Integer.parseInt(mm.getValue()); companies.setFolder_name("C" + jj); databaseHelper.companiesDao.createOrUpdate(companies); ++jj; mm.setValue(jj + ""); databaseHelper.manageDao.createOrUpdate(mm); } } // databaseHelper.companiesDao.insertList(companieses); } catch (Exception e) { Logger.getLogger(POIExcelReader.class.getName()).log(Level.SEVERE, e.getMessage(), e); } }
From source file:gatebass.utils.exel.POIExcelReader.java
@SuppressWarnings("unchecked") public void historyFromExcel(String xlsPath) { InputStream inputStream = null; try {// w w w. ja v a 2s.c o m inputStream = new FileInputStream(xlsPath); } catch (FileNotFoundException e) { System.out.println("File not found in the specified path."); e.printStackTrace(); } POIFSFileSystem fileSystem = null; try { fileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); HSSFSheet sheet = workBook.getSheetAt(0); Iterator rows = sheet.rowIterator(); boolean check; List<History> historys = new ArrayList<>(); while (rows.hasNext()) { check = false; HSSFRow row = (HSSFRow) rows.next(); if (row.getRowNum() <= start_row) { continue; } History historyH = null; // System.out.println("Row No.: " + row.getRowNum()); String history = ""; try { history = row.getCell(7).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(18).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(24).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(29).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(30).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(31).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(32).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } check = false; historyH = null; history = ""; try { history = row.getCell(33).getRichStringCellValue().getString(); if (!history.isEmpty()) { history = history.substring(2); History HistoryTEMP = databaseHelper.historyDao.getFirst("date", history); if (HistoryTEMP == null) { check = true; historyH = new History(history.substring(0, history.indexOf("/")), history.substring(history.indexOf("/") + 1, history.lastIndexOf("/")), history.substring(history.lastIndexOf("/") + 1)); } } if (check) { historys.add(historyH); // databaseHelper.historyDao.createOrUpdate(historyH); } } catch (Exception e) { } } databaseHelper.historyDao.insertList(historys); } catch (Exception e) { e.printStackTrace(); } }