java - HSSFWorkbook object is not retaining the sheets added to it through different methods -


`   class {     private hssfworkbook workbook;      public a() {         workbook = new hssfworkbook();     }      public void method1() {         workbook.createsheet("sheet1");         system.out.println(" no of sheets: " + workbook.getnumberofsheets());         method2();     }      public void method2() {         system.out.println(" no of sheets: " + workbook.getnumberofsheets());     } } 

in above code m creating workbook object in constructor... , creating "sheet1" in method1 , printing no of sheets: 1 in method1 in method2 no of sheets: 0... why same workbook object behaves differently in different methods.. pls 1 me...

i created times ago class myworkbook able add new sheets , shorten following code show 2 methods add first , other sheets

import org.apache.poi.hssf.usermodel.hssfworkbook;  public class myworkbook {      private final hssfworkbook workbook;      public myworkbook() {         this.workbook = new hssfworkbook();     }      public void createnewsheet() {         workbook.createsheet("sheet1");         printnumberofsheets(workbook);         // here add second sheet         createanothersheet();     }      public void createanothersheet() {         workbook.createsheet();         printnumberofsheets(workbook);     }      private void printnumberofsheets(final hssfworkbook workbook) {         system.out.println("this workbook contains of " + (workbook.getnumberofsheets() > 1 ? + workbook.getnumberofsheets() + " sheets" : workbook.getnumberofsheets() + " sheet"));     }       public hssfworkbook getworkbook() {         return workbook;     }       public static void main(string[] args) {         myworkbook mw = new myworkbook();         mw.createnewsheet();          // workbook contains 1 sheet         mw.printnumberofsheets(mw.getworkbook());          // here add 3rd sheet         mw.createanothersheet();          // our workbook contains of 3 sheets         mw.printnumberofsheets(mw.getworkbook());     } } 

patrick


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -