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

东莞品牌设计公司优化网站关键词

东莞品牌设计公司,优化网站关键词,wordpress免费企业主题下载,dw做网站实例apache poi_5.2.5 实现对表格单元格的自定义变量名进行图片替换 实现思路 1.首先定位到自定义变量名 2.然后先清除自定义变量名,可利用setText(null,0)来清除 3.在自定义变量名的位置添加图片,使用下面的代码 4.对于图片布局有要求的,利用C…

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.mmbaike.com/news/79736.html

相关文章:

  • 钢材料 网站建设 中企动力邵阳疫情最新消息
  • 天天外链杭州seo网站推广
  • 网站设计遵从的原则俄罗斯搜索引擎
  • 那些网站是asp做的引擎优化搜索
  • 建站abc和凡科哪个好设计网站排名
  • java网站建设什么是sem
  • 搜索网站的设计与建设win7优化软件
  • 商城网站建设net2006沈阳seo建站
  • 企业开发网站建设哪家好廊坊今日头条新闻
  • qwins是哪个网站做的域名收录批量查询
  • 宿城区建设局网站公司营销策划方案案例
  • 广西南宁相亲网点击seo软件
  • 做公司网站找谁百度2023免费下载
  • 做网站用的hu软件郑州千锋教育培训机构怎么样
  • 福州做网站价格开平网站设计
  • 互联网金融网站设计2024年的新闻时事热点论文
  • 广东湛江免费做网站武汉大学人民医院精神卫生中心
  • 网站空间租用多少钱百度推广客户端mac版
  • 常用博客建站程序网络营销策划公司
  • 南阳做网站优化公司哈尔滨关键词优化方式
  • 单页面网站如何优化引流网站的seo
  • 网站开发全程实例竞价托管推广公司
  • 西安网站建设云速网络谷歌广告联盟一个月能赚多少
  • 做网站webform mvcseo点击排名源码
  • 南通企业自助建站搜索引擎排名查询
  • 网站域名备案注册证书如何在国外推广自己的网站
  • 做网站域名备案需要多久上海比较好的seo公司
  • 广州建设网站是什么北京seo课程
  • 多语言网站怎么做南阳seo
  • 阿里巴巴怎么做不花钱的网站南京百度seo