poi word 表格插入图片

可能3.8,3.9是存在你所说的图片插入表格无法显示的问题,但是我现在用的是4.0.1,已经没有改问题,用的run.addpicture()方法;可以正常显示,正在思考如何做样式。

  1.  
    File outFile = new File("D:\\computer-b63781b9\\cloud\\Desktop\\create_table.docx");
  2.  
    FileInputStream in = new FileInputStream(new File("D:\\data\\guns-lite\\runtime\\upload\\08#A101#IMG_20180220_190720R.jpg"));
  3.  
    FileOutputStream out = new FileOutputStream(outFile);
  4.  
    XWPFDocument docx = new XWPFDocument();
  5.  
     
  6.  
    XWPFTable table = docx.createTable(1,1);
  7.  
    XWPFTableRow row = table.getRow(0);
  8.  
    XWPFTableCell cell = row.getCell(0);
  9.  
    XWPFParagraph newPara = cell.addParagraph();
  10.  
    XWPFRun imageCellRunn = newPara.createRun();
  11.  
    XWPFPicture pic = imageCellRunn.addPicture(in, XWPFDocument.PICTURE_TYPE_PNG, "1.png", Units.toEMU(200), Units.toEMU(200));
  12.  
    imageCellRunn.addBreak();
  13.  
     
  14.  
    in.close();
  15.  
    docx.write(out);
  16.  
    out.close();
posted @ 2021-01-09 13:56  天涯海角路  阅读(978)  评论(0)    收藏  举报