Skip to content

Commit

Permalink
#68 clearCellValue workaround for every setCellFormula() call (#70)
Browse files Browse the repository at this point in the history
streaming
  • Loading branch information
SoltauFintel committed Oct 31, 2020
1 parent a7a1aac commit b25c275
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions 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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down

0 comments on commit b25c275

Please sign in to comment.