https://bz.apache.org/bugzilla/show_bug.cgi?id=69777

            Bug ID: 69777
           Summary: When using XSSF, calling
                    cell.setCellType(CellType.STRING) on a cell with
                    original type STCellType.INLINE_STR results in the
                    cell value becoming empty.
           Product: POI
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: trevi...@gmail.com
  Target Milestone: ---

** Description:
In Apache POI (XSSF), if a cell’s underlying type is STCellType.INLINE_STR
(inline string), manually converting it to CellType.STRING using:

```
cell.setCellType(CellType.STRING);
```
will cause the cell value to become empty (""). This behavior seems
inconsistent, since one would expect the conversion to preserve the string
content.

** Steps to Reproduce:

1. Create or open an XLSX file containing a cell with type inlineStr in XML
(can be generated by some spreadsheet editors).

2. Read the file with XSSF.

3. Call `cell.setCellType(CellType.STRING)` on that cell.

4. Check the cell value with cell.getStringCellValue() — it will be empty.

** Expected Behavior:
The original inline string content should be preserved after converting the
cell type to CellType.STRING.

** Actual Behavior:
The cell content becomes empty after the type change.

** Example Code:

```
try (InputStream is = Files.newInputStream(Paths.get("inlineStr.xlsx"));
     Workbook wb = new XSSFWorkbook(is)) {
    Sheet sheet = wb.getSheetAt(0);
    Cell cell = sheet.getRow(0).getCell(0);

    System.out.println("Before: " + cell.getStringCellValue()); // original
inline string content

    cell.setCellType(CellType.STRING);

    System.out.println("After: " + cell.getStringCellValue()); // becomes empty
}
```

** Possible Cause:
When calling cell.setCellType(CellType.STRING), the implementation appears to
only set

```
/**
 * org.apache.poi.xssf.usermodel.XSSFCell:893
 */
this._cell.setT(STCellType.S);
```
without copying the original inlineStr value into the `_sharedStringSource`.
As a result, after the cell type change, `getStringCellValue()` attempts to
retrieve the value from the `_sharedStringSource` table, but since the value
was never added, it ends up returning an empty string.

```
/**
 * org.apache.poi.xssf.usermodel.XSSFCell:314
 */
private XSSFRichTextString findStringValue() {
        XSSFRichTextString rt;
        STCellType.Enum xmlbeanCellType = _cell.getT();
        if (xmlbeanCellType == STCellType.INLINE_STR) {
            if(_cell.isSetIs()) {
                //string is expressed directly in the cell definition instead
of implementing the shared string table.
                rt = new XSSFRichTextString(_cell.getIs());
            } else if (_cell.isSetV()) {
                //cached result of a formula
                rt = new XSSFRichTextString(_cell.getV());
            } else {
                rt = new XSSFRichTextString("");
            }
        } else if (xmlbeanCellType == STCellType.STR) {
            //cached formula value
            rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
        } else {
            if (_cell.isSetV()) {
                try {
                    int idx = Integer.parseInt(_cell.getV());
                    rt =
(XSSFRichTextString)_sharedStringSource.getItemAt(idx);
                } catch (Throwable t) {
                    if (ExceptionUtil.isFatal(t)) {
                        ExceptionUtil.rethrow(t);
                    }
                    rt = new XSSFRichTextString("");
                }
            } else {
                rt = new XSSFRichTextString("");   // <-- ends up here
            }
        }
        return rt;
    }
```

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to