Reputation: 155
I have a condition where in I need to check for the Delimiter in a string. If found then I need to set excel cell to blank. I have this code:
row = sheet.createRow(newRow);
for(String fieldValues: fieldList)
{
if(fieldValues.contains(","))
{
XSSFCell cell = row.createCell(cellNo);
cell.setCellValue(Cell.CELL_TYPE_BLANK);
cellNo++;
}
else
{
XSSFCell cell = row.createCell(cellNo);
cell.setCellValue(fieldValues);
cellNo++;
}
}
Cell.CELL_TYPE_BLANK is not setting up the Blank Cell even I tried with cell.setCellValue("") but again it doesn't set. Does it try to skip that cell if we want to set a Blank Cell? if not then can anyone let me know how I can do that.
Appreciate your help!!
Upvotes: 7
Views: 21928
Reputation: 976
I am a little late but,
dont think about setting the cell to be empty. Think about leaving the cell empty. When you create a Cell, it is empty by default until you set it's value.
try something like this:
row = sheet.createRow(newRow);
for(String fieldValues: fieldList)
{
if(fieldValues.contains(","))
{
XSSFCell cell = row.createCell(cellNo); //it's already blank here
cellNo++;
}
else
{
XSSFCell cell = row.createCell(cellNo);
cell.setCellValue(fieldValues);
cellNo++;
}
}
Upvotes: 3
Reputation: 708
According to the Documentation and POI implementation I'd expect that
cell.setCellType(Cell.CELL_TYPE_BLANK);
should do the trick and is all you need.
Upvotes: 14
Reputation: 32700
You are using setCellValue
. Instead use this method : setCellStyle
row = sheet.createRow(newRow);
for(String fieldValues: fieldList)
{
if(fieldValues.contains(","))
{
XSSFCell cell = row.createCell(cellNo);
cell.setCellStyle(Cell.CELL_TYPE_BLANK);
cellNo++;
}
else
{
XSSFCell cell = row.createCell(cellNo);
cell.setCellValue(fieldValues);
cellNo++;
}
}
Upvotes: 0