fix: preserve template style when using a converter #402
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request addresses an issue where pre-existing cell styles from a template are lost when the cell's data is processed by a
Converter
. I have created a minimal reproducible example to demonstrate the problem.The Problem
When writing data to an Excel file using a template that contains pre-formatted cells (e.g., with background colors, borders), if a cell's content is handled by a
Converter
(likeLocalDateConverter
which applies a date format), the original style from the template is completely overridden. The final cell only retains the new style elements introduced by the converter (typically just the data format), losing all visual styling from the template.To Reproduce:
I have set up a dedicated repository that clearly demonstrates this issue. It uses a template with a yellow background in cell B2 and writes a
LocalDate
to it.Root Cause Analysis
The root cause lies in the interaction between
Converter
implementations and theFillStyleCellWriteHandler
.Converter
implementations (e.g., forDate
,LocalDate
) callWorkBookUtil.fillDataFormat()
to apply a specific data format.WriteCellStyle
object and sets it on theWriteCellData
.originCellStyle
on theWriteCellData
remainsnull
, as the framework does not automatically capture the cell's existing style from the template at this stage.FillStyleCellWriteHandler
, the handler attempts to mergewriteCellStyle
(from the converter) andoriginCellStyle
(null
). SinceoriginCellStyle
isnull
, the template's style is ignored, and the cell's style is completely replaced by the new style from the converter.The Solution
To address this, I've made a targeted modification to
FillStyleCellWriteHandler
.The new logic introduces a check:
This is a surgical fix that ensures:
originCellStyle
has not been set.Converter
has likely added a data format (writeCellStyle.getDataFormatData() != null
).Cell
object before the merge operation.This approach is minimally invasive and avoids breaking changes to the
Converter
interface or other utility classes.