List of usage examples for org.apache.poi.ss.usermodel WorkbookFactory create
public static Workbook create(File file) throws IOException, EncryptedDocumentException
From source file:io.konig.spreadsheet.WorkbookLoaderTest.java
License:Apache License
@Ignore public void testDecimalDataType() throws Exception { InputStream input = getClass().getClassLoader().getResourceAsStream("decimal-datatype.xlsx"); Workbook book = WorkbookFactory.create(input); Graph graph = new MemoryGraph(); NamespaceManager nsManager = new MemoryNamespaceManager(); graph.setNamespaceManager(nsManager); WorkbookLoader loader = new WorkbookLoader(nsManager); loader.setFailOnErrors(true);/*from ww w . j a v a 2 s .c om*/ File file = new File("src/test/resources/decimal-datatype.xlsx"); thrown.expect(RuntimeException.class); thrown.expectCause(CoreMatchers.isA(SpreadsheetException.class)); thrown.expectMessage( "Property alias:ISDELETED is missing required decimal scale on row 17 in workbook decimal-datatype.xlsx"); loader.load(book, graph, file); }
From source file:io.konig.spreadsheet.WorkbookLoaderTest.java
License:Apache License
public void testRelationshipDegree() throws Exception { InputStream input = getClass().getClassLoader().getResourceAsStream("relationship-degree.xlsx"); Workbook book = WorkbookFactory.create(input); Graph graph = new MemoryGraph(); NamespaceManager nsManager = new MemoryNamespaceManager(); graph.setNamespaceManager(nsManager); WorkbookLoader loader = new WorkbookLoader(nsManager); loader.load(book, graph);/* w w w . java2 s . c o m*/ input.close(); Vertex genderTypeClass = graph.getVertex(uri("http://schema.org/GenderType")); assertTrue(genderTypeClass != null); Vertex subClassOf = genderTypeClass.getVertex(RDFS.SUBCLASSOF); assertTrue(OWL.RESTRICTION.equals(subClassOf.getURI(RDF.TYPE))); assertTrue(uri("http://example.com/ns/demo/genderCode").equals(subClassOf.getURI(OWL.ONPROPERTY))); assertTrue(Konig.OneToOne.equals(subClassOf.getURI(Konig.relationshipDegree))); Vertex imageObjectClass = graph.getVertex(uri("http://schema.org/ImageObject")); assertTrue(imageObjectClass != null); List<Vertex> subClassOfList = imageObjectClass.asTraversal().out(RDFS.SUBCLASSOF).toVertexList(); assertTrue(OWL.RESTRICTION.equals(subClassOfList.get(1).getURI(RDF.TYPE))); assertTrue(uri("http://schema.org/thumbnail").equals(subClassOfList.get(1).getURI(OWL.ONPROPERTY))); assertTrue(Konig.OneToMany.equals(subClassOfList.get(1).getURI(Konig.relationshipDegree))); Vertex videoObjectClass = graph.getVertex(uri("http://schema.org/VideoObject")); assertTrue(videoObjectClass != null); subClassOfList = videoObjectClass.asTraversal().out(RDFS.SUBCLASSOF).toVertexList(); assertTrue(OWL.RESTRICTION.equals(subClassOfList.get(1).getURI(RDF.TYPE))); assertTrue(uri("http://schema.org/thumbnail").equals(subClassOfList.get(1).getURI(OWL.ONPROPERTY))); assertTrue(Konig.OneToMany.equals(subClassOfList.get(1).getURI(Konig.relationshipDegree))); }
From source file:io.konig.spreadsheet.WorkbookProcessorImpl.java
License:Apache License
private Workbook createWorkbook(File workbookFile) throws SpreadsheetException { try {//w ww . ja va2s . c om return WorkbookFactory.create(workbookFile); } catch (Throwable e) { throw new SpreadsheetException("Failed to create workbook: " + workbookFile.getName(), e); } }
From source file:IO.REDACCIONES.java
public static String read(String id) throws FileNotFoundException, IOException { String salida = ""; try {/* ww w.j ava 2 s. c o m*/ Workbook wb = WorkbookFactory.create(new FileInputStream(new File(FILES.ruta))); Sheet hoja = wb.getSheetAt(1); Iterator rows = hoja.rowIterator(); while (rows.hasNext()) { Row row = (Row) rows.next(); if (row.getCell(0).getStringCellValue().equalsIgnoreCase(id)) { salida = row.getCell(1).getStringCellValue(); break; } } } catch (IOException | InvalidFormatException | EncryptedDocumentException ex) { salida = ""; Logger.getLogger(FILES.class.getName()).log(Level.SEVERE, null, ex); } return salida; }
From source file:it.dontesta.liferay.example.portal.client.ImportUsersToLiferay.java
License:Open Source License
/** * Read users to import from excel file. * //from ww w . ja v a 2 s.c om * @return Returns a list of users who are ready for import. */ private static List<UserToImport> getUsersToImportFromExcel() { String fileName = (System.getProperty("fileToImport") != null) ? System.getProperty("fileToImport") : FILE_TO_IMPORT_USERS; InputStream inp = null; List<UserToImport> usersList = new ArrayList<UserToImport>(); UserToImport user = null; boolean readyForImport = true; try { inp = new FileInputStream(fileName); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { LOGGER.debug("Processing row index {}...", row.getRowNum()); if (row.getRowNum() == 0) { LOGGER.debug("First row is the header. Skip this row"); continue; } else { user = new UserToImport(); } for (Cell cell : row) { LOGGER.debug("Processing cell index {}...", cell.getColumnIndex()); switch (cell.getColumnIndex()) { case 0: if (!cell.getStringCellValue().isEmpty()) { user.setTitle(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 1: if (!cell.getStringCellValue().isEmpty()) { user.setScreenName(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } else { LOGGER.warn("The username attribute is not null. Row skipped"); LOGGER.warn("Processing cell index {}...[FAILED]", cell.getColumnIndex()); break; } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 2: if (!cell.getStringCellValue().isEmpty()) { user.setEmail(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } else { LOGGER.warn("The email attribute is not null. Row skipped"); LOGGER.warn("Processing cell index {}...[FAILED]", cell.getColumnIndex()); break; } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 3: if (!cell.getStringCellValue().isEmpty()) { user.setFirstName(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 4: if (!cell.getStringCellValue().isEmpty()) { user.setMiddleName(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 5: if (!cell.getStringCellValue().isEmpty()) { user.setLastName(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 6: if (!cell.getStringCellValue().isEmpty()) { user.setGender(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 7: if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { if (DateUtil.isCellDateFormatted(cell)) { Calendar calBirthDate = Calendar.getInstance(); calBirthDate.setTime(cell.getDateCellValue()); user.setBirthDate(calBirthDate); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getDateCellValue()); } else { LOGGER.warn("Value cell index {} {}", cell.getColumnIndex(), cell.getNumericCellValue()); LOGGER.warn("Value cell index {} not contain a date type format", cell.getColumnIndex()); } } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 8: if (!cell.getStringCellValue().isEmpty()) { user.setJobTitle(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } break; case 9: if (!cell.getStringCellValue().isEmpty()) { user.setSiteName(cell.getStringCellValue().split(",")); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), Arrays.toString(cell.getStringCellValue().split(","))); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 10: if (!cell.getStringCellValue().isEmpty()) { user.setRoleName(cell.getStringCellValue().split(",")); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), Arrays.toString(cell.getStringCellValue().split(","))); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 11: if (!cell.getStringCellValue().isEmpty()) { user.setLanguageId(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 12: if (!cell.getStringCellValue().isEmpty()) { user.setTimeZoneId(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; case 13: if (!cell.getStringCellValue().isEmpty()) { user.setAccountId(cell.getStringCellValue()); LOGGER.debug("Value cell index {} is {}", cell.getColumnIndex(), cell.getStringCellValue()); } LOGGER.debug("Processing cell index {}...[OK]", cell.getColumnIndex()); break; default: break; } } if (user.getScreenName() == null) { LOGGER.warn("The username attribute can not be null for rowId {}", row.getRowNum()); readyForImport = false; } if (user.getEmail() == null) { LOGGER.warn("The email attribute can not be null for rowId {}", row.getRowNum()); readyForImport = false; } if (readyForImport) { LOGGER.info("Add user object {} to user list...", user.toString()); usersList.add(user); } } } catch (FileNotFoundException e) { LOGGER.error(e.getMessage()); } catch (IllegalStateException e) { LOGGER.error(e.getMessage()); } catch (InvalidFormatException e) { LOGGER.error(e.getMessage()); } catch (IOException e) { LOGGER.error(e.getMessage()); } return usersList; }
From source file:it.greenvulcano.excel.reader.BaseReader.java
License:Open Source License
public void processExcel(InputStream in) throws ExcelException, InterruptedException { cleanUp();/* w w w .ja v a 2 s. c om*/ Workbook workbook = null; try { // Open the workbook and then create the FormulaEvaluator and // DataFormatter instances that will be needed to, respectively, // force evaluation of formula found in cells and create a // formatted String encapsulating the cells contents. workbook = WorkbookFactory.create(in); evaluator = workbook.getCreationHelper().createFormulaEvaluator(); formatter = new DataFormatter(true); processExcel(workbook); } catch (ExcelException exc) { throw exc; } catch (Exception exc) { ThreadUtils.checkInterrupted(exc); throw new ExcelException("Error parsing WorkBook", exc); } finally { workbook = null; formatter = null; evaluator = null; } }
From source file:itpreneurs.itp.report.archive.CellStyleDetails.java
License:Apache License
public static void main(String[] args) throws Exception { // if(args.length == 0) { // throw new IllegalArgumentException("Filename must be given"); // }/*from w w w. ja v a 2 s. co m*/ String filename = "/Users/vincentgong/Documents/workspaces/Resource/itpreneurs/report/Workbook1.xlsx"; Workbook wb = WorkbookFactory.create(new File(filename)); DataFormatter formatter = new DataFormatter(); for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) { Sheet sheet = wb.getSheetAt(sn); System.out.println("Sheet #" + sn + " : " + sheet.getSheetName()); for (Row row : sheet) { System.out.println(" Row " + row.getRowNum()); for (Cell cell : row) { CellReference ref = new CellReference(cell); System.out.print(" " + ref.formatAsString()); System.out.print(" (" + cell.getColumnIndex() + ") "); CellStyle style = cell.getCellStyle(); System.out.print("Format=" + style.getDataFormatString() + " "); System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " "); System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " "); Font font = wb.getFontAt(style.getFontIndex()); System.out.print("Font=" + font.getFontName() + " "); System.out.print("FontColor="); if (font instanceof HSSFFont) { System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb))); } if (font instanceof XSSFFont) { System.out.print(renderColor(((XSSFFont) font).getXSSFColor())); } System.out.println(); System.out.println(" " + formatter.formatCellValue(cell)); } } System.out.println(); } }
From source file:jacobi.test.util.JacobiJUnit4ClassRunner.java
License:Open Source License
public JacobiJUnit4ClassRunner(Class<?> clazz) throws InitializationError { super(clazz); if (!clazz.isAnnotationPresent(JacobiImport.class)) { return;/*from w w w. j av a 2 s.co m*/ } String path = clazz.getAnnotation(JacobiImport.class).value(); try (InputStream in = clazz.getResourceAsStream(path)) { this.dataSource = new JacobiDataSource(WorkbookFactory.create(in)); } catch (IOException | InvalidFormatException ex) { throw new InitializationError(ex); } this.injects = new ArrayList<>(); this.results = new TreeMap<>(); this.detectInjection(clazz); System.out.println("injected " + this.injects); }
From source file:jasco.Jasco.java
private void exec(String xlsFile, String sheetName, int x, int y, int width, int height) throws FileNotFoundException { InputStream in = new FileInputStream(xlsFile); Workbook wb;//from www. j a va2 s .c om try { wb = WorkbookFactory.create(in); Sheet sheet1 = wb.getSheet(sheetName); List<List<String>> rows = convertSheetToArrayList(y, height, x, width, sheet1); String encode = JSON.encode(rows); //TODO output File System.out.println(encode); } catch (IOException | InvalidFormatException ex) { Logger.getLogger(Jasco.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:jp.co.orangeright.crossheadofficesample2.jsf.ItemFileInterfaceContoroller.java
public String createTodenHomeItemExcel() { int count = 0; try {/*from ww w. j a v a 2 s. co m*/ File excelFile = this.getFile("todenhome" + this.dataFile.getSubmittedFileName()); Workbook workbook = WorkbookFactory.create(excelFile); Sheet sheet = workbook.getSheet("Sheet1"); for (int rowNumber = sheet.getFirstRowNum() + 1; rowNumber <= sheet.getLastRowNum(); rowNumber++) { String itemCd = this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(0)); ItemSearchCondition itemCondition = new ItemSearchCondition(); itemCondition.setItemcd(itemCd); List<Item> itemList = this.itemEjb.findAll(itemCondition); if (itemList.size() > 0) { //??????? } else { this.itemController.prepareCreate(); this.itemController.getSelected().setItemcd(itemCd); this.itemController.getSelected().setCustomerid(this.customerEjb.find(31925)); this.itemController.getSelected().setUserid(this.userEjb.find("mitanto")); StringBuilder detail = new StringBuilder(); detail.append("/****** ????? ******/"); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(0))); detail.append(System.lineSeparator()); detail.append("?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(1))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(13))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(14))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(15))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(16))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("??: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(17))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?_: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(18))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("?_: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(19))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(20))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("WiFi: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(21))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("IoTNo: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(57))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("ID: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(58))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(59))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(60))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("Notion: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(61))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("Notion: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(62))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(79))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("???_: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(80))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(81))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("???_: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(82))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(83))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(84))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(85))); detail.append("/"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(86))); detail.append("/"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(87))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("_?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(107))); detail.append(System.lineSeparator()); detail.append("_?: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(108))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(109))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(110))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(111))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(112))); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(113))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("_: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(114))); detail.append("-"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(115))); detail.append("-"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(116))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append("__??: "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(117))); detail.append("-"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(118))); detail.append("-"); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(119))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); detail.append(": "); detail.append(this.getTodenExcelCellValue(sheet.getRow(rowNumber).getCell(120))); detail.append(System.lineSeparator()); detail.append(System.lineSeparator()); this.itemController.getSelected().setDetail(detail.toString()); this.itemController.getSelected().setMemo(""); this.itemController.create(); count++; } } excelFile.delete(); JsfUtil.addSuccessMessage(count + "????"); } catch (Exception e) { return null; } return null; }