java使用poi操作word插入图片、段落、表格

  • 其他链接
  • 准备工作
    • 创建word模板.docx文件
    • 编写模板格式.xml文件
  • java上手poi
      • maven依赖
      • 使用到的包
    • 具体应用
      • 对应封装方法 ----------图片插入指定位置操作
        • 图片操作使用到的工具类
      • 对应封装方法 ----------表格操作
        • 表格操作用到的工具类
      • 对应封装方法 ----------段落内容替换操作
        • 段落内容操作用到的工具类
    • 效果图
    • 如果想设置单独的样式

其他链接

java使用poi操作world生成饼图,柱状图,折线图,组合图:一
java使用poi操作world生成饼图,柱状图,折线图,组合图:二
问题链接

准备工作

创建word模板.docx文件

使用模板操作,因为自己使用poi去写word过于麻烦,耦合性太高

${XXX}理解成占位符,会替换为填充的数据,可配置为其他比如*{xxx}*

编写模板格式.xml文件

创建的.docx文件顺序基本是乱的,需要另存为.xml格式手动更改格式

用编译器打开(我用的IDEA)找到要替换的占位符的位置

把所有要替换的位置都找到改为一行


以此类推改完所有替换内容的标识

改完之后在改成.docx格式

java上手poi

maven依赖

        <dependency><groupId>org.apache.xmlbeans</groupId><artifactId>xmlbeans</artifactId><version>2.6.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.16</version></dependency>

使用到的包

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

具体应用

 /*** 读取模板* @param HttpServletResponse * @throws Exception*/
