Class UpdateEmbeddedDoc


  • public class UpdateEmbeddedDoc
    extends Object
    Tests whether it is possible to successfully update an Excel workbook that is embedded into a WordprocessingML document. Note that the test has currently only been conducted with a binary Excel workbook and NOT yet with a SpreadsheetML workbook embedded into the document.

    This code was successfully tested with the following file from the POI test collection: http://svn.apache.org/repos/asf/poi/trunk/test-data/document/EmbeddedDocument.docx

    • Constructor Detail

      • UpdateEmbeddedDoc

        public UpdateEmbeddedDoc​(String filename)
                          throws FileNotFoundException,
                                 IOException
        Create a new instance of the UpdateEmbeddedDoc class using the following parameters;
        Parameters:
        filename - An instance of the String class that encapsulates the name of and path to a WordprocessingML Word document that contains an embedded binary Excel workbook.
        Throws:
        FileNotFoundException - Thrown if the file cannot be found on the underlying file system.
        IOException - Thrown if a problem occurs in the underlying file system.
    • Method Detail

      • updateEmbeddedDoc

        public void updateEmbeddedDoc()
                               throws OpenXML4JException,
                                      IOException
        Called to update the embedded Excel workbook. As the format and structure 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:
        OpenXML4JException - Rather than use the specific classes (HSSF/XSSF) to handle the embedded workbook this method uses those defined 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.
        IOException - Thrown if a problem occurs in the underlying file system.
      • checkUpdatedDoc

        public void checkUpdatedDoc()
                             throws OpenXML4JException,
                                    IOException
        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.

        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

        java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

        to be thrown when the WorkbookFactory.createWorkbook(InputStream) method is executed.

        Throws:
        OpenXML4JException - Rather than use the specific classes (HSSF/XSSF) to handle the embedded workbook this method uses those defined 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.
        IOException - Thrown if a problem occurs in the underlying file system.