List of usage examples for org.apache.poi.openxml4j.opc OPCPackage open
public static OPCPackage open(InputStream in) throws InvalidFormatException, IOException
From source file:steffen.haertlein.file.FileObject.java
License:Apache License
private void readWordDocument() { try {/*w w w . ja v a 2 s . c om*/ FileInputStream fs = new FileInputStream(f); XWPFDocument document; document = new XWPFDocument(OPCPackage.open(fs)); XWPFWordExtractor docxReader = new XWPFWordExtractor(document); String text = docxReader.getText(); docxReader.close(); String[] docxLines = text.split("\n"); for (String line : docxLines) { lines.add(line); } fs.close(); } catch (InvalidFormatException e) { JOptionPane.showMessageDialog(null, "InvalidFormatException in readWordDocument", "Fehler", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "FileNotFoundException in readWordDocument", "Fehler", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (IOException e) { JOptionPane.showMessageDialog(null, "IOException in readWordDocument", "Fehler", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } }
From source file:tan.jam.jsf.OrignalFileDownloadBean.java
public void downloadHandler(ActionEvent e) { System.out.println("download click handler"); try {/*from w w w. j a va2 s. com*/ InputStream stream = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext()).getResourceAsStream("/resources/demo/file/Table.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(stream)); XSSFSheet sheet = wb.getSheetAt(0); wb.setForceFormulaRecalculation(true); /* */ String name; String acc; HashSet OnlineTransferToAccounts = new HashSet(); int RowForOnlineTransferT0 = 105; int RowForRecurringDailyPayment = 105; List<String> LenderDescription = new ArrayList<>(); List<String> LenderDate = new ArrayList<>(); List<Double> LenderAmount = new ArrayList<>(); List<Double> UniqueDailypaymentkeywordAmount = new ArrayList<>(); List<String> UniqueDailypaymentkeywordDate = new ArrayList<>(); List<String> UniqueDailypaymentkeywordDescription = new ArrayList<>(); for (int a = 0; a < companies.size(); a++) { Company c = companies.get(a); name = c.getCompanyName(); acc = c.getAccountNumber(); String month = c.getDate().toLowerCase(); int R = 3; if (month.startsWith(("jan"))) { R = 3; } else if (month.startsWith("feb")) { R = 4; } else if (month.startsWith("mar")) { R = 5; } else if (month.startsWith("apr")) { R = 6; } else if (month.startsWith("may")) { R = 7; } else if (month.startsWith("jun")) { R = 8; } else if (month.startsWith("jul")) { R = 9; } else if (month.startsWith("aug")) { R = 10; } else if (month.startsWith("sep")) { R = 11; } else if (month.startsWith("oct")) { R = 12; } else if (month.startsWith("nov")) { R = 13; } else if (month.startsWith("dec")) { R = 14; } else if (month.startsWith("02") | month.startsWith("2")) { R = 4; } else if (month.startsWith("03") | month.startsWith("3")) { R = 5; } else if (month.startsWith("04") | month.startsWith("4")) { R = 6; } else if (month.startsWith("05") | month.startsWith("5")) { R = 7; } else if (month.startsWith("06") | month.startsWith("6")) { R = 8; } else if (month.startsWith("07") | month.startsWith("7")) { R = 9; } else if (month.startsWith("08") | month.startsWith("8")) { R = 10; } else if (month.startsWith("09") | month.startsWith("9")) { R = 11; } else if (month.startsWith("10") | month.startsWith("10")) { R = 12; } else if (month.toUpperCase().startsWith("11")) { R = 13; } else if (month.toUpperCase().startsWith("12")) { R = 14; } ////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//// sheet.getRow(1).getCell(1).setCellValue("Bank Account**" + acc); if (a == 0) { sheet.getRow(1).getCell(7).setCellValue(name + "**" + acc); sheet.getRow(1).getCell(12).setCellValue(name + "**" + acc); sheet.getRow(R).getCell(7) .setCellFormula("sum(" + c.getTotalDeposits() + "-" + c.getSumOfReversalFrom() + "-" + c.getSumOfOnlineTransferFrom() + "-" + c.getKeywordSum() + ")"); // int Mov=Shifting.FindShift(wb, sheet); sheet.getRow(R).getCell(12).setCellValue(c.getAverageLedgerBalance()); sheet.getRow(R).getCell(13).setCellValue(c.getMinValue()); sheet.getRow(R).getCell(14).setCellValue(c.getNoOfNegativeValues()); // int Onl=Shifting.Online(wb, sheet); } else { int Decision = 0; String Name = name + "**" + acc; for (int s = 0; s < Shifting.FindShift(wb, sheet) + 1; s++) { if (sheet.getRow(1).getCell(7 + s).getStringCellValue().equals(Name)) { Decision = 1; sheet.getRow(R).getCell(7 + s) .setCellFormula("sum(" + c.getTotalDeposits() + "-" + c.getSumOfReversalFrom() + "-" + c.getSumOfOnlineTransferFrom() + "-" + c.getKeywordSum() + ")"); } } if (Decision == 0) { int Mov = Shifting.FindShift(wb, sheet); Shifting.InsertColumn(wb, sheet); sheet.getRow(1).getCell(8 + Mov).setCellValue(name + "**" + acc); sheet.getRow(R).getCell(8 + Mov) .setCellFormula("sum(" + c.getTotalDeposits() + "-" + c.getSumOfReversalFrom() + "-" + c.getSumOfOnlineTransferFrom() + "-" + c.getKeywordSum() + ")"); } int selectcolumn = Shifting.FindShift(wb, sheet) + 12; if (sheet.getRow(1).getCell(selectcolumn).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn).getStringCellValue().equals(Name)) { } else if (sheet.getRow(1).getCell(selectcolumn + 6).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 6).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 6).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 6; } else if (sheet.getRow(1).getCell(selectcolumn + 12).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 12).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 12).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 12; } else if (sheet.getRow(1).getCell(selectcolumn + 18).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 18).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 18).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 18; } else if (sheet.getRow(1).getCell(selectcolumn + 24).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 24).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 24).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 24; } else if (sheet.getRow(1).getCell(selectcolumn + 30).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 30).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 30).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 30; } else if (sheet.getRow(1).getCell(selectcolumn + 36).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 36).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 36).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 36; } else if (sheet.getRow(1).getCell(selectcolumn + 42).getCellType() == 3 || sheet.getRow(1).getCell(selectcolumn + 42).getStringCellValue().isEmpty() || sheet.getRow(1).getCell(selectcolumn + 42).getStringCellValue().equals(Name)) { selectcolumn = selectcolumn + 42; } sheet.getRow(1).getCell(selectcolumn).setCellValue(name + "**" + acc); sheet.getRow(R).getCell(selectcolumn).setCellValue(c.getAverageLedgerBalance()); sheet.getRow(R).getCell(selectcolumn + 1).setCellValue(c.getMinValue()); sheet.getRow(R).getCell(selectcolumn + 2).setCellValue(c.getNoOfNegativeValues()); } /* // int Mov=Shifting.FindShift(wb, sheet); sheet.getRow(R).getCell(12).setCellValue(c.getAverageLedgerBalance()); sheet.getRow(R).getCell(13).setCellValue(c.getMinValue()); sheet.getRow(R).getCell(14).setCellValue(c.getNoOfNegativeValues()); // int Onl=Shifting.Online(wb, sheet); */ ///%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%///// for (int b = 0; b < c.getOnlineTransferToDate().size(); b++) { sheet.getRow(RowForOnlineTransferT0).getCell(9) .setCellValue(c.getOnlineTransferToDate().get(b)); sheet.getRow(RowForOnlineTransferT0).getCell(10) .setCellValue(c.getOnlineTransferToDescription().get(b)); sheet.getRow(RowForOnlineTransferT0).getCell(12) .setCellValue(c.getOnlineTransferToAmount().get(b)); RowForOnlineTransferT0++; } for (int L = 0; L < c.getKeywordAmount().size(); L++) { int index = 0; if (LenderDescription.contains(c.getKeywordDescription().get(L))) { index = LenderDescription.indexOf(c.getKeywordDescription().get(L)); LenderDate.set(index, c.getKeywordDate().get(L)); LenderAmount.set(index, c.getKeywordAmount().get(L)); } else { LenderDescription.add(c.getKeywordDescription().get(L)); LenderDate.add(c.getKeywordDate().get(L)); LenderAmount.add(c.getKeywordAmount().get(L)); } } for (int TT = 0; TT < c.getOnlineTransferToCheckNoDuplicateRemoved().size(); TT++) { OnlineTransferToAccounts.add(c.getOnlineTransferToCheckNoDuplicateRemoved().get(TT)); } if (!name.equals("BlackHills")) { for (int U = 0; U < c.getDailypaymentKeywordDescription().size(); U++) { sheet.getRow(RowForRecurringDailyPayment).getCell(14) .setCellValue(c.getDailypaymentKeywordDate().get(U)); sheet.getRow(RowForRecurringDailyPayment).getCell(15) .setCellValue(c.getDailypaymentKeywordDescription().get(U)); sheet.getRow(RowForRecurringDailyPayment).getCell(17) .setCellValue(c.getDailypaymentKeywordAmount().get(U)); RowForRecurringDailyPayment++; if (!UniqueDailypaymentkeywordDescription .contains(c.getDailypaymentKeywordDescription().get(U))) { UniqueDailypaymentkeywordAmount.add(c.getDailypaymentKeywordAmount().get(U)); UniqueDailypaymentkeywordDate.add(c.getDailypaymentKeywordDate().get(U)); UniqueDailypaymentkeywordDescription.add(c.getDailypaymentKeywordDescription().get(U)); } } } } for (int LL = 0; LL < LenderDescription.size(); LL++) if (25 + LL < 31) { { // if(25+LL > 29) { //Shifting.InsertRow(wb, sheet,25+LL,26+LL); } sheet.getRow(LL + 25).getCell(1).setCellValue(LenderDate.get(LL)); sheet.getRow(LL + 25).getCell(2).setCellValue(LenderDescription.get(LL)); sheet.getRow(LL + 25).getCell(5).setCellValue(LenderAmount.get(LL)); } } List<String> OnlineTransferToUniqueAccounts = new ArrayList<>(OnlineTransferToAccounts); for (int TT = 0; TT < OnlineTransferToUniqueAccounts.size(); TT++) { int Mov = Shifting.FindShift(wb, sheet); Shifting.InsertColumn(wb, sheet); sheet.getRow(1).getCell(8 + Mov).setCellValue("**" + OnlineTransferToUniqueAccounts.get(TT)); } //************************** Daily Payment portion ******************************************// for (int D = 0; D < UniqueDailypaymentkeywordDescription.size(); D++) { for (int r = 25; r < 31; r++) { if (sheet.getRow(r).getCell(2).getCellType() == 3) { sheet.getRow(r).getCell(1).setCellValue(UniqueDailypaymentkeywordDate.get(D)); sheet.getRow(r).getCell(2).setCellValue(UniqueDailypaymentkeywordDescription.get(D)); sheet.getRow(r).getCell(9).setCellValue(UniqueDailypaymentkeywordAmount.get(D)); sheet.getRow(r).getCell(0).setCellValue("OPEN"); break; } else if (sheet.getRow(r).getCell(2).getStringCellValue() .equals(UniqueDailypaymentkeywordDescription.get(D))) { sheet.getRow(r).getCell(9).setCellValue(UniqueDailypaymentkeywordAmount.get(D)); sheet.getRow(r).getCell(0).setCellValue("OPEN"); break; } } } Shifting.InsertFormulas(wb, sheet); Shifting.MergeCells(wb, sheet); // Write output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); wb.write(fileOut); fileOut.close(); InputStream inp = new FileInputStream("workbook.xlsx"); file = new DefaultStreamedContent(inp, "application/vnd.ms-excel", "Table_downloaded.xlsx"); } catch (IOException ex) { Logger.getLogger(OrignalFileDownloadBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidFormatException ex) { Logger.getLogger(OrignalFileDownloadBean.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:test.unit.be.fedict.eid.applet.service.signer.OOXMLSignatureVerifierTest.java
License:Open Source License
@Test public void testOPC() throws Exception { // setup//w ww . j a v a 2 s . c o m InputStream inputStream = OOXMLSignatureVerifierTest.class.getResourceAsStream("/hello-world-signed.docx"); // operate OPCPackage opcPackage = OPCPackage.open(inputStream); ArrayList<PackagePart> parts = opcPackage.getParts(); for (PackagePart part : parts) { LOG.debug("part name: " + part.getPartName().getName()); LOG.debug("part content type: " + part.getContentType()); } ArrayList<PackagePart> signatureParts = opcPackage .getPartsByContentType("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml"); assertFalse(signatureParts.isEmpty()); PackagePart signaturePart = signatureParts.get(0); LOG.debug("signature part class type: " + signaturePart.getClass().getName()); PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager(); // yeah... POI implementation still missing }
From source file:uk.ac.abdn.liftsharing.batch.io.test.ExcelWorkSheetHandlerTest.java
License:Open Source License
/** * @param args//from w w w . java 2s. co m * @throws Exception */ public static void main(String[] args) throws Exception { String SAMPLE_PERSON_DATA_FILE_PATH = "D:\\Sample-Person-Data.xlsx"; // Input File initialize File file = new File(SAMPLE_PERSON_DATA_FILE_PATH); InputStream inputStream = new FileInputStream(file); // Excel Cell Mapping Map<String, String> cellMapping = new HashMap<String, String>(); cellMapping.put("HEADER", "Person Id,Name,Height,Email Address,DOB,Salary"); cellMapping.put("A", "personId"); cellMapping.put("B", "name"); cellMapping.put("C", "height"); cellMapping.put("D", "emailId"); cellMapping.put("E", "dob"); cellMapping.put("F", "salary"); // The package open is instantaneous, as it should be. OPCPackage pkg = null; try { ExcelWorkSheetHandler<PersonVO> workSheetHandler = new ExcelWorkSheetHandler<PersonVO>(PersonVO.class, cellMapping); pkg = OPCPackage.open(inputStream); ExcelSheetCallback sheetCallback = new ExcelSheetCallback() { private int sheetNumber = 0; @Override public void endSheet() { System.out.println("Processing completed for sheet number=" + sheetNumber); } @Override public void startSheet(int sheetNum) { this.sheetNumber = sheetNum; } }; System.out.println("Constructor: pkg, workSheetHandler, sheetCallback"); ExcelReader example1 = new ExcelReader(pkg, workSheetHandler, sheetCallback); example1.process(); if (workSheetHandler.getValueList().isEmpty()) { // No data present LOG.error("sHandler.getValueList() is empty"); } else { LOG.info(workSheetHandler.getValueList().size() + " no. of records read from given excel worksheet successfully."); // Displaying data ead from Excel file displayPersonList(workSheetHandler.getValueList()); } System.out.println("\nConstructor: filePath, workSheetHandler, sheetCallback"); ExcelReader example2 = new ExcelReader(SAMPLE_PERSON_DATA_FILE_PATH, workSheetHandler, sheetCallback); example2.process(); System.out.println("\nConstructor: file, workSheetHandler, sheetCallback"); ExcelReader example3 = new ExcelReader(file, workSheetHandler, null); example3.process(); } catch (RuntimeException are) { LOG.error(are.getMessage(), are.getCause()); } catch (InvalidFormatException ife) { LOG.error(ife.getMessage(), ife.getCause()); } catch (IOException ioe) { LOG.error(ioe.getMessage(), ioe.getCause()); } finally { IOUtils.closeQuietly(inputStream); try { if (null != pkg) { pkg.close(); } } catch (IOException e) { // just ignore IO exception } } }
From source file:uk.ac.abdn.liftsharing.batch.io.test.ExcelWorkSheetRowCallbackHandlerTest.java
License:Open Source License
public static void main(String[] args) throws Exception { String SAMPLE_PERSON_DATA_FILE_PATH = "D:\\Sample-Person-Data.xlsx"; File file = new File(SAMPLE_PERSON_DATA_FILE_PATH); InputStream inputStream = new FileInputStream(file); // The package open is instantaneous, as it should be. OPCPackage pkg = null;// w w w. j a v a 2 s. c o m try { ExcelWorkSheetRowCallbackHandler sheetRowCallbackHandler = new ExcelWorkSheetRowCallbackHandler( new ExcelRowContentCallback() { @Override public void processRow(int rowNum, Map<String, String> map) { // Do any custom row processing here, such as save // to database // Convert map values, as necessary, to dates or // parse as currency, etc System.out.println("rowNum=" + rowNum + ", map=" + map); } }); pkg = OPCPackage.open(inputStream); ExcelSheetCallback sheetCallback = new ExcelSheetCallback() { private int sheetNumber = 0; @Override public void startSheet(int sheetNum) { this.sheetNumber = sheetNum; } @Override public void endSheet() { System.out.println("Processing completed for sheet number=" + sheetNumber); } }; System.out.println("Constructor: pkg, sheetRowCallbackHandler, sheetCallback"); ExcelReader example1 = new ExcelReader(pkg, sheetRowCallbackHandler, sheetCallback); example1.process(); System.out.println("\nConstructor: filePath, sheetRowCallbackHandler, sheetCallback"); ExcelReader example2 = new ExcelReader(SAMPLE_PERSON_DATA_FILE_PATH, sheetRowCallbackHandler, sheetCallback); example2.process(); System.out.println("\nConstructor: file, sheetRowCallbackHandler, sheetCallback"); ExcelReader example3 = new ExcelReader(file, sheetRowCallbackHandler, null); example3.process(); } catch (RuntimeException are) { LOG.error(are.getMessage(), are.getCause()); } catch (InvalidFormatException ife) { LOG.error(ife.getMessage(), ife.getCause()); } catch (IOException ioe) { LOG.error(ioe.getMessage(), ioe.getCause()); } finally { IOUtils.closeQuietly(inputStream); try { if (null != pkg) { pkg.close(); } } catch (IOException e) { // just ignore IO exception } } }
From source file:uk.ac.ucl.cs.cmic.giftcloud.uploader.ExcelWriter.java
License:Open Source License
@Override protected void saveFile(final File file) throws IOException { if (spreadsheetPassword.isPresent() && StringUtils.isNotBlank(new String(spreadsheetPassword.get()))) { try {//from w w w . jav a 2 s .co m // Write the unencrypted spreadsheet to an in-memory stream final ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); baos.close(); // Create objects for encryption POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile); Encryptor enc = info.getEncryptor(); // Set the password enc.confirmPassword(new String(spreadsheetPassword.get())); // Open the in-memory spreadsheet InputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); OPCPackage opc = OPCPackage.open(inputStream); OutputStream os = enc.getDataStream(fs); opc.save(os); opc.close(); inputStream.close(); // Write the encrypted Excel spreadsheet FileOutputStream fos = new FileOutputStream(file); fs.writeFilesystem(fos); fos.close(); } catch (InvalidFormatException e) { throw new IOException( "Unable to save the patient list file due to the following InvalidFormatException when reading the excel file:" + e.getLocalizedMessage(), e); } catch (GeneralSecurityException e) { throw new IOException( "Unable to save the patient list file due to the following GeneralSecurityException when reading the excel file:" + e.getLocalizedMessage(), e); } } else { // Write the unencrypted spreadsheet to an in-memory stream final FileOutputStream fos = new FileOutputStream(file); workbook.write(fos); fos.close(); } }
From source file:vn.vfossa.signature.MicrosoftOOXMLContent.java
License:Open Source License
public MicrosoftOOXMLContent(String path) { this.path = path; try {/* w w w . j a v a 2 s .c o m*/ content = OPCPackage.open(new File(path)); pdsm = new PackageDigitalSignatureManager(content); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:vn.vfossa.signature.MicrosoftOOXMLContent.java
License:Open Source License
public MicrosoftOOXMLContent(String path, byte[] byteArrOfFile) { this.path = path; try {//from ww w . j a va 2 s. c om content = OPCPackage.open(path); pdsm = new PackageDigitalSignatureManager(content); } catch (Exception ex) { ex.printStackTrace(); } }