How to add new sheets to existing excel workbook using apache POI?

前端 未结 5 528
心在旅途
心在旅途 2020-12-15 12:48

I am trying to write List data into multiple excel sheet in one work book. like for first list, the code will create new workbook and create new sheet for list[1], for secon

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 13:10

    While working with existing workbook , You should not create a file object , Just need to Provide the File Location to FileInputStream object and pass it's reference to workbook object, then do whatever you want to do with the existing file. To add a sheet just use create Method command

    public void AddsheetintoExistingworkbook(String sheetname) throws IOException, InvalidFormatException{
    
        //***************************Add a sheet into Existing workbook***********************************************
    
    
     String path="C:\\Workspace\\Selenium_2.53\\src\\InputFiles\\Temp.xlsx";
     fileinp = new FileInputStream(path);
     workbook = new XSSFWorkbook(fileinp);
     workbook.createSheet(sheetname);
    
     fileOut = new FileOutputStream(path);
        workbook.write(fileOut);
        fileOut.close();
     System.out.println("File is written successfully");    
    }
    

提交回复
热议问题