Silvia Petrova
Silvia Petrova

Reputation: 69

Apache POI: Create a cell with type number, but to be empty

I'm trying to create an excel document. What I need is to have a column with type number. The problem, which I have is that not every cell in this column has a value( some of them should be empty). I have one method, which I call in order to create the cells. I have problem when from the methods parameters get "null", because the setCellValue(double value) can't take null as parameter. This is why I decided to check if it is null to put the cell type: empty.

enter image description here enter image description here

At the end what worked for me is: I just put something to the cell if the value is not null. This worked for me.

private void createCell(String cellStyle, String cellValue){
    cell = row.createCell(cellNumber++);
    cell.setCellStyle(styles.get(cellStyle));

    if (cellStyle.equals("numberWithDelimiters")) {
        if(cellValue!=null){
            cell.setCellValue(Integer.parseInt(cellValue));
        }

    }else if(cellStyle.equals("date")){
        Date date=new Date(Long.parseLong(cellValue));
        date.setHours(0);
        cell.setCellValue(date);
    }else{
    cell.setCellValue(cellValue);
    }
}

Upvotes: 2

Views: 3294

Answers (2)

Pavel Poley
Pavel Poley

Reputation: 5577

@Mohit sharma answer is deprecated, for new POI API use:

cell.setCellType(CellType.BLANK);

Upvotes: 0

mohit sharma
mohit sharma

Reputation: 1070

According to the documentation

 XSSFCell cell = row.createCell(1);
 cell.setCellStyle(Cell.CELL_TYPE_BLANK);
 cell.setCellType(Cell.CELL_TYPE_NUMERIC );

Upvotes: 2

Related Questions