user3367327
user3367327

Reputation: 15

Append Excel file using Apache POI

I have a java program that prints 1000 integer values each I run it. I want to copy the output to an excel file each time I run the program. I want to output the first run in the first column in excel file and then copy the next runs in the subsequent columns in the same excel file. For example:

Run: 1 value1 value2 . . value1000 Run:2 value1 value2 . . value1000 I want the first output in the first column of an excel file and the second output in the second column

Here is my code:

  int rownum=1;
  int cellnum=1;
  File file; 
  HSSFWorkbook workbook;
  HSSFSheet sheet;
  HSSFRow row;
  HSSFCell cell;

  HSSFWorkbook workbook = new HSSFWorkbook();
  HSSFSheet sheet = workbook.createSheet("Sample sheet");

  public void writeOutput(double meandispersion) {    

      String dispersion = Double.toString(meandispersion);

      HSSFRow row = sheet.createRow(rownum++);
      HSSFCell cell = row.createCell(cellnum);    
      cell.setCellValue("dispersion");

      try {
          FileOutputStream out = new FileOutputStream("newFile.xls");
          workbook.write(out);
          out.close();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
       }   

  }

There are a total of 1000 time steps in the main code and in each time step this method is called and the value of meandispersion is passed to it. It prints the 1000 values in 1000 rows in the first column. The problem is that when I run the program second time I want to copy the 1000 values in the second column, for third run 3rd column and so on. Currently it is not appending the values, it overwrites the entire file. Can anyone point out the problem?

Upvotes: 1

Views: 8047

Answers (2)

fvu
fvu

Reputation: 32973

POI's Quick Guide aka the "Busy Developers' Guide to HSSF and XSSF Features" contains lots of code snippets, including one that talks about opening an existing workbook, and reading and writing and saving the modified workbook (example verbatim copied from that page, added some comments):

InputStream inp = new FileInputStream("workbook.xls");
// notice how the Workbook must be constructed from the existing file
Workbook wb = WorkbookFactory.create(inp);
// Navigating in POI always follows the same logic:
// 1. grab a sheet
// 2. grab a row from that sheet
// 3. grab a cell from that row
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
// a condition like the one that follows will be needed to know in what column
// you have to write your data:   
if (cell == null)
    cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

That, and the other examples on that page should get you up to speed quickly.

Upvotes: 2

Tanmay Patil
Tanmay Patil

Reputation: 7057

You have to read the existing file, append your new output to the existing data yourself (in correct column and then write it to the file.

Right now you are not reading the file at all, which would overwrite the existing contents.

Hope this helps.

Upvotes: 2

Related Questions