user2810293
user2810293

Reputation: 155

How to set Blank excel cell using POI

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

Answers (3)

Alan
Alan

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

Kai
Kai

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

blackbishop
blackbishop

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

Related Questions