poi-生成word和读取word

    <!--poi word-->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.0.0</version>
        </dependency>
package com.dxy.study_0628.util.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.TextRenderData;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.Map;

/**
 * 生成word工具类
 */
public class WordUtil {
    
    
    /**
     * 生成word工具类
     * @param datas 数据
     * @param mbpath 模板地址
     * @param descpath 输出地址
     * @return 返回生成文件路径
     * @throws IOException
     */
    public static String toword(Map datas,String mbpath,String descpath) throws IOException {
    
    
        String filePath = mbpath;
        String outPath = descpath;
        XWPFTemplate template = XWPFTemplate.compile(filePath)
                .render(datas);
        FileOutputStream out = new FileOutputStream(outPath);
        template.write(out);
        out.flush();
        out.close();
        template.close();
        return descpath;
    }

    /**
     * 读docx
     * @param path
     * @return
     */
    public static String readDocx(String path)
    {
    
    
        //都是只能用String,不能用Stringbuffer,还不知道原因
        String text = null;
        try {
    
    
            InputStream is = new FileInputStream(path);
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            text = extractor.getText();
        } catch (IOException e) {
    
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return text;
    }

    public static void main(String[] args) throws IOException, OpenXML4JException, XmlException {
    
    
        //生成word
       /* Map<String, Object> datas = new HashMap<>();
        datas.put("Name", new TextRenderData("33ff33", "Alex Bin"));
        datas.put("Age", "24");
        WordUtil.toword(datas,"D:\\springmvc\\study_0628\\src\\main\\resources\\doc\\TL.docx","D:\\springmvc\\study_0628\\src\\main\\resources\\doc\\outTo你好.docx");
      */
       /* String s = WordUtil.readDocx("D:\\springmvc\\study_0628\\src\\main\\resources\\doc\\outTo你好.docx");
        System.out.println(s);*/

        //得到.docx文件提取器
        XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLDocument.openPackage("D:\\springmvc\\study_0628\\src\\main\\resources\\doc\\outTo你好.docx"));
        //提取.docx正文文本
        String text = docx.getText();
        System.out.println(text);
    }

}

https://www.cnblogs.com/fqh2020/p/14675105.html
https://blog.csdn.net/qq_21137441/article/details/79226171

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/121304355