Hunter Childress
Hunter Childress

Reputation: 11

Reading/Writing with Apache POI

I just recently began using Apache POI to do some excel reading and writing and I am having a bit of an issue. I have a submit button reading form text fields and printing it to an excel file:

            HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Reservations");
        
        Map<String, Object[]> data = new TreeMap<String, Object[]>();
        
        data.put("1", new Object[] {"Reservation ID", "NAME", "ARRIVAL DATE", "LENGTH OF STAY","HOME ADDRESS", "EMAIL", "PHONE NUMBER" ,"RESERVATION TYPE"});
        data.put("2", new Object[] {1, advanceLastField.getText(), arrivalDateForm.getText(), stayNum.getText(),advanceAddressForm.getText(), advanceEmailForm.getText(),advancePhoneForm.getText(),"D"});
                                                        
        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset){
            Row row = sheet.createRow(rownum++);
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr){
                Cell cell = row.createCell(cellnum++);
                if(obj instanceof String)
                    cell.setCellValue((String)obj);
                else if(obj instanceof Integer)
                    cell.setCellValue((Integer)obj);
            }
            
        }
        try{
            FileOutputStream out = new FileOutputStream(new File("testSheet.xls"),true);
            workbook.write(out);
            out.close();
            System.out.println("Advance Reservation succesfully written");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    }
    }catch(NumberFormatException e){
        parsable1=false;
    }

This is functioning correctly, but my issue is trying to UPDATE the excel sheet afterwards. This is my update code:

FileInputStream inp = new FileInputStream(new File("testSheet.xls"));
HSSFWorkbook wb = new HSSFWorkbook(inp);
HSSFSheet sheet = wb.getSheetAt(0);
if (idLookup.getText() == "1") {
    if (updateNameForm.getText() != null) {
        Row row = sheet.getRow(1);
        Cell cell = row.getCell(1);
        cell.setCellType(CellType.STRING);
        cell.setCellValue(updateNameForm.getText());
    }
}
FileOutputStream fileOut = new FileOutputStream("testSheet.xls", true);
wb.write(fileOut);
fileOut.close();

Can anyone give me a bit of insight or guidance?

Upvotes: 1

Views: 238

Answers (1)

Sully
Sully

Reputation: 14943

You are appending data to the file. I assume you want to recreate the sheet.

public FileOutputStream(String name, boolean append) throws FileNotFoundException

append - if true, then bytes will be written to the end of the file rather than the beginning

Also, make sure to close the input stream before reusing the file

inp.close();

FileOutputStream fileOut = new FileOutputStream("testSheet.xls");
wb.write(fileOut);
fileOut.close();

Upvotes: 2

Related Questions