java利用poi添加多张图片到word
poi提供的方法是有添加图片到word的,但是并不是兼容所有word。所以需要重写他的方法
第一步,导入jar
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.15</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version></dependency>
第二步,写了一个工具类,重写poi里面的方法的
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; /** *@author POI 导出图片bug修复 * */
public class CustomXWPFDocument extends XWPFDocument { public CustomXWPFDocument(InputStream in) throws IOException { super(in); } /** * */ public CustomXWPFDocument() { super(); // TODO Auto-generated constructor stub} /** *@param pkg *@throws IOException */ public CustomXWPFDocument(OPCPackage pkg) throws IOException { super(pkg); // TODO Auto-generated constructor stub} // picAttch 图片后面追加的字符串 可以是空格public void createPicture(XWPFParagraph paragraph,int id, int width, int height,String picAttch) { final int EMU = 9525; width *= EMU; height *= EMU; String blipId = getAllPictures().get(id).getPackageRelationship() .getId(); CTInline inline = paragraph.createRun().getCTR() .addNewDrawing().addNewInline(); paragraph.createRun().setText(picAttch); 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>"; // CTGraphicalObjectData graphicData =inline.addNewGraphic().addNewGraphicData(); XmlToken xmlToken = null; try { xmlToken = XmlToken.Factory.parse(picXml); } catch (XmlException xe) { xe.printStackTrace(); } inline.set(xmlToken); // graphicData.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("图片" + id); docPr.setDescr(""); }
}
第三步,上核心代码
//新建word文档
CustomXWPFDocument document=new CustomXWPFDocument();
document.addPictureData(inputStream, XWPFDocument.PICTURE_TYPE_JPEG); document.createPicture(document.createParagraph(),document.getAllPictures().size()-1, 400, 400," ");
说明:inputStream代表你要放入word的流
最后
//把word文档写到文件中
File newfile=new File("newfile.docx");
FileOutputStream fos = new FileOutputStream(newfile);
document.write(fos);
虽然写出来了,但是感觉自己还停留在会用,不了解其中为什么要这么用的含义,研究的不深
java利用poi添加多张图片到word相关推荐
- Java利用poi生成word(包含插入图片,动态表格,行合并)
Java利用poi生成word(包含插入图片,动态表格,行合并) 测试模板样式: 图表 1 Word生成结果: 图表 2 需要的jar包:(具体jar可自行去maven下载) Test测试类: imp ...
- java利用POI替换word文档中的标签
java利用POI替换word文档中的标签 <dependency><groupId>org.apache.poi</groupId><artifactId& ...
- java利用poi读取excel_java利用POI读取excel文件的方法
摘要:利用java读取excel文件,读取文件并获取文件中每一个sheet中的值. 一.需要提前导入的包: import java.io.File; import java.io.FileInputS ...
- JAVA 利用poi EXCLE模板文档导出数据
JAVA 利用poi EXCLE模板文档导出数据 1.导入jar包 下载地址:添加链接描述 提取码:xqkg 2.EXCLE模板 3.代码示例 package utill;import java.io ...
- java利用poi导出excel功能-附带图片导出
java利用poi导出excel功能-附带图片导出 写在前面 最近刚离职,闲来无事,于是把上两家公司都有碰到过的需求但都没有去研究实现:即导出带图片的excel报表.于是就折腾了一下这个功能,研究出来 ...
- java利用poi实现Excel考勤报表的输出
java利用poi实现Excel考勤报表的输出 实现效果 SXSSFWorkbook超大数据导出 标题.表头.内容有样式 可以多个sheet(满65535行数据换新的sheet) 一度为快 maven ...
- JAVA Apache POI解析docx格式的word文件并提取带样式文本
关于JAVA Apache POI读取word文档,网上资料很多,但是大多数还是仅仅提取文档中的纯文本,好一点的,也就提取所有图片,但是,word文档本身是具有样式的,这样简单粗暴的提取就会丢失字体. ...
- Java使用POI生成柱状图导出到word文档(柱状图)
本篇文章主要介绍,如何使用Apache POI组件生成柱状图导出到word文档中,具体步骤看下文. 一.实现效果 Java使用POI技术生成柱状图导出到word文档中,最终生成的柱状图如下所示: 二. ...
- Java 利用POI处理Excel的时间格式
问题: 用Java的POI处理Excel中的时间单元格,往往得到的格式不是我们想要的.比如: 按上图中方式设置好后,通过以下代码 XSSFRow row = sheet.getRow(0);Cell ...
最新文章
- 温故(2):pass by value
- latex 常用小结
- Java基础实战Bank项目01-04
- 会议交流 | 如何提升推荐系统的可解释性?——DataFunSummit2022知识图谱在线峰会...
- 根据varchar排列是怎么比大小的_骨架大小怎么看?肩宽、胸腔宽、胯宽是关键,加码大骨架穿搭技巧...
- Linux 用户和组 (1)基本概念
- 五大常用算法之贪心算法
- 专访《白话区块链》作者蒋勇:学习区块链空谈不如实践
- 计算机组成体系结构复习笔记
- 酷我音乐到设备和驱动器_怎么把腾讯视频图标从电脑设备和驱动器里面删除
- python项目实战:实现王者荣耀敏感词汇过滤器
- 卡壳卡壳(是读qia)
- 【SpringBoot教程】SpringBoot开发HTTP接口GET请求实战
- jquery jCal
- JS自定义元素节点/属性的使用 createElement、setAttribute、getAttribute、appendChild
- JavaScript:二维码生成与解析
- 360软件管家和软件开放搜索
- Android 线刷入Recovery.img
- Windows Mobile中使用WinCE驱动调试助手的小技巧
- 分享两个比较好的APP模型设计工具