/** * Called to update the embedded Excel workbook. As the format and structire of the workbook are * known in advance, all this code attempts to do is write a new value into the first cell on the * first row of the first worksheet. Prior to executing this method, that cell will contain the * value 1. * * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException Rather than use the specific * classes (HSSF/XSSF) to handle the embedded workbook this method uses those defeined in the * SS stream. As a result, it might be the case that a SpreadsheetML file is opened for * processing, throwing this exception if that file is invalid. * @throws java.io.IOException Thrown if a problem occurs in the underlying file system. */ public void updateEmbeddedDoc() throws OpenXML4JException, IOException { Workbook workbook = null; Sheet sheet = null; Row row = null; Cell cell = null; PackagePart pPart = null; Iterator<PackagePart> pIter = null; List<PackagePart> embeddedDocs = this.doc.getAllEmbedds(); if (embeddedDocs != null && !embeddedDocs.isEmpty()) { pIter = embeddedDocs.iterator(); while (pIter.hasNext()) { pPart = pIter.next(); if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION) || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) { // Get an InputStream from the pacage part and pass that // to the create method of the WorkbookFactory class. Update // the resulting Workbook and then stream that out again // using an OutputStream obtained from the same PackagePart. workbook = WorkbookFactory.create(pPart.getInputStream()); sheet = workbook.getSheetAt(SHEET_NUM); row = sheet.getRow(ROW_NUM); cell = row.getCell(CELL_NUM); cell.setCellValue(NEW_VALUE); workbook.write(pPart.getOutputStream()); } } // Finally, write the newly modified Word document out to file. this.doc.write(new FileOutputStream(this.docFile)); } }
/** * Called to test whether or not the embedded workbook was correctly updated. This method simply * recovers the first cell from the first row of the first workbook and tests the value it * contains. * * <p>Note that execution will not continue up to the assertion as the embedded workbook is now * corrupted and causes an IllegalArgumentException with the following message * * <p><em>java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an * OOXML stream</em> * * <p>to be thrown when the WorkbookFactory.createWorkbook(InputStream) method is executed. * * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException Rather than use the specific * classes (HSSF/XSSF) to handle the embedded workbook this method uses those defeined in the * SS stream. As a result, it might be the case that a SpreadsheetML file is opened for * processing, throwing this exception if that file is invalid. * @throws java.io.IOException Thrown if a problem occurs in the underlying file system. */ public void checkUpdatedDoc() throws OpenXML4JException, IOException { Workbook workbook = null; Sheet sheet = null; Row row = null; Cell cell = null; PackagePart pPart = null; Iterator<PackagePart> pIter = null; List<PackagePart> embeddedDocs = this.doc.getAllEmbedds(); if (embeddedDocs != null && !embeddedDocs.isEmpty()) { pIter = embeddedDocs.iterator(); while (pIter.hasNext()) { pPart = pIter.next(); if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION) || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) { workbook = WorkbookFactory.create(pPart.getInputStream()); sheet = workbook.getSheetAt(SHEET_NUM); row = sheet.getRow(ROW_NUM); cell = row.getCell(CELL_NUM); assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001); } } } }