Saturday 4 June 2016

How to create common functions for working with different format of Excel Sheet by using Apache POI



By using below verifyExcelVersion() we can create common function for handle with different Excel format by using Apache POI



code:

public static void main(String agrs[])
{

Workbook book2=verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


if(!isExcelSheetExist(wb,"Sheet2"))
createExcelSheet(wb,"Sheet2",Download_FilePath);

}




// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}

No comments:

Post a Comment