poi-word模版替换:再续前缘

988 阅读2分钟

这短短的一生我们最终都会失去
你不妨大胆一些,爱一个人,攀一座山,追一个梦


    接上一版之后,主要记录一下关于poi常见类的解析以及关于word模版指定位置的图片替换,所有内容均为网络加上楼主的理解写的,如有错误,欢迎指正,评论下见。对所造成的影响概不负责,只是记录。

🔗 码云仓库

Poi 常见类解析

XWPFDocument document //word文档
XWPFParagraph xp   //段落
XWPFRun run  //段落里的最小单位

CTPPr ppr = xp.getCTP().getPPr();  //段落的各种属性(段前后磅数,行数,间距)
CTRPr rpr = run.getCTR().getRPR();   //run的各种属性
CTFonts 字体 rpr.getRFonts()
CTHpsMeasure 字体大小 rpr.getSz()  rpr.getSzCs()  //2个不知道为啥
CTHighlight  字突出显示文本 rpr.getHighlight()
CTUnderline 下划线 rpr.getU()   rpr.getU().setColor() //下划线颜色
CTShd  底纹  rpr.getShd()

run.setBold(isBlod);  加粗
run.setItalic(isItalic);  斜体
run.setStrike(isStrike);  中划线
run.setColor(colorVal);  字体颜色

段落由run组成,getRuns获得所有的run。每个run按照顺序排好。insertNewRun(int pos)插入一个新的run,参数pos插入的位置,原位置会往后移一位

参考链接:blog.csdn.net/liuqz2009/a…

指定位置图片替换

道理同替换文本一致,只是替换的类型是图片而已。 图片参数传入的是InputStream流,附带有图片的长宽格式, 理念是复制一份原有的,同时增加修改,之后删除旧的内容

参考链接:blog.csdn.net/jeff2007/ar…

                    //从传入的参数中获取替换的值
                    Object value = parametersMap.get(key.toString());
	            if(value instanceof Map) {
						//要替换的为图片,这里定义的是图片实用map类型传入的
			Map pic = (Map)value;  
                        int width = Integer.parseInt(pic.get("width").toString());  
                        int height = Integer.parseInt(pic.get("height").toString());  
                        int picType = getPictureType(pic.get("type").toString());  
                        byte[] byteArray = (byte[]) pic.get("content");  
                        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);  
                        try {  
// insertNewRun.addPicture(byteInputStream, picType, "", width, height);
                            String ind = document.addPictureData(byteInputStream,picType);  
                            document.createPicture(ind,document.getNextPicNameNumber(picType), width , height,insertNewRun);  
                        } catch (Exception e) { 
                            e.printStackTrace();  
                        }
                        //图片下方添加下划线
                        CTRPr rPr = insertNewRun.getCTR().getRPr();
                        CTUnderline u = rPr.isSetU() ? rPr.getU() : rPr.addNewU(); 
                        u.setVal(STUnderline.Enum.forInt(1));
                        xWPFParagraph.removeRun(beginRunIndex + 1);
                    }else {
						//文字替换
					insertNewRun.setText(getValueBykey(key.toString(),parametersMap));
					xWPFParagraph.removeRun(beginRunIndex + 1);
					}

根据得到的参数类型(由获取模版内的参数得到)决定替换的类型
Map类型的图片参数

	//图片map
	Map<String,Object> header = new HashMap<String, Object>();
        header.put("width", 150);  
        header.put("height", 70);  
        header.put("type", "png");  
        header.put("content", WordUtil.inputStream2ByteArray(new FileInputStream("d:\\1111.png"), true));  
        parametersMap.put("header",header); 

添加图片的步骤

  1. 将图片添加进文档的图片数据并得到图片的索引
String ind = document.addPictureData(byteInputStream,picType);
  1. 在指定run(insertNewRun)里创建指定图片(ind),该方法需自己手写,之后该run就是一个图片run
document.createPicture(ind,document.getNextPicNameNumber(picType), width , height,insertNewRun);

效果展示
模板 模板 输出 输出