How to set blank excel cell using POI

I have a condition where I need to check for a delimiter in a string. If found, I need to set the 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 doesn't set up empty cell, even I tried with cell.setCellValue ("") but again not set. Has he tried skipping that cell if we want to set an empty cell? if not then someone can let me know how I can do this.

Appreciate your help!

+4


source to share


3 answers


As per the implementation of the documentation and POI, I expect that

cell.setCellType(Cell.CELL_TYPE_BLANK);

      



should do the trick and whatever you need.

+10


source


You are using setCellValue

. Use this method instead: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++;
  }
}

      

0


source


I'm a little late, but,

don't worry about setting the cell to empty. Consider leaving the camera blank. When you create a cell, it is empty by default until you set its value.

try something like this:

            row = sheet.createRow(newRow);
        for(String fieldValues: fieldList)
        {
            if(fieldValues.contains(","))
            {
                XSSFCell cell = row.createCell(cellNo); //it already blank here
                cellNo++;
            }
            else
            {
                XSSFCell cell = row.createCell(cellNo);
                cell.setCellValue(fieldValues);
                cellNo++;
            }
        }

      

0


source







All Articles