Class ExcelHandler
- All Implemented Interfaces:
FileHandler
This class is responsible for writing the SXSSFWorkbook to an OutputStream,
with optional support for Excel password encryption.
It ensures that the workbook is consumed only once.
Why not AutoCloseable?
The primary usage pattern is ResponseEntity<StreamingResponseBody>,
where the handler is captured by a lambda and consumed asynchronously.
Implementing AutoCloseable would cause IDE "resource not closed" warnings
in this pattern, requiring @SuppressWarnings("resource") on every call site.
The workbook is always closed inside writeTo(OutputStream) overloads,
and there is no realistic code path where the handler is obtained but never consumed,
since callers either invoke it immediately or pass it to a StreamingResponseBody lambda.
- Since:
- 2025-07-19
-
Method Summary
Modifier and TypeMethodDescriptionvoidwriteTo(OutputStream outputStream) Writes the workbook to the given OutputStream.voidwriteTo(OutputStream outputStream, char[] password) Writes the workbook to the given OutputStream with Excel-compatible password encryption.voidwriteTo(OutputStream outputStream, String password) Writes the workbook to the given OutputStream with Excel-compatible password encryption.voidWrites the workbook directly to a file path with Excel-compatible password encryption.voidWrites the workbook directly to a file path with Excel-compatible password encryption.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.github.dornol.excelkit.core.FileHandler
writeTo
-
Method Details
-
writeTo
Writes the workbook to the given OutputStream.If a password was set via
ExcelWriter.password(String)orExcelWorkbook.password(String), the output is automatically encrypted using the "agile" encryption mode.This method can only be called once; subsequent calls will throw an exception.
- Specified by:
writeToin interfaceFileHandler- Parameters:
outputStream- The OutputStream to write the Excel file to- Throws:
ExcelWriteException- If this method has already been called or if an I/O error occurs
-
writeTo
Writes the workbook to the given OutputStream with Excel-compatible password encryption.This overload encrypts the file using the "agile" encryption mode supported by modern Excel versions. Cannot be used when a password was already set via
ExcelWriter.password(String)orExcelWorkbook.password(String)— usewriteTo(OutputStream)instead.Memory note: POI's encryption API does not support file-backed buffering for the OLE container — the encrypted payload is held in heap (≈ size of the resulting XLSX) before being streamed out. For very large exports (hundreds of MB) plan heap accordingly, or prefer the plain
writeTo(OutputStream)path.- Parameters:
outputStream- The OutputStream to write the encrypted Excel file topassword- The password to protect the Excel file with- Throws:
ExcelWriteException- If an I/O or encryption error occurs during writingIllegalStateException- If a password was already set at the writer level, or if already consumed- Since:
- 0.16.5
-
writeTo
Writes the workbook to the given OutputStream with Excel-compatible password encryption.This overload accepts a
char[]to allow callers to clear the password from memory after use. The array is zeroed out after encryption completes (or on failure). Cannot be used when a password was already set viaExcelWriter.password(String)orExcelWorkbook.password(String)— usewriteTo(OutputStream)instead.Memory note: same as
writeTo(OutputStream, String)— POI buffers the encrypted OLE container in heap.- Parameters:
outputStream- The OutputStream to write the encrypted Excel file topassword- The password as a char array (will be zeroed after use)- Throws:
ExcelWriteException- If an I/O or encryption error occurs during writingIllegalStateException- If a password was already set at the writer level, or if already consumed- Since:
- 0.16.5
-
writeTo
Writes the workbook directly to a file path with Excel-compatible password encryption.Convenience overload that opens a buffered
OutputStreamto the given path and delegates towriteTo(OutputStream, String). Cannot be used when a password was already set viaExcelWriter.password(String)orExcelWorkbook.password(String)— useFileHandler.writeTo(Path)instead.- Parameters:
path- The destination file pathpassword- The password to protect the Excel file with- Throws:
ExcelWriteException- If an I/O or encryption error occurs during writingIllegalStateException- If a password was already set at the writer level, or if already consumed- Since:
- 0.16.6
-
writeTo
Writes the workbook directly to a file path with Excel-compatible password encryption.This overload accepts a
char[]to allow callers to clear the password from memory after use. The array is zeroed out after encryption completes (or on failure). Cannot be used when a password was already set viaExcelWriter.password(String)orExcelWorkbook.password(String)— useFileHandler.writeTo(Path)instead.- Parameters:
path- The destination file pathpassword- The password as a char array (will be zeroed after use)- Throws:
ExcelWriteException- If an I/O or encryption error occurs during writingIllegalStateException- If a password was already set at the writer level, or if already consumed- Since:
- 0.16.6
-