Formatting Issues

Fix Excel Losing Cell Formatting in Apache POI XLS Files

Nimra MalikNimra Malik Nov 2, 2026 868 views

Question details

The user's Excel 97-2003 (XLS) file generated via Apache POI loses cell formatting after approximately the 43rd column when opened in Excel 2019.

How to Fix Excel Losing Cell Formatting in Apache POI XLS Files
Product
Apache POI / Microsoft Excel
Device & OS
not provided
Scenario
Generating legacy XLS spreadsheet files programmatically using Java and Apache POI, then opening them in a modern version of Microsoft Excel.
Observed behavior
Cell formatting disappears after around the 43rd column because the code creates a brand new cell style object for every single cell during the loop iteration.
Before you start

Review your Java or Kotlin Apache POI code to locate the data population loops and verify where workbook.createCellStyle() is being called.

Solution 1Recommended

Create and Reuse Cell Styles Outside the Data Loop

The most effective way to prevent formatting loss and compatibility issues is to instantiate cell styles once per workbook and apply them to multiple cells.

Apache POI defines workbook styles as persistent objects. Even though HSSFWorkbook theoretically supports up to 4,000 unique styles, creating a new style for every single cell rapidly duplicates style records.

When older .xls files with excessively duplicated style tables are opened in newer versions like Excel 2019, it can trigger rendering bugs that cause formatting to drop entirely after a certain threshold.

1
Locate the cell loop

Find the loop in your application code where rows and cells are being created and populated with data.

2
Extract the style creation

Move the workbook.createCellStyle() method call outside and above the loop so it only runs once.

3
Configure the style properties

Set up your desired fonts, borders, data formats, and background colors on this single, centralized style object.

4
Apply the style to cells

Inside the loop, use cell.setCellStyle(yourStyle) to apply the already existing style to each newly generated cell.

Create and Reuse Cell Styles Outside the Data Loop
Performance Boost: Reusing styles not only fixes the missing formatting bug but also significantly reduces the generated file size and improves application execution speed.
Free Microsoft Office alternative

Try WPS Office to Open and Edit Legacy XLS Files Seamlessly

If you frequently work with legacy Excel 97-2003 (XLS) files generated by Apache POI and experience strict rendering limits or compatibility bugs in newer Excel versions, consider switching to WPS Office. It provides highly robust backward compatibility for older formats while remaining fast and lightweight.

  1. 1. Download WPS Office: Visit the official WPS website to download and install the free software suite.
  2. 2. Open WPS Spreadsheet: Launch the WPS Spreadsheet application from your desktop or start menu.
  3. 3. View your XLS File: Drag and drop your Apache POI generated .xls file into the workspace to view it with full formatting intact.
Highly compatible with all Microsoft Excel formats including legacy .xls and modern .xlsx.Reliable rendering of complex cell formatting and styles across large spreadsheets.Free, lightweight, and fast installation process.Familiar user interface with zero learning curve for seamless migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why does Apache POI documentation state a 4,000 style limit if my file loses formatting at 50 columns?

While the HSSFWorkbook format technically allows up to 4,000 unique styles, modern versions of Excel (like Excel 2019) can struggle to parse redundant, dynamic style records generated for every single cell in legacy .xls formats. Reusing a single style bypasses this rendering limitation.

Does this cell formatting issue affect XSSFWorkbook (.xlsx) files as well?

While the newer .xlsx format handles larger style tables better, creating a new style for every cell is still considered a bad practice in Apache POI. It bloats the XML structure and can lead to severe performance degradation or maximum style limits.

How can I apply multiple different formats without exceeding style limits?

Group your cells logically (e.g., headers, dates, currencies, standard text) and create one specific style for each group outside of your data loops. Apply the respective predefined style to the cells as they are generated.