POI在Word文档插入表格,表格中插入图片总结-蒲公英云

一、引入相关jar

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi-ooxml</artifactId>
  4. <version>3.10-FINAL</version>
  5. </dependency>

二、原始写法

1)、在首页插入一个表格,单元格中带有图片

  1. public static void writeTblWithImageToDocx_1() {
  2. BufferedReader in = null;
  3. XWPFDocument temp = null;
  4. BufferedOutputStream out = null;
  5. File tempDoc = new File("d:\\test\\test11.docx");
  6. try {
  7. //in = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\test\\2.doc"), "ISO8859_1"));
  8. temp = new XWPFDocument(new BufferedInputStream(new FileInputStream(tempDoc)));
  9. out = new BufferedOutputStream(new FileOutputStream("D:\\test\\test_2.docx"));
  10. XWPFParagraph p = temp.getParagraphArray(0);
  11. p.setAlignment(ParagraphAlignment.LEFT);
  12. XWPFRun run = p.insertNewRun(0);
  13. //表格生成 6行5列.
  14. int rows = 6;
  15. int cols = 5;
  16. XmlCursor cursor = p.getCTP().newCursor();
  17. XWPFTable tableOne = temp.insertNewTbl(cursor);
  18. //样式控制
  19. CTTbl ttbl = tableOne.getCTTbl();
  20. CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
  21. CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
  22. CTJc cTJc = tblPr.addNewJc();
  23. cTJc.setVal(STJc.Enum.forString("center"));//表格居中
  24. tblWidth.setW(new BigInteger("9000"));//每个表格宽度
  25. tblWidth.setType(STTblWidth.AUTO);
  26. //表格创建
  27. XWPFTableRow tableRowTitle = tableOne.getRow(0);
  28. tableRowTitle.getCell(0).setText("标题");
  29. tableRowTitle.addNewTableCell().setText("内容");
  30. tableRowTitle.addNewTableCell().setText("姓名");
  31. tableRowTitle.addNewTableCell().setText("日期");
  32. tableRowTitle.addNewTableCell().setText("备注");
  33. for (int i = 1; i < rows; i++) {
  34. XWPFTableRow createRow = tableOne.createRow();
  35. for (int j = 0; j < cols; j++) {
  36. createRow.getCell(j).setText("我是第"+i+"行,第"+(j+1)+"列");
  37. }
  38. }
  39. //插入图片测试
  40. XWPFTableRow rowTest = tableOne.getRow(0);
  41. XWPFTableCell imageCell = rowTest.getCell(0);
  42. List<XWPFParagraph> paragraphs = imageCell.getParagraphs();
  43. XWPFParagraph newPara = paragraphs.get(0);
  44. XWPFRun imageCellRunn = newPara.createRun();
  45. imageCellRunn.addPicture(new FileInputStream("d:/test/1.png"), XWPFDocument.PICTURE_TYPE_PNG, "1.png", Units.toEMU(600), Units.toEMU(300));
  46. run.addBreak();
  47. temp.write(out);
  48. } catch (UnsupportedEncodingException e) {
  49. // TODO 自动生成的 catch 块
  50. e.printStackTrace();
  51. } catch (FileNotFoundException e) {
  52. // TODO 自动生成的 catch 块
  53. e.printStackTrace();
  54. } catch (IOException e) {
  55. // TODO 自动生成的 catch 块
  56. e.printStackTrace();
  57. } catch (InvalidFormatException i) {
  58. // TODO 自动生成的 catch 块
  59. i.printStackTrace();
  60. }
  61. finally {
  62. if (in != null) {
  63. try {
  64. in.close();
  65. } catch (IOException e) {
  66. // TODO 自动生成的 catch 块
  67. e.printStackTrace();
  68. }
  69. }
  70. if (out != null) {
  71. try {
  72. out.close();
  73. } catch (IOException e) {
  74. // TODO 自动生成的 catch 块
  75. e.printStackTrace();
  76. }
  77. }
  78. // tempDoc.deleteOnExit();
  79. }
  80. System.out.println("写入完成。。。。。。。。。。。。。。");
  81. }

