Search code examples
apache-poipoi-hssf

Apache Poi: Converting from HSSF to SS?


I've converted all of my old code in HSSF to SS except for the portion where I make the work book.


Old Code: HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));


New Non-working Code: Workbook[] wb2 = new Workbook[] {new FileInputStream};


This is an example of conversion that the site gave:

NEW: Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; OLD: HSSFWorkbook wb = new HSSFWorkbook();


Solution

  • Workbook is an interface, you can't instantiate it. Instead, what you want is WorkbookFactory. Pass that either a File or an InputStream, and it'll give you the appropriate Workbook instance

    Your old code:

    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
    

    Becomes:

    Workbook wb = WorkbookFactory.create(file);