Apache POI handles .xls but throws exception on .xlsx - java

I am using NetBeans 14 to build a Java 8 project with Ant build. I have included:
poi-5.2.3.jar
poi-ooxml-full-5.2.3.jar
SpareseBitSet-1.2.jar
commons-codec-1.15.jar
commons-collections4-4.4.jar
commons-compress-1.2.1.jar
commons-io-2.11.0.jar
commons-math3-3.6.1.jar
curvesapi-1.0.7.jar
xml-api-1.4.0.1.jar
xmlbeans-5.1.1.jar
It builds fine. When I create a Workbook using an xls file as input it works. When I try to create a Workbook using an xlsx file it throws an exception reporting:
IO Exception processing input Bio file
java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: OOXML, having providers: [org.apache.poi.hssf.usermodel.HSSFWorkbookFactory#71a2f010]
at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:334) ~[poi-5.2.3.jar:5.2.3]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:311) ~[poi-5.2.3.jar:5.2.3]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:277) ~[poi-5.2.3.jar:5.2.3]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:255) ~[poi-5.2.3.jar:5.2.3]
I've tried changing ooxml library types (-lite, -full, both, etc.).
Any help would be welcome.

I have tried sample code, working fine.
File myFile = new File("C://temp/Employee.xlsx"); FileInputStream fis = new
FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
// Get iterator to all the rows in current
sheet Iterator<Row> rowIterator = mySheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
// Enter you code here to fetch data based on cell type
}
} System.out.println("");
}

Related

File Path not changing in code

So I have created a class that searches an excel file and prints all the rows and columns of the file. Now I have an exception file not found error. In order to fix this error I have changed the file path in the code but for some reason I get the same error and the error message in the console shows the previous file path I was using that gave me error.
I know the new file path is correct but Eclipse doesn't seem to recognize that the code has been updated. Here is the code from the class:
public class ExcelReader {
public static final String SAMPLE_XLSX_FILE_PATH = "K:\\Documents\\Project\\Netword_GUI\\Netword_GUI\\src\\libs\\cc2017.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
/*
=============================================================
Iterating over all the sheets in the workbook (Multiple ways)
=============================================================
*/
// You can obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
//System.out.println(sheet.getRow(0));
System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
if (sheet.getActiveCell() == null) {
// Closing the workbook
workbook.close();
}
}
}
Now here is the error message displayed in the console:
Exception in thread "main" java.io.FileNotFoundException:
C:\Users\User\Dropbox\Placement\Private_Backup\Netword_GUI\Netword_GUI\src\cc2017.xlsx
at
org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250)
at
org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:226)
at
org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:205)
at ExcelReader.main(ExcelReader.java:13)
Notice: The file path in the console is different to the file path in the code. Previously the file path in the error message was in the code this is because I have the same class on another computer so the file path was changed as the location is different on the computer I am currently working on.
Sometimes the build in Eclipse is not up to date.
In that case you should clean and re-build your project (from the "Project" menu)
could you please add your code to make every one get the big picture of your code to give you a correct answer
try this one
File f=new File(fixed_file_path);
Workbook workbook = WorkbookFactory.create(f);

Unable to read the excel file using Apache POI - getting exception

