当前位置 博文首页 > 立志欲坚不欲锐,成功在久不在速度:spring boot项目整合 Apache

    立志欲坚不欲锐,成功在久不在速度:spring boot项目整合 Apache

    作者:[db:作者] 时间:2021-07-31 08:57

    什么是Apache?POI?

    Apache POI提供API给Java程式对Microsoft Office(Excel、WORD、PowerPoint、Visio等)格式档案读和写的功能

    Apache?POI常用的类:

    • HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
    • XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
    • HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
    • XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
    • HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
    • HDGF - 提供读Microsoft Visio格式档案的功能。
    • HPBF - 提供读Microsoft Publisher格式档案的功能。
    • HSMF - 提供读Microsoft Outlook格式档案的功能。

    今天我们先了解一下POI是如何处理word文档的:

    目前有一个需求:? 现在有一个素材库我们需要用户可以自己通过doc形式导入素材,然后将素材上传到指定位置下,并且将素材的URL地址存储到mysql数据库中,并且在系统中展示出来。

    步骤:一首先导入POI?依赖??

    ?

    <!-- 引入poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>3.17</version>
            </dependency>

    PoiWordController中添加如下方法,解析doc和docx需要使用不同的组件,具体内容见代码注释

    /**
         * 解析一个word文档
         * @param file
         * @return
         * @throws Exception
         */
        @ApiOperation(value="解析word文档")
        @RequestMapping(value = "upload", method = RequestMethod.POST)
        public Map uploadFile(@RequestParam(value = "file", required = true) MultipartFile file){
            //获取文件名字
            String textFileName=file.getOriginalFilename();
            Map wordMap = new LinkedHashMap();//创建一个map对象存放word中的内容
            try {
                //判断文件的扩展名类型
                if(textFileName.endsWith(".doc")){ //判断文件格式
                    //创建文件流
                    InputStream fis = file.getInputStream();
                    HWPFDocument wordExtractor = new HWPFDocument (fis);//使用HWPF组件中WordExtractor类从Word文档中提取文本
                    //提取文本并且给txt
                    String txt=wordExtractor.getDocumentText()
                    
                    fis.close();
                }
                if(textFileName.endsWith(".docx")){
                    File uFile = new File("tempFile.docx");//创建一个临时文件
                    if(!uFile.exists()){
                        uFile.createNewFile();
                    }
                    FileCopyUtils.copy(file.getBytes(), uFile);//复制文件内容
                    OPCPackage opcPackage = POIXMLDocument.openPackage("tempFile.docx");//包含所有POI OOXML文档类的通用功能,打开一个文件包。
                    XWPFDocument document = new XWPFDocument(opcPackage);//使用XWPF组件XWPFDocument类获取文档内容
                    String paras = document.getDocumentText();
                    uFile.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(wordMap);
            return wordMap;
        }

    ?

    ?

    cs