“Package should contain a content type part [M1.13]”

前端 未结 11 1225
野趣味
野趣味 2020-12-19 02:36

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

相关标签:
11条回答
  • 2020-12-19 02:57

    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

    0 讨论(0)
  • 2020-12-19 03:00

    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.

    0 讨论(0)
  • 2020-12-19 03:04

    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");
    }
    
    0 讨论(0)
  • 2020-12-19 03:04

    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.

    0 讨论(0)
  • 2020-12-19 03:04

    I came across similar issue, finally found that file was having sensitivity --> as confidential, change it to public ..solved my issue. all the best

    0 讨论(0)
  • 2020-12-19 03:05

    My excel sheet was encrypted. Deleted the password and it worked.

    0 讨论(0)
提交回复
热议问题