Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 849
user2810293 Avatar asked Jul 01 '15 07:07

user2810293


People also ask

How do I get the blank cell value in Apache POI?

One way to do what you're requesting is to use HSSFCell. getColumnIndex() . Row. getCell(int) is not deprecated!

How do I write to a cell in Apache POI?

To create a cell in Excel Sheet, we can use Apache POI which provides createCell() method. This method requires an integer argument which should be a column number. The createCell() is a method of Row class. Lets see an example in which we are creating a cell at the specified location.

How do I change the background color of a cell in Excel using Apache POI?

Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose. A list of colors is defined in the IndexedColors class. Similarly, a list of patterns is defined in FillPatternType.


2 Answers

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.

like image 146
Kai Avatar answered Oct 11 '22 00:10

Kai


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++;
            }
        }
like image 23
Alan Avatar answered Oct 11 '22 01:10

Alan