简体   繁体   中英

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!!

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.

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++;
  }
}

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++;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM