Class XSSFOptimiser

java.lang.Object
org.apache.poi.xssf.usermodel.XSSFOptimiser

public final class XSSFOptimiser extends Object
XSSF equivalent of HSSFOptimiser.

Provides methods to optimise cell styles and fonts in XSSF workbooks by remapping cells that use duplicate styles/fonts to use canonical (first) occurrences.

This helps avoid "style explosion" issues where Excel workbooks accumulate too many style definitions, causing performance problems or file corruption.

Usage:

   XSSFWorkbook workbook = new XSSFWorkbook(file);
   XSSFOptimiser.optimiseFonts(workbook);
   XSSFOptimiser.optimiseCellStyles(workbook);
   workbook.write(outputStream);
 

Note: Unlike HSSFOptimiser which can remove unused styles from the binary format, this implementation remaps cells to canonical styles without removing entries from the XML. This is because XSSF's StylesTable maintains a separate Java list from the CTStylesheet XML, and removing entries can cause XmlValueDisconnectedException on save. The remapping approach prevents further style explosion and is safe.

Since:
6.0.0
See Also:
  • Method Details

    • optimiseCellStyles

      public static int optimiseCellStyles(XSSFWorkbook workbook)
      Goes through the Workbook, optimising the cell styles by remapping cells that use duplicate styles to use the canonical (first) occurrence.

      Two styles are considered duplicates if they have identical properties: font properties (not just font index), data format, alignment, borders, fill colors, etc.

      For best results, call optimiseFonts(XSSFWorkbook) first.

      Parameters:
      workbook - The workbook to optimise
      Returns:
      The number of cells that were remapped to canonical styles
    • optimiseFonts

      public static int optimiseFonts(XSSFWorkbook workbook)
      Goes through the Workbook, optimising the fonts by updating cell styles that reference duplicate fonts to use the canonical (first) occurrence.

      Two fonts are considered duplicates if they have identical properties: name, size, bold, italic, strikeout, underline, color, typeOffset, charset.

      Parameters:
      workbook - The workbook to optimise
      Returns:
      The number of duplicate fonts found