java - 如何有选择地将边框应用于 Apache POI 中的 docx 表?

标签 java apache-poi docx

在 Microsoft Word 中,如果您转到表格的属性,然后转到“边框和底纹”部分,您将看到可以在表格的 8 个方面应用边框。 上、下、左、右、垂直中心、水平中心、左对角线和右对角线

如何使用 POI 有选择地打开它们?

enter image description here

最佳答案

在当前的apache poi 4.1.0中,类XWPFTable为此提供了方法。

例如XWPFTable.setTopBorder :

public void setTopBorder(XWPFTable.XWPFBorderType type,
                         int size,
                         int space,
                         java.lang.String rgbColor)

Set Top borders for table

Parameters:
    type - - XWPFTable.XWPFBorderType e.g. single, double, thick
    size - - Specifies the width of the current border. The width of this border is 
             specified in measurements of eighths of a point, with a minimum value of two 
             (onefourth of a point) and a maximum value of 96 (twelve points). 
             Any values outside this range may be reassigned to a more appropriate value.
    space - - Specifies the spacing offset that shall be used to place this border 
              on the table
    rgbColor - - This color may either be presented as a hex value (in RRGGBB format), 
                 or auto to allow a consumer to automatically determine the border color 
                 as appropriate. 

参见XWPFTable.XWPFBorderType可能的边框类型。

完整示例:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableBorders {

 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  //create the table
  XWPFTable table = document.createTable(3,3);
  table.setWidth("100%");
  for (int row = 0; row < 3; row++) {
   for (int col = 0; col < 3; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
   }
  }

  //set borders
  table.setTopBorder(XWPFTable.XWPFBorderType.THICK_THIN_LARGE_GAP, 32, 0, "FF0000");
  table.setBottomBorder(XWPFTable.XWPFBorderType.THICK_THIN_LARGE_GAP, 32, 0, "FF0000");
  table.setLeftBorder(XWPFTable.XWPFBorderType.THICK_THIN_LARGE_GAP, 32, 0, "FF0000");
  table.setRightBorder(XWPFTable.XWPFBorderType.THICK_THIN_LARGE_GAP, 32, 0, "FF0000");
  table.setInsideHBorder(XWPFTable.XWPFBorderType.DOT_DASH, 16, 0, "00FF00");
  table.setInsideVBorder(XWPFTable.XWPFBorderType.DOTTED, 24, 0, "0000FF");

  paragraph = document.createParagraph();

  FileOutputStream fileOut = new FileOutputStream("create_table.docx"); 
  document.write(fileOut);
  fileOut.close();
  document.close();
 }
}

关于java - 如何有选择地将边框应用于 Apache POI 中的 docx 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58450113/

相关文章:

java - 当 App Engine (Java) 启动新实例时,如何确保任务队列 Memcache cron 在它们之间共享?

java - 数组未正确更新,因为 "show array"类对象已在另一个类中初始化

java - 反射中的父类(super class)构造函数问题

java - JOptionPane showMessageDialog - 取消焦点 'OK' 按钮

java - 在java中读取Excel文件不会读取空白单元格或将其计为空

java - 无法使用 Apache POI 3.14 读取 XLSX 文件

java - 使用 Apache POI 时在数据驱动框架场景中出现异常

java - 在 Java 中以编程方式将 HTML/MXML 文件转换为 Word 文档

linux - 将 HTML 转换为 odt、doc、docx

ruby-on-rails - 使用 ruby​​ 在浏览器中显示 docx 或 ppt