1. 程式人生 > 其它 >Word檔案匯入匯出(apache poi)

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;
    }

本文來自部落格園,作者:小輝輝。。,轉載請註明原文連結:https://www.cnblogs.com/zjylsh/p/15509467.html