org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
Exception
org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
Cause :
Trying to read "xls" file using XSSFWorkbook which is for "xlsx" files.
Consider the below snippet
Below code reads all the sheets and all the cells within the sheet and checks whether the cell has formula injected.
FileInputStream fis = null;
Workbook workbook = null;
try
{
fis = new FileInputStream(new File("D:\\test.xls")); // Reading xls file
workbook = new XSSFWorkbook(fis);
//creating workbook object with XSSFWorkbook
for (int i = 0; i < workbook.getNumberOfSheets(); i++)
{
Sheet sheet = workbook.getSheetAt(i);
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.FORMULA) {
System.out.print("Formula Injected");
break;
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(workbook != null)
workbook.close();
if(fis != null)
{
fis.close();
}
}
Resolution
1. Create Workbook object with HSSFWorkbook for "xls" and XSSFWorkbook for "xlsx"
OR
2. Create generic Workbook object which will work for both "xls" and "xlsx"
Hence , use below
workbook = WorkbookFactory.create(new File("D:\\VA\\Infy\\excel\\macro_enabled.xls"));
instead of
fis = new FileInputStream(new File("D:\\test.xls")); // Reading xls file
workbook = new XSSFWorkbook(fis);
//creating workbook object with XSSFWorkbook
org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
Cause :
Trying to read "xls" file using XSSFWorkbook which is for "xlsx" files.
Consider the below snippet
Below code reads all the sheets and all the cells within the sheet and checks whether the cell has formula injected.
FileInputStream fis = null;
Workbook workbook = null;
try
{
fis = new FileInputStream(new File("D:\\test.xls")); // Reading xls file
workbook = new XSSFWorkbook(fis);
//creating workbook object with XSSFWorkbook
for (int i = 0; i < workbook.getNumberOfSheets(); i++)
{
Sheet sheet = workbook.getSheetAt(i);
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.FORMULA) {
System.out.print("Formula Injected");
break;
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(workbook != null)
workbook.close();
if(fis != null)
{
fis.close();
}
}
Resolution
1. Create Workbook object with HSSFWorkbook for "xls" and XSSFWorkbook for "xlsx"
OR
2. Create generic Workbook object which will work for both "xls" and "xlsx"
Hence , use below
workbook = WorkbookFactory.create(new File("D:\\VA\\Infy\\excel\\macro_enabled.xls"));
instead of
fis = new FileInputStream(new File("D:\\test.xls")); // Reading xls file
workbook = new XSSFWorkbook(fis);
//creating workbook object with XSSFWorkbook
Comments
Post a Comment