0

我正在尝试在 XWPFDocument 和合并单元格的帮助下创建以下标题。

下图显示了我正在尝试创建的标题的图像

在此处输入图像描述

*XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable(1, 8);*
CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
borders.addNewBottom().setVal(STBorder.THICK);
borders.addNewLeft().setVal(STBorder.NONE);
borders.addNewRight().setVal(STBorder.NONE);
borders.addNewTop().setVal(STBorder.THICK);
borders.addNewInsideV().setVal(STBorder.THICK);
table.getCTTbl().getTblPr().setTblBorders(borders);
table.setWidth(1440);

for (int col = 0 ; col < 8; col++) {
CTTblWidth tblWidth = table.getRow(0).getCell(col).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(1440));
//STTblWidth.DXA is used to specify width in twentieths of a point.
tblWidth.setType(STTblWidth.DXA);
}    
**mergeCellHorizontally(table, 0, 0, 1);**
XWPFTableCell cell = table.getRow(0).getCell(1);
CTTcPr ctTcPr = cell.getCTTc().getTcPr();
CTTcBorders ctTcBorders = ctTcPr.addNewTcBorders();
ctTcBorders.addNewRight().setVal(STBorder.NONE);
ctTcBorders.addNewBottom().setVal(STBorder.NONE);
4

1 回答 1

0

此类表格问题的第一个问题始终是:表格最多有多少列?或者换句话说:有多少列计算表中列最多的行?如果该答案已知,则应创建具有该最大列数的表。通过设置列宽、单元格边框和/或合并单元格,所有其他的都是可能的。

但是在您的特殊情况下,我看不出合并单元格有什么帮助。假设最大列数为 8,则应该可以通过设置列宽和单元格边框来创建示例表。

例子:

import java.io.File;
import java.io.FileOutputStream;

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class CreateWordTableSpecial {

 static void setColumnWidth(XWPFTable table, int row, int col, int width) {
  CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
  tblWidth.setW(BigInteger.valueOf(width));
  tblWidth.setType(STTblWidth.DXA);
  CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
  if (tcPr != null) {
   tcPr.setTcW(tblWidth);
  } else {
   tcPr = CTTcPr.Factory.newInstance();
   tcPr.setTcW(tblWidth);
   table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
  }
 }

 static void setCellBorders(XWPFTableCell cell, STBorder.Enum[] borderTypesLTRB) {
  CTTcBorders borders = CTTcBorders.Factory.newInstance();
  borders.addNewLeft().setVal(borderTypesLTRB[0]);
  borders.addNewTop().setVal(borderTypesLTRB[1]);
  borders.addNewRight().setVal(borderTypesLTRB[2]);
  borders.addNewBottom().setVal(borderTypesLTRB[3]);
  CTTcPr tcPr = cell.getCTTc().getTcPr();
  if (tcPr != null) {
   tcPr.setTcBorders(borders);
  } else {
   tcPr = CTTcPr.Factory.newInstance();
   tcPr.setTcBorders(borders);
   cell.getCTTc().setTcPr(tcPr);
  }
 }

 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 table
  XWPFTable table = document.createTable(3, 8);
  table.setWidth(1*1440*8); // table width = 8 inches

  //defining the column widths for the grid
  //column width values are in unit twentieths of a point (1/1440 of an inch)
  int defaultColWidth = 1*1440*8/8; // 8 columns fits to 8 inches 

  int[] colunmWidths = new int[] {
   defaultColWidth*3/4, defaultColWidth*2/4, defaultColWidth*8/4, defaultColWidth*3/4, 
   defaultColWidth*3/4, defaultColWidth*2/4, defaultColWidth*8/4, defaultColWidth*3/4
  };

  //create CTTblGrid for this table with widths of the 8 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[0]));
  setColumnWidth(table, 0, 0, colunmWidths[0]);
  //other columns
  for (int col = 1; col < colunmWidths.length; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
   setColumnWidth(table, 0, col, colunmWidths[col]);
  }

  //set cell borders
  for (int col = 0; col < 3; col++) {
   setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[] {STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL}); 
  } 
  setCellBorders(table.getRow(0).getCell(3), new STBorder.Enum[] {STBorder.NIL, STBorder.THICK, STBorder.THICK, STBorder.NIL}); 
  setCellBorders(table.getRow(0).getCell(4), new STBorder.Enum[] {STBorder.THICK, STBorder.THICK, STBorder.NIL, STBorder.NIL}); 
  for (int col = 5; col < 8; col++) {
   setCellBorders(table.getRow(0).getCell(col), new STBorder.Enum[] {STBorder.NIL, STBorder.THICK, STBorder.NIL, STBorder.NIL}); 
  }

  for (int col = 0; col < 3; col++) {
   setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[] {STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK}); 
  } 
  setCellBorders(table.getRow(1).getCell(3), new STBorder.Enum[] {STBorder.NIL, STBorder.NIL, STBorder.THICK, STBorder.THICK}); 
  setCellBorders(table.getRow(1).getCell(4), new STBorder.Enum[] {STBorder.THICK, STBorder.NIL, STBorder.NIL, STBorder.THICK}); 
  for (int col = 5; col < 8; col++) {
   setCellBorders(table.getRow(1).getCell(col), new STBorder.Enum[] {STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.THICK}); 
  }

  for (int col = 0; col < 8; col++) {
   setCellBorders(table.getRow(2).getCell(col), new STBorder.Enum[] {STBorder.NIL, STBorder.NIL, STBorder.NIL, STBorder.NIL}); 
  }

  table.getRow(0).setHeight(28*20); // 28pt row height
  table.getRow(0).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

  XWPFTableCell cell = table.getRow(0).getCell(0);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.setBold(true);
  run.setText("Species:");

  cell = table.getRow(0).getCell(2);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  paragraph.setAlignment(ParagraphAlignment.RIGHT);
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.setBold(true);
  run.setText("Greater silver smelt");

  cell = table.getRow(0).getCell(4);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.setBold(true);
  run.setText("Zone:");

  cell = table.getRow(0).getCell(6);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.setBold(true);
  run.setText("Union and international waters of 1 and 2");

  table.getRow(1).setHeight(28*20); // 28pt row height
  table.getRow(1).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

  cell = table.getRow(1).getCell(2);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  paragraph.setAlignment(ParagraphAlignment.RIGHT);
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.addBreak();
  run = paragraph.createRun();
  run.setItalic(true);
  run.setText("Argentinia silos");

  cell = table.getRow(1).getCell(6);
  paragraph = (cell.getParagraphs().size() > 0)?cell.getParagraphs().get(0):cell.addParagraph();
  paragraph.setIndentationLeft(5*20); // 10pt left indentation
  run = (paragraph.getRuns().size() > 0)?paragraph.getRuns().get(0):paragraph.createRun();
  run.addTab();
  run = paragraph.createRun();
  run.addTab();
  run = paragraph.createRun();
  run.setText("(2)");
  run.addBreak();
  run = paragraph.createRun();
  run.setText("(ARU/1/2)");
 
  paragraph = document.createParagraph();

  CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageSz pageSz = sectPr.addNewPgSz();
  pageSz.setOrient(STPageOrientation.LANDSCAPE);
  pageSz.setH(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
  pageSz.setW(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"

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

 }
}

结果:

在此处输入图像描述

于 2019-11-02T15:33:50.250 回答