Apache poi Spreadsheet

Asked

Viewed 108 times

1

My code when generating a spreadsheet inside an excel file it erases all the others, as I do for it to create the spreadsheet in 2° without it 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");
   }
}

1 answer

0


This happens because you are always overwriting the file data instead of loading and changing it.

Example with xlsx file:

Note that in the first line I instate 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.