/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.fp.excel; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.StringTokenizer; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author malmeida */ public class WorkbookFactory implements ExcelToMapMessages { private InputStream inp; private String file_path, workbookFactoryStatus, file_extension; private Workbook wb; public WorkbookFactory(String file_Path){ this.file_path = file_Path; this.workbookFactoryStatus = null; this.inp = this.input_Stream_Builder(this.file_path); this.wb = this.excel_Workbook_Builder(); this.file_extension = null; } private InputStream input_Stream_Builder(String file_Path){ try { InputStream inpm = new FileInputStream(file_Path); this.workbookFactoryStatus = WorkbookFactory.INPUT_STREAM_CREATED; return inpm; } catch (FileNotFoundException ex) { this.workbookFactoryStatus = WorkbookFactory.FILE_NOT_FOUND; return null; } } private Workbook excel_Workbook_Builder (){ if(this.workbookFactoryStatus.equals(WorkbookFactory.FILE_NOT_FOUND)){ this.workbookFactoryStatus = WorkbookFactory.WORKBOOK_NOT_CREATED; return null; }else{ Workbook wbm = null; try { this.file_extension = this.get_Workbook_Extension(this.file_path); if(this.file_extension.equals("xls")){ wbm = new HSSFWorkbook(this.inp); } if(this.file_extension.equals("xlsx")){ wbm = new XSSFWorkbook(this.inp); } this.workbookFactoryStatus = WorkbookFactory.WORKBOOK_CREATED; return wbm; } catch (IOException ex) { this.workbookFactoryStatus = WorkbookFactory.WORKBOOK_NOT_CREATED; return null; }catch (Exception ex) { this.workbookFactoryStatus = WorkbookFactory.WORKBOOK_NOT_CREATED; return null; } } } private String get_Workbook_Extension(String file_path){ StringTokenizer tkn = new StringTokenizer(file_path,"."); String fe = null; while (tkn.hasMoreTokens()){ fe = tkn.nextToken(); } return fe; } public String get_WorkbookFactory_Status(){ return this.workbookFactoryStatus; } public Workbook get_ExcelWorkBook(){ return this.wb; } }