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!