0
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(format.getFormat("@"));

Set<Integer> forceTextIndexes = new HashSet<>();
if ("LE_27".equalsIgnoreCase(excelName)) {
    forceTextIndexes.add(3);
    forceTextIndexes.add(6);
} else if ("LE_28".equalsIgnoreCase(excelName)
    || "LE_29".equalsIgnoreCase(excelName)) {
    forceTextIndexes.add(3);
    forceTextIndexes.add(4);
}

for (int i = COPY_VALUES_START_INDEX; i < columnValues.length; i++) {
    int cellIndex = WRITE_START_COLUMN + (i - COPY_VALUES_START_INDEX);
    Cell cell = row.createCell(cellIndex);
    Object value = columnValues[i];

    boolean forceAsText = forceTextIndexes.contains(i);

    if (value == null) {
        cell.setCellType(CellType.BLANK);
    } else if (forceAsText) {
        cell.setCellStyle(textStyle);
        cell.setCellValue("'" + value);
    }
}

I use Apache POI, and I am trying to change the formatting of some cells to text in Excel. Every time I fetch an nvarchar from my SQL database that looks like a number, it keeps on writing it in Excel as a number and not as text. Help!

1 Answer 1

0

cell.setCellValue("'" + value);

This is just putting a literal apostrophe in the cell - it's a workaround when typing manually but in this case, it writes it as actual data

Try changing it to:

cell.setCellValue(String.valueOf(value));

Sign up to request clarification or add additional context in comments.

1 Comment

That shouldn't help because "'" + value is also a string type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.