Hi,

I am trying to add content to an excel sheet using the below code. However, when I open the excel sheet, nothing is added. Please advice.

public class StepNoToExcel 
{
    HSSFWorkbook wb = null;
    String fileName = "C:\\StepNoForEvidences.xls";
    File file = null;
    StepNoToExcel()
    {
        file = new File(fileName);
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        wb = new HSSFWorkbook();
        wb.createSheet("Step Number");
    }

    public void addToWorkBook(String step) throws IOException
    {
        System.out.println(step);
        HSSFSheet sheet = wb.getSheetAt(wb.getActiveSheetIndex());
        for(Row row : sheet)
        {

            Cell cell = row.createCell(0);
            if(cell.equals(Cell.CELL_TYPE_BLANK))
            {
                cell.setCellValue(step);
            }
        }
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
    }
}

Dani AI

Generated

Quick diagnosis (what's actually happening)
Your constructor always creates a fresh HSSFWorkbook and a new sheet, then addToWorkBook iterates the existing rows. If the sheet has no rows the for (Row row : sheet) loop never runs, so nothing is written. Also cell.equals(Cell.CELL_TYPE_BLANK) is incorrect: that compares an object to a numeric constant instead of asking the cell for its type. If the goal is to append to an existing XLS on disk you must open that file into a Workbook first — otherwise you’re always starting with an empty workbook and overwriting the file. (poi.apache.org)

A simple, correct workflow (load → append → write)

  1. If the file exists open it with a FileInputStream and build an HSSFWorkbook. 2) Get (or create) the sheet. 3) Compute the next empty row (use getPhysicalNumberOfRows() to detect an empty sheet, otherwise getLastRowNum() + 1). 4) createRow(...), createCell(...), setCellValue(...). 5) Close input, then write the workbook with a FileOutputStream. Example pattern (short):
HSSFWorkbook wb;
if (file.exists()) {
  try (FileInputStream fis = new FileInputStream(file)) { wb = new HSSFWorkbook(fis); }
} else {
  wb = new HSSFWorkbook();
}
HSSFSheet sheet = wb.getSheet("Step Number");
if (sheet == null) sheet = wb.createSheet("Step Number");
int newRow = (sheet.getPhysicalNumberOfRows() == 0) ? 0 : sheet.getLastRowNum() + 1;
Row row = sheet.createRow(newRow);
row.createCell(0).setCellValue(step);
try (FileOutputStream fos = new FileOutputStream(file)) { wb.write(fos); }
wb.close();

See the Sheet javadoc for getLastRowNum() / getPhysicalNumberOfRows() behavior and POI quick-guide examples for reading/writing workbooks. (poi.apache.org)

Notes and gotchas

  • Use cell.getCellType() / CellType.BLANK (newer POI) rather than equals(...) against numeric constants. (poi.apache.org)
  • Close the FileInputStream before writing (or use try-with-resources). HSSFWorkbook.write(...) replaces the file; do not attempt to write to an open input stream. (poi.apache.org)

Thanks to for the sample and to for the pointer to community threads — the JavaDoc + the quick-guide examples above are the clearest, version-safe references when you implement the append approach.

I found this link, see if it helps:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.