zoukankan      html  css  js  c++  java
  • 通过poi解析word(替换word中的部分内容)

    解析03版(.doc)word:

    HWPFDocument doc=null;              //HWPFDocument 对应03版word
            try {
                 doc =new HWPFDocument(new FileInputStream("F:\upload\template_03.doc"));    //获得要替换的word模板
                 Range range = doc.getRange();                             //range获取word中的内容
                for (Map.Entry<String,String> entry:replaceContent.entrySet()){          //通过一个Map来替换内容 Map中key值存被替换的内容  Map中value值存要替换的内容,最后通过一个循环实现
                    range.replaceText(entry.getKey(),entry.getValue());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return doc;

    解析07(.docx) word:

    XWPFDocument docx=null;
            try {
                docx=new XWPFDocument(new FileInputStream("F:\upload\template_07.docx"));
                List<XWPFParagraph> paragraphs = docx.getParagraphs();                //07版需先获取段落;最后在获取以格式分割的最小单位run
                for (XWPFParagraph paragraph:paragraphs){
                    List<XWPFRun> runs = paragraph.getRuns();
                    for (XWPFRun run:runs){
                        String str=run.getText(run.getTextPosition());          //获取run中的字符串
                        for (Map.Entry<String,String> entry:replaceContent.entrySet()){
                            str=str.replace(entry.getKey(),entry.getValue());
                        }
                        run.setText(str,0);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return docx;
  • 相关阅读:
    window.open打开页面及页面大小设置
    java--->Stack的使用
    java简单实现直接运算表达式
    idea--->tomcat控制台乱码
    多线程--->线程的几种基本实现
    java--->读取wsdl方法(二)
    java--->wsdl的简单使用(spring+cxf)
    ActiveMQ(学习1)
    基本类型和包装类型的区别
    弄懂JDK、JRE和JVM到底是什么
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12099714.html
Copyright © 2011-2022 走看看