From b25c275e6f1ab28dde36e5753654dc2987be4837 Mon Sep 17 00:00:00 2001 From: SoltauFintel Date: Sat, 31 Oct 2020 10:32:02 +0100 Subject: [PATCH] #68 clearCellValue workaround for every setCellFormula() call (#70) streaming --- .../org/jxls/transform/poi/PoiCellData.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/jxls-poi/src/main/java/org/jxls/transform/poi/PoiCellData.java b/jxls-poi/src/main/java/org/jxls/transform/poi/PoiCellData.java index 44108168..adbdd85b 100644 --- a/jxls-poi/src/main/java/org/jxls/transform/poi/PoiCellData.java +++ b/jxls-poi/src/main/java/org/jxls/transform/poi/PoiCellData.java @@ -1,5 +1,8 @@ package org.jxls.transform.poi; +import java.util.Date; +import java.util.Map; + import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -9,15 +12,14 @@ import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.usermodel.XSSFCell; import org.jxls.common.CellRef; import org.jxls.common.Context; import org.jxls.util.Util; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; -import java.util.Map; - /** * Cell data wrapper for POI cell * @@ -244,6 +246,7 @@ private void updateFormulaCellContents(Cell cell) { cell.setCellValue((String) evaluationResult); } else { cell.setCellFormula((String) evaluationResult); + clearCellValue(cell); // This call is especially important for streaming. } } catch (FormulaParseException e) { try { @@ -257,6 +260,19 @@ private void updateFormulaCellContents(Cell cell) { } } + // protected so any user can change this piece of code + protected void clearCellValue(org.apache.poi.ss.usermodel.Cell poiCell) { + if (poiCell instanceof XSSFCell) { + CTCell cell = ((XSSFCell) poiCell).getCTCell(); // POI internal access, but there's no other way + // Now do the XSSFCell.setFormula code that was done before POI commit https://github.com/apache/poi/commit/1253a29 + // After setting the formula in attribute f we clear the value attribute v if set. This causes a recalculation + // and prevents wrong formula results. + if (cell.isSetV()) { + cell.unsetV(); + } + } + } + private void updateCellStyle(Cell cell, CellStyle cellStyle) { cell.setCellStyle(cellStyle); }