Word文件导入导出(apache poi)

1.导入jar

     <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>5.0.0</version>
        </dependency>

2.导出

导出有03和07两个版本

    /**
     * @description: file export
     * @param pathName 文件名称  map 需要替换的参数  type 版本类型*/
    public static void exportFile(HttpServletRequest request, HttpServletResponse response, String pathName, Map<String,String> map, String type) throws IOException {
        //文件处理
        String filePath = TEMPLATEPATH+pathName;
        InputStream in = FileUtils.fileToInputStream(new File(filePath));
        //封装参数
        Map<String,String> params = new HashMap<>();
        params.put("${}",map.get(""));
        //选择模板+导出文件
        if (type.equals("03")){
            exportWordFile03(params,in);
        }
        if (type.equals("07")){
            exportWordFile07(params,in);
        }
        FileUtils.expotFile(request,response,new File(filePath),pathName);
    }
    /**
     * @description: export wordFile  word03
*/ public static HWPFDocument exportWordFile03(Map<String,String> map, InputStream in) { HWPFDocument doc = null; try { doc = new HWPFDocument(in); Range range = doc.getRange(); // 对整个word文档进行文本替换 for (Map.Entry<String, String> entry : map.entrySet()) { range.replaceText(entry.getKey(), entry.getValue()); } }catch (IOException e){ e.printStackTrace(); } return doc; } /** * @description: 07*/ public static XWPFDocument exportWordFile07(Map<String,String> map, InputStream in) { XWPFDocument docx = null; try { docx = new XWPFDocument(in); List<XWPFParagraph> paragraphList = docx.getParagraphs(); for(XWPFParagraph paragraph : paragraphList) { // 07版在每个段落依据格式又切分成不同的小的单元runs List<XWPFRun> runs = paragraph.getRuns(); for(XWPFRun run : runs) { // 取出每个run中的文字,在每个run里进行替换 String str = run.getText(run.getTextPosition()); for(Map.Entry<String,String> entry : map.entrySet()) { str = str.replace(entry.getKey(),entry.getValue()); } // 改变每个run里的文字,从里的第0个字符开始替换 run.setText(str,0); } } }catch (IOException e){ e.printStackTrace(); } return docx; }

3.导入

解析到的数据  分隔符:03版本使用  \t  ,07版本使用<br/>,根据业务需求进行相应处理

    /**
     * @description: import wordFile  03  07
     */
    public static StringBuilder importWordFile(MultipartFile multipartFileile) throws Exception {
        File file = FileUtils.multipartFileToFile(multipartFileile);
        StringBuilder content = new StringBuilder();
        try{
            // 03  \t
            HWPFDocument doc = new HWPFDocument(POIFSFileSystem.create(file));
            String documentText = doc.getDocumentText();
            content.append(documentText);
        }catch (OfficeXmlFileException e){
            // 07  <br/>
            XWPFDocument docx = new XWPFDocument(OPCPackage.open(file));
            List<XWPFParagraph> paragraphList = docx.getParagraphs();
            for( int i=0 ;i< paragraphList.size(); i++) {
                if (i != 0){
                    content.append("<br/>");
                }
                content.append(paragraphList.get(i).getText());
            }
        }catch (NotOfficeXmlFileException e){
            throw new Exception(e.getMessage());
        }
        return content;
    }

 

posted @ 2021-11-04 18:02  小辉辉。。  阅读(455)  评论(0)    收藏  举报