Apache poi SpreadSheet

1

My code when generating a spreadsheet within an excel file it erases all the others, how do I make it create the spreadsheet in 2nd without deleting the others?

My code:

import java.io.*;
import org.apache.poi.xssf.usermodel.*;

public class Apache {

   public static void main(String[] args)throws Exception {


      XSSFWorkbook workbook = new XSSFWorkbook(); 


      FileOutputStream out = new FileOutputStream("workbook.ods");

      XSSFSheet spreadsheet = workbook.createSheet("teste 2");

      workbook.write(out);
      out.close();
      System.out.println("workbook.ods");
   }
}
    
asked by anonymous 12.11.2017 / 15:48

1 answer

0

This is because you are always replacing the data in the file, rather than loading and changing it.

Example with xlsx file:

Note that in the first line I instantiate a Workbook based on an existing file.

    XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("caminho arquivo xlsx"));

    FileOutputStream out = new FileOutputStream("caminho arquivo xlsx");

    XSSFSheet spreadsheet = workbook.createSheet("teste 2");

    workbook.write(out);
    out.close();

Obs: If you try to create a Sheet with a name that already exists, it will generate an exception.

    
13.11.2017 / 14:31