Interface FileHandler

All Known Implementing Classes:
CsvHandler, ExcelHandler

public interface FileHandler
Common contract for file handlers produced by writer entry points.

A FileHandler wraps a generated file payload (an SXSSF workbook, a staged CSV temp file, etc.) and writes it to a target OutputStream exactly once. This lets callers treat Excel and CSV outputs polymorphically — for example, a single Spring controller method can return either by wiring the handler into a StreamingResponseBody.

One-shot contract

Each implementation must refuse a second writeTo(OutputStream) call and must release any temporary resources (workbooks, staging files) inside the call, whether it succeeds or throws.

Closed hierarchy

The shipped implementations (ExcelHandler, CsvHandler) are final. excel-kit ships as an automatic module (no module-info.java), so this interface is not declared sealed — but the library does not support pluggable output formats beyond the ones it ships. Third-party implementations are unsupported.
Since:
0.11.0
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Writes the generated file content to the given output stream.
    default void
    writeTo(Path path)
    Writes the generated file content directly to a file path.
  • Method Details

    • writeTo

      void writeTo(OutputStream out)
      Writes the generated file content to the given output stream.

      Can only be called once per handler instance. The handler's backing resources (workbook, staging file) are released before this method returns.

      I/O errors are wrapped as unchecked exceptions (e.g., ExcelWriteException, CsvWriteException) so callers do not need to handle checked exceptions.

      Parameters:
      out - the destination stream
    • writeTo

      default void writeTo(Path path)
      Writes the generated file content directly to a file path.

      Convenience overload that opens a buffered OutputStream to the given path, delegates to writeTo(OutputStream), and closes the stream. The one-shot contract applies — this method can only be called once.

      
       ExcelWriter.<User>create()
           .column("Name", User::getName)
           .write(stream)
           .writeTo(Path.of("users.xlsx"));
       
      Parameters:
      path - the destination file path
      Since:
      0.14.0