List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook HSSFWorkbook
public HSSFWorkbook(InputStream s) throws IOException
From source file:cn.orignzmn.shopkepper.common.utils.excel.ImportExcel.java
License:Open Source License
/** * /*from w w w. j ava2 s . c o m*/ * @param path * @param headerNum ???=?+1 * @param sheetIndex ? * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { if (StringUtils.isBlank(fileName)) { throw new RuntimeException("!"); } else if (fileName.toLowerCase().endsWith("xls")) { this.wb = new HSSFWorkbook(is); } else if (fileName.toLowerCase().endsWith("xlsx")) { this.wb = new XSSFWorkbook(is); } else { throw new RuntimeException("??!"); } if (this.wb.getNumberOfSheets() < sheetIndex) { throw new RuntimeException("!"); } this.sheet = this.wb.getSheetAt(sheetIndex); this.headerNum = headerNum; log.debug("Initialize success."); }
From source file:cn.trymore.core.util.excel.PoiExcelParser.java
License:Open Source License
public void open(int sheetIdx) throws Exception { InputStream input = new FileInputStream(this.xlsFile); POIFSFileSystem poi = new POIFSFileSystem(input); this.book = new HSSFWorkbook(poi); this.sheet = getSheet(sheetIdx); this.cfms = this.sheet.getSheetConditionalFormatting(); }
From source file:cn.vlabs.umt.common.xls.UserXLSParser.java
License:Apache License
public UserXLSParser(InputStream in) throws XLSException { HSSFWorkbook workbook;/*from w ww. j ava2s . c o m*/ try { workbook = new HSSFWorkbook(in); sheet = workbook.getSheetAt(0); total = sheet.getLastRowNum(); } catch (IOException e) { throw new XLSException(e.getMessage()); } }
From source file:co.com.smartcode.bitcom.managedbeans.crud.utils.ExcelUtils.java
public static List<Producto> getProductosFromXls(byte[] bytes) { List<Producto> productos = new ArrayList<>(); try {//w w w. j av a 2s . c om HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes)); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Producto producto = new Producto(); Row row = rowIterator.next(); String nombre = row.getCell(0).getStringCellValue(); producto.setNombre(nombre); String referencia = getString(row, 1); producto.setReferencia(referencia); if (row.getCell(2) != null) { String nombreMarca = row.getCell(2).getStringCellValue(); if (nombreMarca != null && nombreMarca.trim().length() > 0) { Marca marca = new Marca(); marca.setNombre(nombreMarca); producto.setMarca(marca); } } if (row.getCell(3) != null) { String imagen = row.getCell(3).getStringCellValue(); producto.setImg(imagen); } BigDecimal precio = null; try { String precioString = getString(row, 4); precio = new BigDecimal(precioString); producto.setPrecio(precio); } catch (Exception e) { continue; } if (row.getCell(5) != null) { BigDecimal precioOferta = null; try { String precioString = getString(row, 5); precioOferta = new BigDecimal(precioString); producto.setPrecioOferta(precioOferta); } catch (Exception e) { } } if (producto.getPrecioOferta() == null) { producto.setPrecioOferta(new BigDecimal("0")); } if (row.getCell(6) != null) { String descripcion = row.getCell(6).getStringCellValue(); producto.setDescripcion(descripcion); } if (row.getCell(7) != null) { String especificaciones = row.getCell(7).getStringCellValue(); producto.setEspecificaciones(especificaciones); } if (row.getCell(8) != null) { String nombreSeccion = row.getCell(8).getStringCellValue(); if (nombreSeccion != null && nombreSeccion.trim().length() > 0) { String[] split = nombreSeccion.split("-"); if (split.length == 2) { Seccion seccion = new Seccion(); seccion.setNombre(split[0]); Subseccion subseccion = new Subseccion(); subseccion.setNombre(split[1]); subseccion.setSeccion(seccion); producto.setSubseccion(subseccion); } } } productos.add(producto); } } catch (Exception e) { e.printStackTrace(); } return productos; }
From source file:com.a9ski.jsf.exporter.DataTableExcelExporter.java
License:Apache License
protected HSSFWorkbook createHSSFWorkbook(final DataTableExporterOptions options) throws ExportException { final File templateFile = options.getTemplateFile(); final HSSFWorkbook wb; if (templateFile != null && templateFile.exists()) { try {/*www.j a v a2 s .c o m*/ final InputStream is = new FileInputStream(templateFile); try { wb = new HSSFWorkbook(is); is.close(); } finally { IOUtils.closeQuietly(is); } } catch (final IOException ex) { throw new ExportException(ex); } } else { wb = new HSSFWorkbook(); } return wb; }
From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.file.reader.ExcelReaderService.java
License:Open Source License
private Workbook openFileAsWorkBook(final MultipartFile file, final FileParserMessage msg) throws IOException { try {/*from w w w. ja va 2 s .c om*/ final String fileName = file.getOriginalFilename(); if (XLSX_FILE_EXTENSION.equals(fileName.substring(fileName.lastIndexOf(".")))) { return new XSSFWorkbook(file.getInputStream()); } if (XLS_FILE_EXTENSION.equals(fileName.substring(fileName.lastIndexOf(".")))) { return new HSSFWorkbook(file.getInputStream()); } throw new NotOfficeXmlFileException("Not recognized type"); } catch (NotOfficeXmlFileException e) { msg.addFileParseError(new FileParseError(0, "Invalid file type")); throw new IOException(); } }
From source file:com.abixen.platform.service.businessintelligence.multivisualisation.service.impl.parser.ExcelParserServiceImpl.java
License:Open Source License
private Workbook openFileAsWorkBook(MultipartFile file, FileParserMessage<DataFileColumn> msg) throws IOException { try {//from w w w . j a v a2 s .c o m String fileName = file.getOriginalFilename(); if (".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) { return new XSSFWorkbook(file.getInputStream()); } if (".xls".equals(fileName.substring(fileName.lastIndexOf(".")))) { return new HSSFWorkbook(file.getInputStream()); } throw new NotOfficeXmlFileException("Not recognized type"); } catch (NotOfficeXmlFileException e) { msg.addFileParseError(new FileParseError(0, "Invalid file type")); throw new IOException(); } }
From source file:com.accenture.control.GerenciaPlanilhaTS.java
public void geraNovaPlanilhaTS(String dir, List<TesteCaseTSBean> listTS) throws FileNotFoundException, IOException { FileInputStream arquivo = new FileInputStream(new File(dir)); HSSFWorkbook workbook = new HSSFWorkbook(arquivo); HSSFSheet sheetTS = workbook.getSheetAt(0); HSSFDataFormat format = workbook.createDataFormat(); HSSFCellStyle estilo = workbook.createCellStyle(); String formatData = "aaaa-mm-dd\"T12:00:00-03:00\""; int linha = 1; Row row = sheetTS.getRow(linha);//from w w w. j a va 2 s.c om Cell descriptionPlan = row.getCell(0); Cell prj = row.getCell(1); Cell fase = row.getCell(2); Cell testPhase = row.getCell(3); Cell testScriptName = row.getCell(4); Cell testScriptDescription = row.getCell(5); Cell stepNo = row.getCell(6); Cell stepDescription = row.getCell(7); Cell expectedResults = row.getCell(8); Cell product = row.getCell(9); Cell dataPlanejada = row.getCell(10); for (int i = 0; i < listTS.size(); i++) { estilo.setDataFormat(format.getFormat(formatData)); estilo.setFillBackgroundColor(HSSFColor.GREEN.index); row = sheetTS.getRow(linha); descriptionPlan = row.getCell(0); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); descriptionPlan.setCellValue(listTS.get(i).getTestPlan()); prj.setCellValue(listTS.get(i).getSTIPRJ()); fase.setCellValue(listTS.get(i).getFASE()); testPhase.setCellValue(listTS.get(i).getTestPhase()); testScriptName.setCellValue(listTS.get(i).getTestScriptName()); testScriptDescription.setCellValue(listTS.get(i).getTestScriptDescription()); stepNo.setCellValue(listTS.get(i).getSTEP_NUMERO()); stepDescription.setCellValue(listTS.get(i).getStepDescription()); expectedResults.setCellValue(listTS.get(i).getExpectedResults()); product.setCellValue(listTS.get(i).getProduct()); dataPlanejada.setCellValue(listTS.get(i).getDataPlanejada()); dataPlanejada.setCellStyle(estilo); linha = linha + 2; } FileOutputStream fileOut = new FileOutputStream(new File(dir)); workbook.write(fileOut); fileOut.close(); arquivo.close(); }
From source file:com.accounting.mbeans.SmsController.java
public void handleFileUpload(FileUploadEvent event) { // FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); // FacesContext.getCurrentInstance().addMessage(null, message); // System.out.println("entering into excel upload "); if (!PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId())) { event.setPhaseId(PhaseId.INVOKE_APPLICATION); event.queue();/* w w w. j av a 2s . c o m*/ } else { //do stuff here, #{ngoPhotoBean.description} is set System.out.println("smsMessage " + smsMessage); if (smsMessage.isEmpty() || smsMessage.length() < 5) { System.out.println("Please write sms message first."); HelperUtil.showErrorMessage("Please write sms message first."); } else { try { UploadedFile uploadedFile = (UploadedFile) event.getFile(); String filename = uploadedFile.getFileName(); // System.out.println("file name is " + filename); InputStream input = uploadedFile.getInputstream(); if (filename.endsWith("xlsx")) { XSSFWorkbook wb = new XSSFWorkbook(input); XSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); setupSmsInfoData(rows); } else if (filename.endsWith("xls")) { HSSFWorkbook wb = new HSSFWorkbook(input); HSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); setupSmsInfoData(rows); } } catch (IOException ex) { ex.printStackTrace(); } } checkBalance(); } }
From source file:com.adobe.ags.curly.controller.DataImporterController.java
License:Apache License
private void openLegacyExcel(File file) throws IOException { openWorkbook(new HSSFWorkbook(new FileInputStream(file))); }