2)、调用该方法

  1. public static void main(String[] args) {
  2. writeTblWithImageToDocx_1();
  3. }

3)、结果

WPS:图片是空白的图,无法打开,表格宽度样式无效

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3htMzkzMzkyNjI1_size_16_color_FFFFFF_t_70

Office:无法打开

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3htMzkzMzkyNjI1_size_16_color_FFFFFF_t_70 1

三、修改代码支持可显示图片

Poi代码bug,亲测3.9和3.10都有该问题,其他版本未测,可自行测试。

修改后代码:

1)、首先重写XWPFDocument,定义构造函数,自定义读取图片的方法

  1. public class CustomXWPFDocument extends XWPFDocument {
  2. public CustomXWPFDocument(InputStream inputStream) throws IOException {
  3. super(inputStream);
  4. }
  5. public void createPic(String blipId, int id, int width, int height, CTInline inline) {
  6. final int EMU = 9525;
  7. width *= EMU;
  8. height *= EMU;
  9. //String blipId = getAllPictures().get(id).getPackageRelationship().getId();
  10. //CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
  11. String picXml = "" +
  12. "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
  13. " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
  14. " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
  15. " <pic:nvPicPr>" +
  16. " <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
  17. " <pic:cNvPicPr/>" +
  18. " </pic:nvPicPr>" +
  19. " <pic:blipFill>" +
  20. " <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
  21. " <a:stretch>" +
  22. " <a:fillRect/>" +
  23. " </a:stretch>" +
  24. " </pic:blipFill>" +
  25. " <pic:spPr>" +
  26. " <a:xfrm>" +
  27. " <a:off x=\"0\" y=\"0\"/>" +
  28. " <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
  29. " </a:xfrm>" +
  30. " <a:prstGeom prst=\"rect\">" +
  31. " <a:avLst/>" +
  32. " </a:prstGeom>" +
  33. " </pic:spPr>" +
  34. " </pic:pic>" +
  35. " </a:graphicData>" +
  36. "</a:graphic>";
  37. //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
  38. XmlToken xmlToken = null;
  39. try {
  40. xmlToken = XmlToken.Factory.parse(picXml);
  41. } catch (XmlException xe) {
  42. xe.printStackTrace();
  43. }
  44. inline.set(xmlToken);
  45. //graphicData.set(xmlToken);
  46. inline.setDistT(0);
  47. inline.setDistB(0);
  48. inline.setDistL(0);
  49. inline.setDistR(0);
  50. CTPositiveSize2D extent = inline.addNewExtent();
  51. extent.setCx(width);
  52. extent.setCy(height);
  53. CTNonVisualDrawingProps docPr = inline.addNewDocPr();
  54. docPr.setId(id);
  55. docPr.setName("Picture " + id);
  56. docPr.setDescr("Generated");
  57. }
  58. }

