How to set cell value?

puffles picture puffles · May 26, 2016 · Viewed 8k times · Source

I have got the following code:

        HSSFSheet sheet = workbook.getSheetAt(0);

        Cell resultCell=(Cell) sheet.getRow(1).getCell(0);

Problem is result cell doesn't have any method setCellValue(). Following statement gives me an error

 resultCell.setCellValue("PASS");

Answer

Gagravarr picture Gagravarr · May 27, 2016

If you look at the Apache POI JavaDocs for Cell, or the Apache POI Cell examples on the website, you will clearly see that org.apache.poi.ss.usermodel.Cell has a number of setCellValue methods including setCellValue(double) and setCellValue(String)

Most likely you've imported the wrong class for Cell. You should review the import statements at the top of your class, remove the wrong ones, and ensure you only have one of:

import org.apache.poi.ss.usermodel.Worbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;

or:

import org.apache.poi.ss.usermodel.*;