poi导入图片到word
2015-06-09 14:06
337 查看
package com.inspur.mng.project.config.action; 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.poi.xwpf.usermodel.XWPFRun; 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;
// <span style="font-family: Arial, Helvetica, sans-serif;">getAllPictures()方法可以获得所有图片的属性</span> 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(""); } }
调用方法如下,该方法是从网盘上下载,如果不需要可以直接传地址
CustomXWPFDocument doc = new CustomXWPFDocument();
XWPFParagraph paragraph = null; paragraph = doc.createParagraph(); String filePath = cname; response.reset(); // 清空buffer response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); filePath = java.net.URLEncoder .encode(filePath, "UTF-8"); // 从网盘下载到tomcat下,图片从网盘上下载的 downloadFile(businesstype, cname); String fileName = cname; // System.out.println("中文文件名279行fileName------------"+fileName); String root = null; if (root == null) { root = request.getRealPath("/");// 服务器路径 } String dir = "assets\\data\\"; response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("UTF-8"); java.io.BufferedInputStream bis = null; java.io.BufferedOutputStream bos = null; String url = root + dir + fileName; // 改成下载到tomcat下的路径,url为图片路径 File image1 = new File(url); String fname = image1.getName();
//图片后缀 String prefix = fileName.substring(fname .lastIndexOf(".") + 1); int format = 0; if (prefix.equals("jpg") || prefix.equals(".jpeg")) { format = XWPFDocument.PICTURE_TYPE_JPEG; } else if (prefix.equals("gif")) { format = XWPFDocument.PICTURE_TYPE_GIF; } else if (prefix.equals("bmp")) { format = XWPFDocument.PICTURE_TYPE_BMP; } else if (prefix.equals("png")) { format = XWPFDocument.PICTURE_TYPE_PNG; } else if (prefix.equals("pict")) { format = XWPFDocument.PICTURE_TYPE_PICT; } else if (prefix.equals("wmf")) { format = XWPFDocument.PICTURE_TYPE_WMF; } else if (prefix.equals("wpg")) { format = XWPFDocument.PICTURE_TYPE_WPG; } FileInputStream fis = new FileInputStream(image1); byte[] bytes = IOUtils.toByteArray(fis);
//添加图片 自带的方法,add方法将图片添加到doc中,此时可以通过获得所有图片的一个方法看到所有图片的属性,包括id doc.addPictureData(bytes, format);
//构造图片调用上面的方法 参数分别为段落,<strong>第几张图片</strong>,宽度,高度,名称 doc.createPicture(paragraph,<strong> 0</strong>, 550, 800, filePath); fis.close(); // 删除本地文件 File file = new File(url); if (file.exists()) { file.delete(); }
相关文章推荐
- Plus One
- 再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果
- Leetcode[169]-Majority Element
- 大约ActionContext.getContext()使用体验
- 程序员保值的4个秘密
- 基于外部数据的用户信用评估
- POJ 1496
- C学习脚印
- CentOS系统的VPS安全设置与优化
- rpmbulid学习redis打包
- 模拟客户查看cdn节点的访问情况
- ABAP 上传、下载请求文件
- kindEditor,html编辑器用法与体会,及如何获取文本域中的值 (转)
- Directx11基于GeometryShader的粒子系统
- 网络取证原理与实战
- Linux rpm 命令参数使用详解
- CSS Precedence
- POJ 1477
- MySQL数据表格导入导出
- Visual C++学习笔记2:对话框布局操作