版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java利用poi对word插入文字图片文档末尾rar压缩包内,完整的Java project demo,包含所需的所有jar包,示例文件,使用说明等。三步实现:一、package com.word.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.XWPFPara
2、graph;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.wordprocessingDra
3、wing.CTInline;/* 类名:CustomXWPFDocument* 功能:XWPFDocument读写word.docx* 作者:qiucx 2017年4月12日* 修改记录:* 日期 修改人 &
4、#160; 修改说明*/public class CustomXWPFDocument extends XWPFDocument public CustomXWPFDocument(InputStream in) throws IOException super(in);public CustomXWPFDocument() super();public CustomXWPFDocument(OPCPackage pkg) thr
5、ows IOException super(pkg);/* 函数名: createPicture* 功能 : 在word中标记的位置生成指定路径的图片* 作者 : qiucx 2017年4月12日* 参数表: param id* 参数表: param width 设置图片宽度* 参数表: param height 设置图片高度* 参数表: param paragraph 段落参数 *
6、;返回值: void * 修改记录:* 日期 修改人
7、60; 修改说明*/public void createPicture(int id, int width, int height,XWPFParagraph paragraph) final int EMU = 9525;width *= EMU;height *= EMU;System.out.println("getAllPictures-" + getAllPictures().size();String blipId = getAllPictures().get(id).getPackageRelationship().getId
8、();CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();String picXml = ""+ "<a:graphic xmlns:a="/drawingml/2006/main">"+ "<a:graphicData uri="/drawingml/2006/p
9、icture">"+ "<pic:pic xmlns:pic="/drawingml/2006/picture">"+ "<pic:nvPicPr>" + "<pic:cNvPr id=""+ id+ "" name="Generated"/>"+ "<pic:cNvPicPr/>"+ "&
10、lt;/pic:nvPicPr>"+ "<pic:blipFill>"+ "<a:blip r:embed=""+ blipId+ "" xmlns:r="/officeDocument/2006/relationships"/>"+ "<a:stretch>"+ "<a:fillRect/>"+ "</a:str
11、etch>"+ "</pic:blipFill>"+ "<pic:spPr>"+ "<a:xfrm>"+ "<a:off x="0" y="0"/>"+ "<a:ext cx=""+ width+ "" cy=""+ height+ ""/>"+ "</a:xfrm>"+
12、 "<a:prstGeom prst="rect">"+ "<a:avLst/>"+ "</a:prstGeom>"+ "</pic:spPr>"+ "</pic:pic>"+ "</a:graphicData>" + "</a:graphic>"inline.addNewGraphic().addNewGraphicData();XmlToken xm
13、lToken = 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);CTNonVis
14、ualDrawingProps docPr = inline.addNewDocPr();docPr.setId(id);docPr.setName("图片" + id);docPr.setDescr("测试");二、package com.word.poi;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.List;import java.
15、util.Map;import java.util.Map.Entry;import org.apache.poi.POIXMLDocument;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.xwpf.usermodel.XWPFParagraph;import org.apache.poi.xwpf.usermodel.XWPFRun;import org.apache.poi.xwpf.usermodel.XWPFTable;import org.apache.poi.xwpf.usermodel.
16、XWPFTableCell;import org.apache.poi.xwpf.usermodel.XWPFTableRow;/* 类名:WordUtil* 功能:POI Word读取编辑工具类* 作者:qiucx 2017年4月12日* 修改记录:* 日期
17、160; 修改人 修改说明*/public class WordUtil /* 函数名: generateWord* 功能 : 通用功能处理* 作者 : qiucx 2017年4月12日* 参数表: param param* 参数表: param temp
18、late* 参数表: return * 返回值: CustomXWPFDocument * 修改记录:* 日期 修改人
19、0; 修改说明*/public static CustomXWPFDocument generateWord(Map<String, Object> param, String template) CustomXWPFDocument doc = null; try OPCPackage pack = POIXMLDocument.openPackage(template); doc = new Custo
20、mXWPFDocument(pack); if (param != null && param.size() > 0) /处理段落 List<XWPFParagraph> paragraphList = doc.getParagraphs(); processParagraphs(paragraphList, param, doc); /处理表格 Iterator<XWPFTable> it = doc.getTablesIterator(); while (it.hasNext() XWPFTable table = it.next(); Lis
21、t<XWPFTableRow> rows = table.getRows(); for (XWPFTableRow row : rows) List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) List<XWPFParagraph> paragraphListTable = cell.getParagraphs(); processParagraphs(paragraphListTable, param, doc); catch (Exception
22、 e) e.printStackTrace(); return doc; /* * 函数名: processParagraphs * 功能 : 处理段落 * 作者 : qiucx 2017年4月12日 * 参数表: param paragraphList * 参数表: param param * 参数表: param doc * 返回值: void
23、60; * 修改记录: * 日期 修改人 修改说明 */ publi
24、c static void processParagraphs(List<XWPFParagraph> paragraphList,Map<String, Object> param,CustomXWPFDocument doc) if(paragraphList != null && paragraphList.size() > 0) for(XWPFParagraph paragraph:paragraphList) List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run
25、 : runs) String text = run.getText(0); if(text != null) boolean isSetText = false; for (Entry<String, Object> entry : param.entrySet() String key = entry.getKey(); if(text.indexOf(key) != -1) isSetText = true; Object value = entry.getValue(); if (value instanceof String) /文本替换 text = text.repl
26、ace(key, value.toString(); else if (value instanceof Map) /图片替换 text = text.replace(key, ""); 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
27、("type").toString(); byte byteArray = (byte) pic.get("content"); ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray); try int ind = doc.addPicture(byteInputStream,picType); System.out.println(ind+"-"); doc.createPicture(ind, width , height,paragra
28、ph); catch (Exception e) e.printStackTrace(); if(isSetText) run.setText(text,0); /* * 函数名: getPictureType * 功能 : 根据图片类型,取得对应的图片类型代码 * 作者 : qiucx 2017年4月12日 * 参数表: param picType * 参数表: return * 返回值: int
29、; * 修改记录: * 日期 修改人
30、60;修改说明 */ private static int getPictureType(String picType) int res = CustomXWPFDocument.PICTURE_TYPE_PICT; if(picType != null) if(picType.equalsIgnoreCase("png") res = CustomXWPFDocument.PICTURE_TYPE_PNG; else if(picType.equalsIgnoreCase("dib") res = CustomXWPFDocument.PICTURE_
31、TYPE_DIB; else if(picType.equalsIgnoreCase("emf") res = CustomXWPFDocument.PICTURE_TYPE_EMF; else if(picType.equalsIgnoreCase("jpg") | picType.equalsIgnoreCase("jpeg") res = CustomXWPFDocument.PICTURE_TYPE_JPEG; else if(picType.equalsIgnoreCase("wmf") res = Cu
32、stomXWPFDocument.PICTURE_TYPE_WMF; return res; /* * 函数名: inputStream2ByteArray * 功能 : 将输入流中的数据写入字节数组 * 作者 : qiucx 2017年4月12日 * 参数表: param in * 参数表: param isClose * 参数表: return * 返回值: byte
33、60; * 修改记录: * 日期 修改人 修
34、改说明 */ public static byte inputStream2ByteArray(InputStream in,boolean isClose) byte byteArray = null; try int total = in.available(); byteArray = new bytetotal; in.read(byteArray); catch (IOException e) e.printStackTrace(); finally if(isClose) try in.close(); catch (Exception e2) System.out.println
35、("关闭流失败"); return byteArray; 三、package com.word.poi;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Date;import java.util.HashMap;import java.util.Map;/* 类名:Test* 功能:测试类 指定一些标记的参数,图片路径,文件模板位置,文件输出位置* 作者:qiucx 2017年4月12日* 修改记录:* 日期 &
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 脓毒性休克的护理流程
- 截肢患者营养护理指南下载
- 粗隆骨折患者的睡眠管理
- 城市交通秩序规范承诺书8篇
- 2026年天津市南开区卫生健康系统公开招聘事业单位工作人员(含高层次人才)备考题库及答案详解(新)
- 2025年葫芦岛市建昌县宣传部及社会工作部所属事业单位公开招聘高层次人才备考题库及一套完整答案详解
- 项目时间节点达成及效率承诺书3篇
- 2026年苏州城际铁路有限公司公开招聘备考题库有答案详解
- 海外企业合规经营承诺书8篇
- 2026年金华社发人力资源发展有限公司招聘派遣制工作人员备考题库及答案详解一套
- 2025年度科室护士长工作总结与2026年工作计划
- 酒类进货合同范本
- 江苏省南京市2024-2025学年高一上学期期末学情调研测试物理试卷
- 2026年教师资格之中学综合素质考试题库500道及答案【真题汇编】
- TCEC5023-2020电力建设工程起重施工技术规范报批稿1
- 2025秋国开《人力资源管理理论与实务》形考任务1234参考答案
- 2026年5G网络升级培训课件
- 2025安徽宣城宁国市面向社会招聘社区工作者25人(公共基础知识)综合能力测试题附答案解析
- 农产品营销策略研究国内外文献综述
- 广东省广州市越秀区2024-2025学年上学期期末考试九年级数学试题
- 2025年区域经济一体化发展模式可行性研究报告及总结分析
评论
0/150
提交评论