I am attempting to write to an Excel file however I keep getting the error:
Exception in thread \"main\" org.apache.poi.POIXMLException: org.apache.po
CHECK EXCEL FORMAT .... Firstly I created an sample excel with poi and I changed the columns with mine first attempt it give same error but after few try it successfully read .I wonder why it didn't worked first run :( but If you have right library please check the correct excel format
This may happen when your create your XLS/XLSX file through LibreOffice. Apparently something is lost in the conversion and the file is not the same as a spreadsheet made in Microsoft Office. I had the same error and the solution for me was copying all the work I have done in LibreOffice Calc to a MS Excel spreadsheet and then save a new file.
What you have is version mismatch between your Excel file and workbook you are trying to create. The best way to avoid is: choose Interface implementation.
I built on top of Hitesh Sahu's solution:
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
Workbook workbook = null;
// parse files from request
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartDataPointsFile = multipartRequest.getFile("yourFileHere");
try {
if(multipartDataPointsFile!=null) {
String originalFileName= multipartDataPointsFile.getOriginalFilename();
if(originalFileName!=null && originalFileName.length()>0) {
switch (originalFileName.substring(originalFileName.lastIndexOf(".") + 1,
originalFileName.length())) {
case "xls":
try {
workbook = WorkbookFactory.create(multipartDataPointsFile.getInputStream());
}catch(org.apache.poi.openxml4j.exceptions.InvalidFormatException ie){
logger.error("Malformed Excel");
throw new IOException();
}
if(workbook!=null) {
// Do something in here
}else{
logger.error("Could not pass along the workbook");
throw new IOException();
}
case "xlsx":
try {
workbook = WorkbookFactory.create(multipartDataPointsFile.getInputStream());
}catch(org.apache.poi.openxml4j.exceptions.InvalidFormatException ie){
logger.error("Malformed Excel");
throw new IOException();
}
if(workbook!=null) {
// Do something in here
}else{
logger.error("Could not pass along the workbook");
throw new IOException();
}
default:
logger.error("File type is not recognized Excell type");
throw new IOException();
}
}else{
logger.error("Can Not Read File Name");
throw new IOException();
}
}else{
logger.error("Did not select a file");
throw new IOException();
}
} catch (IOException e) {
throw new ApplicationErrorException("Can't parse Excel file");
}
Ok in my case this is how I had it and line3 was throwing this exception:
File xlsxFile = new File( "C:\\myWorkbook.xlsx" );
FileInputStream finXLSX = new FileInputStream( xlsxFile ); //line1
FileOutputStream foutXLSX = new FileOutputStream( xlsxFile ); //line2
XSSFWorkbook workSheet = new XSSFWorkbook( finXLSX ); //line3
But I figured out that line3 wasn't working as I had also opened an output stream on my xlsx file via line2 and then doing line3 was failing. I removed line2 to make it work.
I came across similar issue, finally found that file was having sensitivity --> as confidential, change it to public ..solved my issue. all the best
My excel sheet was encrypted. Deleted the password and it worked.