poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Javen O'Neal" <one...@apache.org>
Subject Re: Not able to add excelsheets to existing excel file
Date Thu, 11 May 2017 05:29:27 GMT
You create 2 workbook objects if the file exists. One of those workbooks
doesn't get closed.

If the workbook exists, you read from and write to the same file while the
workbook is open. This corrupts the file. Either use the in-place write
method (this is a new addition, still in development), or write to a
different file location, close the workbook, and shuffle the files around
as necessary.

On May 8, 2017 07:51, "Aakash Rathod" <aakashrathod111@gmail.com> wrote:

Hi All,
I want to add new sheets to existing excel file, but when file doesn't
exist it it successfully created and even opens successfully with the data
and when i try to add new sheet to this existing file it doesn't throw any
error but when i try to open this file it throws error saying "We found a
problem with some content in 'filename' ? Do you want us to try to
recover..........". Code is pasted below. Please look into it and help me

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FileCheck {
        public static void main(String[] args) {
                XSSFWorkbook workbook = new XSSFWorkbook();
            String fileName="Trial.xlsx";
            File file = new File(fileName);
            FileOutputStream out;
            if(!file.exists()){ // This will create new workbook with new
sheet if it doesnt exists{
                        XSSFSheet mySheet = workbook.createSheet("A");
            } else{ // This add new sheet to above created workbook
                    try {
                        XSSFWorkbook myWorkBook = (XSSFWorkbook)
                        XSSFSheet mySheet = (XSSFSheet)
                    } catch (InvalidFormatException e) {
                        // TODO Auto-generated catch block
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                out = new FileOutputStream(fileName,true);
                }catch(Exception e){

Aakash Rathod

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message