@ Override public void close ( ) throws IOException { try { // prepare metadata prepareMetaData ( ) ; // write if ( this . oStream != null ) { if ( this . howc . getPassword ( ) == null ) { // no encryption finalizeWriteNotEncrypted ( ) ; } else // encryption if ( this . currentWorkbook instanceof HSSFWorkbook ) { // old Excel format finalizeWriteEncryptedHSSF ( ) ; } else if ( this . currentWorkbook instanceof XSSFWorkbook ) { finalizeWriteEncryptedXSSF ( ) ; } else { LOG . error ( "Could not write encrypted workbook, because type of workbook is unknown" ) ; } } } finally { // close filesystems if ( this . ooxmlDocumentFileSystem != null ) { ooxmlDocumentFileSystem . close ( ) ; } // close main workbook if ( this . currentWorkbook != null ) { this . currentWorkbook . close ( ) ; } // close linked workbooks for ( Workbook currentWorkbookItem : this . listOfWorkbooks ) { if ( currentWorkbookItem != null ) { currentWorkbookItem . close ( ) ; } } } try { // do we need to sign => sign if ( this . signUtil != null ) { // sign LOG . info ( "Signing document \"" + this . howc . getFileName ( ) + "\"" ) ; if ( this . howc . getSigCertificate ( ) == null ) { LOG . error ( "Cannot sign document \"" + this . howc . getFileName ( ) + "\". No certificate for key provided" ) ; } else if ( ! ( this . currentWorkbook instanceof XSSFWorkbook ) ) { LOG . warn ( "Signing of docuemnts in old Excel format not supported for \"" + this . howc . getFileName ( ) + "\"" ) ; } else { try { ArrayList < X509Certificate > certList = new ArrayList <> ( ) ; certList . add ( this . howc . getSigCertificate ( ) ) ; this . signUtil . sign ( this . howc . getSigKey ( ) , certList , this . howc . getPassword ( ) , MSExcelWriter . getHashAlgorithm ( this . howc . getSigHash ( ) ) ) ; } catch ( XMLSignatureException | MarshalException | IOException | FormatNotUnderstoodException e ) { LOG . error ( "Cannot sign document \"" + this . howc . getFileName ( ) + "\" " + e ) ; } } } } finally { if ( this . signUtil != null ) { this . signUtil . close ( ) ; } } }
Writes the document in - memory representation to the OutputStream . Afterwards it closes all related workbooks .