public void operatorWord(HttpServletResponse response) throws Exception {//获得模板文件InputStream docis = new FileInputStream("C:\Users\16630\Desktop\模板A.docx");//转成wordCustomXWPFDocument document = new CustomXWPFDocument(docis);//写入图片this.insertImage(document);//写入表格this.insertTable(document);//段落替换对象this.insertText(document)//把doc输出到输出流response.setCharacterEncoding("UTF-8");response.setContentType("application/msword");//文件名String fileName = "考试成绩分析" + htmlDataTime.getStartData();response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));ServletOutputStream responseOutputStream = response.getOutputStream();document.write(responseOutputStream);responseOutputStream.flush();responseOutputStream.close();in.close();}

对应封装方法 ----------图片插入指定位置操作

 /*** 写入图片在word中* @param document* @throws IOException* @throws InvalidFormatException*/private void insertImage(CustomXWPFDocument document) throws IOException, InvalidFormatException {//图片FileInputStream in = new FileInputStream(new("C:\Users\16630\Desktop\image.jpg"));//段落集合List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {//获取到段落中的所有文本内容String text = paragraph.getText();//判断此段落中是否有需要进行替换的文本if (WordUtil.checkText(text)) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {//替换模板原来位置String key = "${image}";if (run.toString().indexOf(key) != -1) {byte[] ba = new byte[in.available()];int len = in.read(ba);ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba, 0, len);//设置图片document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_PNG);//创建一个word图片,并插入到文档中-->像素可改document.createPicture(document.getAllPictures().size() - 1, 240, 240,paragraph);}break;}break;}}}

图片操作使用到的工具类


/*** @author BM_hyjw*/
import java.io.IOException;
import java.io.InputStream;import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;public class CustomXWPFDocument extends XWPFDocument{public CustomXWPFDocument(InputStream in) throws IOException {super(in);}public CustomXWPFDocument() {super();}public CustomXWPFDocument(OPCPackage pkg) throws IOException {super(pkg);}/*** @param id* @param width*            宽* @param height*            高* @param paragraph*            段落*/public void createPicture(int id, int width, int height,XWPFParagraph paragraph) {final int EMU = 9525;width *= EMU;height *= EMU;String blipId = super.getRelationId(super.getAllPictures().get(id));CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();String picXml = ""+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"+ "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"+ "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"+ "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""+ id+ "\" name=\"Generated\"/>"+ "            <pic:cNvPicPr/>"+ "         </pic:nvPicPr>"+ "         <pic:blipFill>"+ "            <a:blip r:embed=\""+ blipId+ "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"+ "            <a:stretch>"+ "               <a:fillRect/>"+ "            </a:stretch>"+ "         </pic:blipFill>"+ "         <pic:spPr>"+ "            <a:xfrm>"+ "               <a:off x=\"0\" y=\"0\"/>"+ "               <a:ext cx=\""+ width+ "\" cy=\""+ height+ "\"/>"+ "            </a:xfrm>"+ "            <a:prstGeom prst=\"rect\">"+ "               <a:avLst/>"+ "            </a:prstGeom>"+ "         </pic:spPr>"+ "      </pic:pic>"+ "   </a:graphicData>" + "</a:graphic>";inline.addNewGraphic().addNewGraphicData();XmlToken xmlToken = null;try {xmlToken = XmlToken.Factory.parse(picXml);} catch (XmlException xe) {xe.printStackTrace();}inline.set(xmlToken);inline.setDistT(0);inline.setDistB(0);inline.setDistL(0);inline.setDistR(0);CTPositiveSize2D extent = inline.addNewExtent();extent.setCx(width);extent.setCy(height);CTNonVisualDrawingProps docPr = inline.addNewDocPr();docPr.setId(id);docPr.setName("图片名称");docPr.setDescr("描述信息");}
}

对应封装方法 ----------表格操作

    private void insertTable(CustomXWPFDocument document) {//创建表格接受参数-->外层list是行内层是列List<List<String>> tableList = new ArrayList();List<String> cells = new ArrayList<>();cells.add("1");cells.add("张三");cells.add("100");tableList.add(cells);List<String> cellList = new ArrayList<>();cellList.add("2");cellList.add("李四");cellList.add("10");tableList.add(cellList);//获取表格位置List<XWPFTable> tables = document.getTables();WordUtil.insertTable(tables.get(0),tableList);}

表格操作用到的工具类

    /*** 为表格插入数据,行数不够添加新行** @param table     需要插入数据的表格* @param tableList 插入数据集合*/public static void insertTable(XWPFTable table, List<List<String>> tableList) {//创建行,根据需要插入的数据添加新行,不处理表头for (int i = 1; i <= tableList.size(); i++) {table.createRow();}//遍历表格插入数据List<XWPFTableRow> rows = table.getRows();for (int i = 1; i < rows.size(); i++) {XWPFTableRow newRow = table.getRow(i);List<XWPFTableCell> cells = newRow.getTableCells();for (int j = 0; j < cells.size(); j++) {XWPFTableCell cell = cells.get(j);cell.setText(tableList.get(i - 1).get(j));//表格样式一致-->没有此段表格会默认左对齐//有此段会使表格格式一致CTTc cttc = cell.getCTTc();CTTcPr ctPr = cttc.addNewTcPr();ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);}}}

对应封装方法 ----------段落内容替换操作

    private void insertText(CustomXWPFDocument document) {//声明替换模板对象Map textMap = new HashMap();textMap.put("${maxName}","张三");textMap.put("${maxScore}", "100");textMap.put("${minName}","李四");textMap.put("${minScore}", "10");//替换模板数据WordUtil.changeText(document,textMap);}

段落内容操作用到的工具类

        /*** 替换段落文本** @param document docx解析对象* @param textMap  需要替换的信息集合*/public static void changeText(XWPFDocument document, Map<String, Object> textMap) {//获取段落集合List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {//获取到段落中的所有文本内容String text = paragraph.getText();//判断此段落中是否有需要进行替换的文本if (checkText(text)) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {//替换模板原来位置run.setText(changeValue(run.toString(), textMap), 0);}}}}/*** 判断文本中是否包含$** @param text 文本* @return 包含返回true, 不包含返回false*/public static boolean checkText(String text) {boolean check = false;if (text.indexOf("$") != -1) {check = true;}return check;}/*** 替换模板${}*/private static Object changeValue(String value, Map<String, Object> textMap) {Set<Map.Entry<String, Object>> textSets = textMap.entrySet();Object valu = "";for (Map.Entry<String, Object> textSet : textSets) {// 匹配模板与替换值 格式${key}String key = textSet.getKey();if (value.contains(key)) {valu = textSet.getValue();}}return valu;}

效果图

如果想设置单独的样式

比如加颜色、居中等,要确保.xml中替换单位为一行

效果

java使用poi操作word模板,插入图片、段落、表格相关推荐

  1. Java利用poi生成word(包含插入图片,动态表格,行合并)

    Java利用poi生成word(包含插入图片,动态表格,行合并) 测试模板样式: 图表 1 Word生成结果: 图表 2 需要的jar包:(具体jar可自行去maven下载) Test测试类: imp ...

  2. java读写word模板_Java操作Word模板插入列表

    Java操作Word模板插入列表 通过poi操作word,插入各种编号的列表,包括数字,字母,罗马字符,自定义任意字符.下面的示例就用了音乐符♬. 1.制作模板,插入列表使用语法*,模板内容为{{*n ...

  3. java word apache poi 操作word模板。

    apache poi 操作word模板. 操作方式: 1.对于固定格,可以遍历格子然后替换其中指定的值例如在要替换的cell写入${example} 这样格式,遍历到之后替换. 2.对于需要增长的表格 ...

  4. java使用poi在word模板中替换柱状图、折线图、饼图、表格、文本、图片

    介绍 java使用poi在word模板中替换柱状图.折线图.饼图.表格.文本.图片 软件架构 安装教程 环境搭建:jdk1.8.0_291.maven1.8.0_291 IDEA工具:IntelliJ ...

  5. 使用java Apache poi 根据word模板生成word报表

    使用java Apache poi 根据word模板生成word报表 使用poi读取word模板,替换word中的{text}标签,并根据自定义标签循环生成表格或表格中的行. 代码示例下载:https ...

  6. apache poi使用例_使用java Apache poi 根据word模板生成word报表例子

    [实例简介] 使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持. 使用说明:https://b ...

  7. java使用poi读写word中的图片(二)

    文章目录 准备工作 简单读取 复杂读取 查看Word的XML 特别说明:Word中的Svg图片 第一种写入图片到Word中的方式 第二种写入图片到Word中的方式 最后 准备工作 这里就不在复述了,可 ...

  8. java使用poi替换word模板

    poi 操作word文档 0. 参考文档 感谢大萌音音's B站视频的分享, 如有不懂, 可以去看这个视频 1. poi 依赖 <dependency><groupId>org ...

  9. Java使用poi根据word模板进行导出(有效可行)

    最近一直在做与导出这部分相关的事情,有一个就是需要将数据库中查询到的信息写入到word模板中再进行导出.下面的代码直接复制使用即可,没啥毛病,无论是段落中的,还是表格中的,都是有效的.使用过程中遇到的 ...

最新文章

  1. Service IntentService区别 (面试)
  2. 华为交换机 路由器 常用命令
  3. ITK:提取二值图像中连接区域的边界
  4. 官宣!又一所新大学来了!
  5. 单片机ADC采样算法----平均值采样法
  6. S1:动态方法调用:call apply
  7. 旋转校正原理_【干货】全站仪水准器的检校原理及方法,值得学习!
  8. 有人利用两个SaltStack 漏洞攻击思科 VIRL-PE 基础设施
  9. 测井数据的聚类算法分段分层及图例
  10. 卷积神经网络的基本原理
  11. 信息安全技术国家相关标准一览表
  12. ubuntu开机后nvidia驱动突然消失,nvidia显卡驱动卸载与安装
  13. 分析社会热点与网络营销的关联
  14. java盘盈盘亏_反映财产物资的盘盈、盘亏和毁损情况,应当设( )科目。
  15. 完美解决VS2003.Net fatal error LNK1201: 写入程序数据库“.pdb”时出错 - 细雨淅淅
  16. 使用Python来模拟鼠标的点击; 模拟键盘
  17. 【openMV or openCV】
  18. 访问GitLab 返回502错误
  19. 解密「UWB」精准定位黑科技
  20. 金蝶K3WISE13.1销售发票不能删除

热门文章

  1. 【人工智能毕设之基于深度学习+pyqt的老照片修复系统-哔哩哔哩】 https://b23.tv/t5kVJrZ
  2. NTFS文件系统详解(一)硬盘基本信息
  3. 小白理财初级训练之经验总结
  4. 怎样把word转换成pdf文件
  5. 给Swagger换个新皮肤
  6. 我的第一篇IT(C语言)博客文章
  7. Esri Compressed Geometry 解码
  8. 用javaScript编写lrc歌词解析器
  9. Java小游戏之捕鱼达人000.序章
  10. RabbitMQ学习篇——(二)Rabbit安装完后,启动出现闪退问题解决!