嵌入式对象(doc / excel)在Java中不可访问

时间:2018-08-02 06:30:21

标签: java spring-mvc apache-poi

  • 以前的参考文献没有多大帮助。
    我想一次又一行地访问一行,即workbook.getAllEmbedds()。
    在调试模式下,内容类型恰好反映为...“ application / vnd.openxmlformats-officedocument.oleObject
    使用选项插入->对象->从文件创建->显示为图标来嵌入文件。
    我知道,“您不应该从客户端上传文件,而是保存在服务器上。”但仍然...

Controller.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<Object> upload(@RequestParam(value = "file") MultipartFile multipartFile) throws Exception{
    ResponseObject responseObject = new ResponseObject();
    // call service class method
    service.excelUpload(multipartFile);
    responseObject.setSuccess(true);
    responseObject.setMessage("Embedded upload successful");
    return  new ResponseEntity(responseObject, HttpStatus.OK);
}

Service.java

    @Override
public void excelUpload(MultipartFile multipartFile) throws IOException, OpenXML4JException {
    InputStream inputStream = multipartFile.getInputStream();
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    List<DAOClass> daoClassList = new ArrayList<>();
    for(Sheet sheet : workbook){
        for (Row row : sheet){
            // Set excel values to DAO class in order to save in DB
            DAOClass daoClass = new DAOClass();
            for (Cell cell : row){
                if(cell.getColumnIndex() == 0){
                    daoClass.setID(cell.getNumericValue());
                }else if(cell.getColumnIndex() == 1){
                    daoClass.setName(cell.getStringCellValue());
                }else if(cell.getColumnIndex() == 2){
                    daoClass.setAddress(cell.getStringCellValue());
                }else if(cell.getColumnIndex() == 3){

                    // ***Here I need help!!!***

                    PackagePart pPart = (PackagePart) workbook.getAllEmbedds();
                    String contentType = pPart.getContentType();
                    // Excel Workbook - either binary or OpenXML
                    if (contentType.equals("application/vnd.ms-excel")) {
                        HSSFWorkbook binaryEmbeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
                        System.out.println("binaryEmbeddedWorkbook: "+binaryEmbeddedWorkbook);
                    }
                    // Excel Workbook - OpenXML file format
                    else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
                        OPCPackage docPackage = OPCPackage.open(pPart.getInputStream());
                        XSSFWorkbook xmlEmbeddedWorkbook = new XSSFWorkbook(docPackage);
                        System.out.println("xmlEmbeddedWorkbook: "+xmlEmbeddedWorkbook);
                    }
                    // Word Document - binary (OLE2CDF) file format
                     else if (contentType.equals("application/msword")) {
                        HWPFDocument hwpfDocument = new HWPFDocument(pPart.getInputStream());
                         System.out.println("hwpfDocument: "+hwpfDocument);
                    }
                    // Word Document - OpenXML file format
                    else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                        OPCPackage docPackage = OPCPackage.open(pPart.getInputStream());
                        XWPFDocument xwpfDocument = new XWPFDocument(docPackage);
                         System.out.println("xwpfDocument: "+xwpfDocument);
                    }
                }else{
                    continue;
                }
            }
            daoClassList.add(daoClass);
        }
    }
}

0 个答案:

没有答案