Please find the Apache POI java code to read the .xls file.
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
while reading the .xls file using Java Apache POI, I am getting the below error in the Java Console.
java.io.IOException: Invalid header signature; read 0x6C6D783F3CBFBBEF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
I am able to manually open the excel file without any issues. Do we have the solution to overcome this. I'm completely out of ideas so any help/pointers are greatly appreciated :)
FileInputStream fis = new FileInputStream(new File(yourpath+"/WebContent/ProductUpload.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
int numberOfSheets = workbook.getNumberOfSheets();
for(int i=0; i < numberOfSheets; i++){
XSSFSheet sheet = workbook.getSheetAt(i);
Iterator ite = sheet.rowIterator();
while(ite.hasNext()){
Row row = (Row)ite.next();
Iterator<org.apache.poi.ss.usermodel.Cell> cite = row.cellIterator();
while(cite.hasNext()){
org.apache.poi.ss.usermodel.Cell cell = cite.next();
}
}
}

Row count displayed as -1 while reading downloaded xlsx file in Java

I am trying to read a xlsx file with particular row number which will be provided as parameter.
I am getting NullPointerException if sheet.getRow(RowNum). I am able to read the xlsx file if i adjust column width and save it again manually. But that destroys my purpose of automation. I am able to read any other xlsx files which are created manually.
Here is the sample code :
public String readCouponCode(int getRowCount) {
try {
Registration reg=new Registration();
File inputFile = new File(this.DownloadFile);
System.out.println(DownloadFile);
// Get the workbook instance for XLSX file
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wb.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();
// Traversing over each row of XLSX file
Row row = sheet.getRow(getRowCount);
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int cellIndex = cell.getColumnIndex();
// System.out.println(cellIndex);
if (cellIndex == 0) {
CouponCode = cell.getStringCellValue();
System.out.println(cell.getStringCellValue() + "\t");
}
}
} catch (Exception e) {
System.err.println("Exception :" + e.getMessage());
}
return CouponCode;
}
I also tried with XSSFRow but it yields same result.
Note : I tried by commenting sheet.getRow() line and it prints only the last row. I tried to get number of rows by using sheet.getPhysicalNumberOfRows(), it gives 1 but actually my xlsx file has 7 rows.
Jars used:
dom4j poi-3.13-20150929
poi-excelant-3.13-20150929
poi-ooxml-3.13-20150929
poi-ooxml-schemas-3.13-20150929
xmlbeans-2.5.0

Unable to open hyperlinked files in Excel with JAVA using Apache POI

I'm trying to open all the files that are linked in an Excel Sheet using Apache POI. This is what I've got:
FileInputStream inputstream = new FileInputStream(file);
workbook = new HSSFWorkbook(inputstream);
HSSFSheet sheet = workbook.getSheetAt(0);
log("Processing Sheet: " + sheet.getSheetName());
Iterator rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
HSSFRow row = (HSSFRow) rowIterator.next();
Iterator cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
HSSFCell cell = (HSSFCell) cellIterator.next();
if(cell.getHyperlink() != null){
HSSFHyperlink hyperlink = cell.getHyperlink();
log("Hyperlink found: " + hyperlink.getAddress());
try{
FileInputStream fs = new FileInputStream(hyperlink.getAddress());
}catch(Exception ex){
log(ex.getMessage());
}
}
}
}
But hyperlink.getAddress() doesn't return the correct path, it looks like it returns the relative path but without ../.
I also tried using the getDirectoryRoot() method on the workbook. But that just returns /. I have the path to my Excel file but without the base path that the excel uses or the ../'s in the attachments path, I'm not able to get the correct path.
Instead you can use one easier way: Take a file with that relative address and then get the absolute path of that file.
e.g.
System.out.println(new File(hyperlink.getAddress()).getAbsolutePath());

Apache POI xls file error

I want to read both xls and xlsx file format. It is working fine for xlsx format but I am getting following error while uploading xls file.
Code:
try {
FileInputStream fileInputStream = new FileInputStream("/apps/" + fileName);
//POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
Workbook workBook = WorkbookFactory.create(OPCPackage.open(fileInputStream));
//XSSFWorkbook workBook1 = new XSSFWorkbook();
Sheet ssSheet = workBook.getSheetAt(0);
Iterator rowIterator = ssSheet.rowIterator();
while (rowIterator.hasNext()) {
Row ssRow = (Row) rowIterator.next();
Iterator iterator = ssRow.cellIterator();
List cellTempList = new ArrayList();
while (iterator.hasNext()) {
Cell ssCell = (Cell) iterator.next();
cellTempList.add(ssCell);
}
cellDataList.add(cellTempList);
}
} catch (Exception e) {
e.printStackTrace();
}
Error:
org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:148)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:623)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:230)
Please help.
-Thanks
I think that your problem is due to you trying to construct your workbook with the OPCPackage, even if you use WorkbookFactory. OPCPackage "unzip" your .xlsx in order to be able to read the xml files inside, but this should not work for HSSF since it is a binary file.
My recomendation would be that you use another constructor such as
WorkbookFactory.create(InputStream input)
I guess it should work fine.

Categories