当前位置: 首页 > news >正文

apache poi_5.2.5 实现对表格单元格的自定义变量名进行图片替换

apache poi_5.2.5 实现对表格单元格的自定义变量名进行图片替换

实现思路

1.首先定位到自定义变量名
2.然后先清除自定义变量名,可利用setText(null,0)来清除
3.在自定义变量名的位置添加图片,使用下面的代码
4.对于图片布局有要求的,利用CTAnchor进行实现
addNewWrapNone;//根据setBehindDoc,来确定浮于文字上方还是下方
addNewWrapSquare;//四周型布局
【注:默认是嵌入型,文字会遮挡部分图片。】

依赖包

		<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>5.2.5</version></dependency>

代码实现

 public static void main(String[] args) throws Exception {String wenshuUrl = "D:\\demo1.docx";File file = new File(wenshuUrl);byte[] bytes = Files.readAllBytes(Paths.get(file.toURI()));ByteArrayInputStream in = new ByteArrayInputStream(bytes);XWPFDocument doc = new XWPFDocument(in);String pic = "D:\\demo1.png";File file2 = new File(pic);byte[] bytes2 = Files.readAllBytes(Paths.get(file2.toURI()));ByteArrayInputStream bis2 = new ByteArrayInputStream(bytes2);for (int i = 0; i < doc.getTables().size(); i++) {for (int rowIndex = 0; rowIndex < doc.getTables().get(i).getRows().size(); rowIndex++) {XWPFTableRow row = doc.getTables().get(i).getRow(rowIndex);row.getTableCells().stream().forEach(cell -> {for (XWPFParagraph paragraph : cell.getParagraphs()) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {try {addPicture(run, bis2,pic,500,400);} catch (InvalidFormatException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);} catch (SAXException e) {throw new RuntimeException(e);} catch (XmlException e) {throw new RuntimeException(e);}}}});}}OutputStream output = new FileOutputStream("D:\\afteradd1.doc");doc.write(output);output.close();}/*** 添加图片(布局环绕模式)* @param run* @param stream 图片流* @param filename 文件路径* @param width 图片的宽度* @param height  图片的高度*/private static void addPicture(XWPFRun run, InputStream stream, String filename, int width, int height ) throws InvalidFormatException, IOException, SAXException, XmlException {run.addPicture(stream, getPictureTypeByFileName(filename), filename, Units.toEMU(width), Units.toEMU(height));CTDrawing drawing = run.getCTR().getDrawingArray(0);CTGraphicalObject graphicalObject = drawing.getInlineArray(0).getGraphic();//这个是relationId,即图片位置的idlong id = drawing.getInlineArray(0).getDocPr().getId();//设置一个浮动模式CTAnchor anchor = drawing.addNewAnchor();//创建一个包含浮动模式的xml字符串String xml = "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\"><a:graphicData uri=\"" + CTPicture.type.getName().getNamespaceURI() + "\"><pic:pic xmlns:pic=\"" + CTPicture.type.getName().getNamespaceURI() + "\" /></a:graphicData></a:graphic>";//将xml字符串转换为InputSource对象InputSource is = new InputSource(new StringReader(xml));//生成Document对象,用于将Document doc = DocumentHelper.readDocument(is);anchor.set(XmlToken.Factory.parse(doc.getDocumentElement(),DEFAULT_XML_OPTIONS));//设置浮动位置,0表示紧贴文字anchor.setDistT(0L);anchor.setDistR(0L);anchor.setDistB(0L);anchor.setDistL(0L);anchor.setLocked(false);anchor.setLayoutInCell(true);anchor.setSimplePos2(false);anchor.setAllowOverlap(true);anchor.setRelativeHeight(0);//图片的布局环绕模式anchor.addNewWrapSquare();CTPosH ctPosH = anchor.addNewPositionH();//STRelFromH 水平方向的位置相对于谁进行调整 character:文字ctPosH.setRelativeFrom(STRelFromH.CHARACTER);//调整水平方向的位置,从左往右递增,0代表紧贴文字ctPosH.setPosOffset( Units.toEMU(0));CTPosV ctPosV = anchor.addNewPositionV();//STRelFromV 垂直方向的位置相对于谁进行调整 PARAGRAPH:字段ctPosV.setRelativeFrom(STRelFromV.PARAGRAPH);//调整垂直方向的位置,从上往下数值递增ctPosV.setPosOffset( Units.toEMU(-10));CTPoint2D ctPoint2D = anchor.addNewSimplePos();ctPoint2D.setX(0);ctPoint2D.setY(0);anchor.addNewCNvGraphicFramePr();CTNonVisualDrawingProps docPr = anchor.addNewDocPr();docPr.setId(id);docPr.setName("Drawing " + id);docPr.setDescr(filename);CTPositiveSize2D extent = anchor.addNewExtent();extent.setCx(Units.toEMU(width));extent.setCy(Units.toEMU(height));anchor.setGraphic(graphicalObject);//添加浮动属性drawing.setAnchorArray(new CTAnchor[]{anchor});//删除行内属性drawing.removeInline(0);}public static int getPictureTypeByFileName(String fileName) {if (StringUtils.isEmpty(fileName)) {throw new RuntimeException("文件名称不能为空!");}String suffix = fileName.substring(fileName.lastIndexOf("."));switch (suffix) {case ".jpg":case ".jpeg":return XWPFDocument.PICTURE_TYPE_JPEG;case ".wmf":return XWPFDocument.PICTURE_TYPE_WMF;case ".png":case ".PNG":return XWPFDocument.PICTURE_TYPE_PNG;case ".gif":return XWPFDocument.PICTURE_TYPE_GIF;case ".tiff":return XWPFDocument.PICTURE_TYPE_TIFF;case ".bmp":return XWPFDocument.PICTURE_TYPE_BMP;default:throw new RuntimeException("不支持的文件类型:" + suffix);}}
http://www.lryc.cn/news/264436.html

相关文章:

  • Kafka--Kafka日志索引详解以及生产常见问题分析与总结
  • Vue3-23-组件-依赖注入的使用详解
  • css 美化滚动条
  • Tomcat介绍及使用:构建强大的Java Web应用服务器
  • 怎么定义一套完成标准的JAVA枚举类型
  • Apache Seatunnel本地源码构建编译运行调试
  • 构建高效持久层:深度解析 MyBatis-Plus(02)
  • Gitlab仓库推送到Gitee仓库的一种思路
  • 快速能访问服务器的文件
  • Diary26-Vue综合案例1-书籍购物车
  • 【EasyExcel实践】万能导出,一个接口导出多张表以及任意字段(可指定字段顺序)-简化升级版
  • 解决 Hive 外部表分隔符问题的实用指南
  • 一文学会 Apache Zeppelin
  • ROS学习笔记(七)---参数服务器
  • 【RTOS学习】源码分析(信号量和互斥量 事件组 任务通知)
  • 1316:【例4.6】数的计数(Noip2001) 代码+解析
  • 征集倒计时 | 2023年卓越影响力榜单-第四届中国产业创新奖报名即将截止
  • vue的语法模板与数据绑定的说明
  • VueCron使用方法
  • SpringBlade export-user SQL 注入漏洞复现
  • 结构体的一些补充知识
  • 20V升26V 600mA升压型LED驱动芯片,PWM调光芯片-AH1160
  • 如何在Go中制作HTTP服务器
  • Linux笔记---系统信息
  • 最新版android stuido加上namespace
  • Wireshark基础及捕获技巧
  • Windows下Navicat15.0连接Oracle11g报ORA-28547解决
  • 21 Vue3中使用v-for遍历对象数组
  • 深入理解Java自定义异常与全局异常处理 @RestControllerAdvice
  • h5页面跳转微信小程序(最简单的方法|URL Scheme)