2)、修改首页插入一个表格,单元格中带有图片的相关代码

  1. public static void writeTblWithImageToDocx_2() {
  2. BufferedReader in = null;
  3. CustomXWPFDocument temp = null;
  4. BufferedOutputStream out = null;
  5. File tempDoc = new File("d:\\test\\test11.docx");
  6. try {
  7. //in = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\test\\2.doc"), "ISO8859_1"));
  8. temp = new CustomXWPFDocument(new BufferedInputStream(new FileInputStream(tempDoc)));
  9. out = new BufferedOutputStream(new FileOutputStream("D:\\test\\test_2.docx"));
  10. XWPFParagraph p = temp.getParagraphArray(0);
  11. p.setAlignment(ParagraphAlignment.LEFT);
  12. XWPFRun run = p.insertNewRun(0);
  13. //表格生成 6行5列.
  14. int rows = 6;
  15. int cols = 5;
  16. XmlCursor cursor = p.getCTP().newCursor();
  17. XWPFTable tableOne = temp.insertNewTbl(cursor);
  18. //样式控制
  19. CTTbl ttbl = tableOne.getCTTbl();
  20. CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
  21. CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
  22. CTJc cTJc = tblPr.addNewJc();
  23. cTJc.setVal(STJc.Enum.forString("center"));//表格居中
  24. tblWidth.setW(new BigInteger("8000"));//每个表格宽度
  25. tblWidth.setType(STTblWidth.DXA);
  26. //表格创建
  27. XWPFTableRow tableRowTitle = tableOne.getRow(0);
  28. tableRowTitle.setHeight(380);
  29. tableRowTitle.getCell(0).setText("标题");
  30. tableRowTitle.addNewTableCell().setText("内容");
  31. tableRowTitle.addNewTableCell().setText("姓名");
  32. tableRowTitle.addNewTableCell().setText("日期");
  33. tableRowTitle.addNewTableCell().setText("备注");
  34. for (int i = 1; i < rows; i++) {
  35. XWPFTableRow createRow = tableOne.createRow();
  36. for (int j = 0; j < cols; j++) {
  37. createRow.getCell(j).setText("我是第"+i+"行,第"+(j+1)+"列");
  38. }
  39. }
  40. //插入图片测试
  41. XWPFTableRow rowTest = tableOne.getRow(0);
  42. XWPFTableCell imageCell = rowTest.getCell(0);
  43. List<XWPFParagraph> paragraphs = imageCell.getParagraphs();
  44. XWPFParagraph newPara = paragraphs.get(0);
  45. XWPFRun imageCellRunn = newPara.createRun();
  46. String id = temp.addPictureData(new FileInputStream("d:/test/1.png"), XWPFDocument.PICTURE_TYPE_PNG);//添加图片数据
  47. int id2=temp.getAllPackagePictures().size()+1;
  48. CTInline ctinline=imageCellRunn.getCTR().addNewDrawing().addNewInline();//设置段落行
  49. temp.createPic(id,id2, 259, 259,ctinline);//添加图片
  50. mergeCellsHorizontal(tableOne,0,0,1);//WPS不支持跨列
  51. mergeCellsVertically(tableOne,1,1,2);
  52. run.addBreak();
  53. temp.write(out);
  54. } catch (UnsupportedEncodingException e) {
  55. // TODO 自动生成的 catch 块
  56. e.printStackTrace();
  57. } catch (FileNotFoundException e) {
  58. // TODO 自动生成的 catch 块
  59. e.printStackTrace();
  60. } catch (IOException e) {
  61. // TODO 自动生成的 catch 块
  62. e.printStackTrace();
  63. } catch (InvalidFormatException i) {
  64. // TODO 自动生成的 catch 块
  65. i.printStackTrace();
  66. }
  67. finally {
  68. if (in != null) {
  69. try {
  70. in.close();
  71. } catch (IOException e) {
  72. // TODO 自动生成的 catch 块
  73. e.printStackTrace();
  74. }
  75. }
  76. if (out != null) {
  77. try {
  78. out.close();
  79. } catch (IOException e) {
  80. // TODO 自动生成的 catch 块
  81. e.printStackTrace();
  82. }
  83. }
  84. // tempDoc.deleteOnExit();
  85. }
  86. System.out.println("写入完成。。。。。。。。。。。。。。");
  87. }

3)、新增合并单元格相关代码

  1. // word跨列合并单元格
  2. public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
  3. for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
  4. XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
  5. if ( cellIndex == fromCell ) {
  6. // The first merged cell is set with RESTART merge value
  7. cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
  8. } else {
  9. // Cells which join (merge) the first one, are set with CONTINUE
  10. cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
  11. }
  12. }
  13. }
  14. // word跨行并单元格
  15. public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
  16. for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
  17. XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
  18. if ( rowIndex == fromRow ) {
  19. // The first merged cell is set with RESTART merge value
  20. cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
  21. } else {
  22. // Cells which join (merge) the first one, are set with CONTINUE
  23. cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
  24. }
  25. }
  26. }

3)、测试结果

WPS:图片已经正常显示,样式依旧无效,合并列也无效,合并行有效

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3htMzkzMzkyNjI1_size_16_color_FFFFFF_t_70 2

Office:可以正常显示

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3htMzkzMzkyNjI1_size_16_color_FFFFFF_t_70 3


原网址: 访问
创建于: 2023-05-08 16:43:12
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论