aeinstein83
aeinstein83

Reputation: 289

How to write multiple sheets in same file using apache poi

I have 2 workbooks that i want to write in same excel file, following is my code

public void csvWriteToFile(HSSFWorkbook workbook1,HSSFWorkbook workbook2,String fileName){
   try {
          FileOutputStream out = new FileOutputStream(new File(fileName));
      workbook1.write(out);
      workbook2.write(out);
      out.close();
      System.out.println("Excel written successfully..");            
         } catch (FileNotFoundException e) {
     e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
    }

This is creating only one sheet in the excel file.. am I doing something wrong here..?

Upvotes: 0

Views: 2364

Answers (1)

Pino
Pino

Reputation: 9333

You are concatenating the binary code of two documents in one new file. On the contrary you should copy the sheets of the first doc to the other one using POI API, then you can save the resulting document in that way.

Upvotes: 2

Related Questions