码迷,mamicode.com
首页 > 其他好文 > 详细

poi 导出word,导出图片解决方法

时间:2017-11-16 14:26:15      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:poi 导出word图片

    /**
	 * 写入图片
	 * @param document
	 * @param picName
	 * @param width
	 * @param height
	 * @param alignment 
	 */
	private void WriteImage(CustomXWPFDocument document, String picName, int width, int height, ParagraphAlignment alignment) {
		try {
			CustomXWPFDocument document= new CustomXWPFDocument();
			XWPFParagraph imgPara = document.createParagraph();
			imgPara.setAlignment(alignment);
			int format = XWPFDocument.PICTURE_TYPE_PNG;
			if(picName.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
			else if(picName.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
			else if(picName.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
			else if(picName.endsWith(".jpeg") || picName.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
			else if(picName.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
			else if(picName.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
			else if(picName.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
			else if(picName.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
			else if(picName.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
			else if(picName.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
			else if(picName.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
			XWPFRun stdMarkRun = imgPara.createRun();
			stdMarkRun.addBreak();
			
			String blipId = imgPara.getDocument().addPictureData(new FileInputStream(new File(picName)),format); 
			document.createPicture(blipId,document.getNextPicNameNumber(format),width,height, imgPara);
				
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	*创建自定义Java类继承自XWPFDocument
	*/
	class CustomXWPFDocument extends XWPFDocument {  
	  public CustomXWPFDocument() {  
	    super();  
	  }  
	  
	  public CustomXWPFDocument(InputStream in) throws IOException {  
	    super(in);  
	  }  
	  
	  public CustomXWPFDocument(OPCPackage pkg) throws IOException {  
	    super(pkg);  
	  }  
	  
	  public void createPicture(String blipId, int id, int width, int height,  
	      XWPFParagraph paragraph) {  
	    final int EMU = 9525;  
	    width *= EMU;  
	    height *= EMU;  
	    // String blipId =  
    // getAllPictures().get(id).getPackageRelationship().getId();  
    if (paragraph == null) {  
      paragraph = createParagraph();  
    }  
    CTInline inline = paragraph.createRun().getCTR().addNewDrawing()  
        .addNewInline();  
    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=\"img_"  
        + id  
        + "\"/>"  
        + "            <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("docx_img_ " + id);  
    docPr.setDescr("docx Picture");  
  }  
  
  public void createPictureCxCy(String blipId, int id, long cx, long cy) {  
    CTInline inline = createParagraph().createRun().getCTR()  
        .addNewDrawing().addNewInline();  
    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=\""  
        + cx  
        + "\" cy=\""  
        + cy  
        + "\"/>"  
        + "            </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(cx);  
    extent.setCy(cy);  
    CTNonVisualDrawingProps docPr = inline.addNewDocPr();  
    docPr.setId(id);  
    docPr.setName("docx_img_ " + id);  
    docPr.setDescr("docx Picture");  
  }  
}


本文出自 “BrightAries” 博客,谢绝转载!

poi 导出word,导出图片解决方法

标签:poi 导出word图片

原文地址:http://brightaries.blog.51cto.com/13167995/1982297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!