【问题标题】:how to set specific cell width in different row in apache poi table?如何在apache poi表的不同行中设置特定的单元格宽度?
【发布时间】:2019-03-22 19:16:14
【问题描述】:

我想在 Apache POI 表的不同行中设置特定的单元格宽度。

我正在使用下面的代码来设置单元格宽度,但它会改变整个列的宽度。有什么办法可以解决这个问题吗?

CTTblWidth width = cell1.getCTTc().addNewTcPr().addNewTcW();
width.setType(STTblWidth.DXA);
width.setW(BigInteger.valueOf(500));

我想要这种风格:

【问题讨论】:

  • 要在Word中建立这样的表,要么必须合并某些列,要么必须在表格网格中设置列跨越。见stackoverflow.com/questions/34647624/…
  • 嗨@AxelRichter,我也使用了合并单元格和隐藏边框。但我被困在 row(2).cell(0) 中。请看这张图片link。正如您在该链接中看到的, row(2).cell(0) 宽度与 row(0)cell(0) 相同。我想减小 row(2).cell(0) 的宽度。但是改变特定的单元格宽度总是影响整列。还有其他替代方法吗?
  • 首先确定网格中所需列的总数。为此,请在整个表格中绘制所有垂直线。如果我数得正确,您将需要 8 列。在第一行中,第一个单元格跨越前三列,第二个单元格跨越接下来的 4 列,第三个单元格是第八列。在第二行中,第一个单元格跨越 7 列,第二个单元格是第八列。在第三行中,第一个单元格是第一列,第二个单元格跨越第二到第四列,第三个单元格是第五列,第四个单元格是第六列,第五个单元格是第七列,第六个单元格是第八列。

标签: java apache-poi


【解决方案1】:

Word 文档中生成如此复杂的表格需要了解Word 表格的内部结构。

首先确定网格中所需列的总数。为此,请在整个表格中绘制所有垂直线。对于您的示例表,您将需要 8 列。

在第一行中,第一个单元格跨越前三列,第二个单元格跨越接下来的 4 列,第三个单元格是第八列。

在第二行中,第一个单元格跨越 7 列,第二个单元格是第八列。

在第三行和第四行中,第一个单元格是第一列,第二个单元格跨越第二到第四列,第三个单元格是第五列,第四个单元格是第六列,第五个单元格是第七列,第六个单元格是第八列。

在第五行,所有单元格都被合并了。

...

一旦您知道每行中的单个单元格需要如何跨越网格,您就可以确定网格列的列宽。在下面的示例中,默认网格列是 6/8 英寸宽。所以 8 列适合 6 英寸的页面宽度。

现在您可以使用给定的示例 How to colspan a table in word with APACHE POI 来设置列跨度。

在执行此操作时,您需要注意每次合并还会删除不再需要的列。当多次合并一行中的列时,删除后必须重新计算新的列索引。

合并后生成的列的列宽需要设置。

例子:

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

import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

public class CreateWordTableMerge {

 static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
  for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
   XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
   CTVMerge vmerge = CTVMerge.Factory.newInstance();
   if(rowIndex == fromRow){
    // The first merged cell is set with RESTART merge value
    vmerge.setVal(STMerge.RESTART);
   } else {
    // Cells which join (merge) the first one, are set with CONTINUE
    vmerge.setVal(STMerge.CONTINUE);
    // and the content should be removed
    for (int i = cell.getParagraphs().size(); i > 0; i--) {
     cell.removeParagraph(0);
    }
    cell.addParagraph();
   }
   // Try getting the TcPr. Not simply setting an new one every time.
   CTTcPr tcPr = cell.getCTTc().getTcPr();
   if (tcPr == null) tcPr = cell.getCTTc().addNewTcPr();
   tcPr.setVMerge(vmerge);
  }
 }

 //merging horizontally by setting grid span instead of using CTHMerge
 static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
  XWPFTableCell cell = table.getRow(row).getCell(fromCol);
  // Try getting the TcPr. Not simply setting an new one every time.
  CTTcPr tcPr = cell.getCTTc().getTcPr();
  if (tcPr == null) tcPr = cell.getCTTc().addNewTcPr();
  // The first merged cell has grid span property set
  if (tcPr.isSetGridSpan()) {
   tcPr.getGridSpan().setVal(BigInteger.valueOf(toCol-fromCol+1));
  } else {
   tcPr.addNewGridSpan().setVal(BigInteger.valueOf(toCol-fromCol+1));
  }
  // Cells which join (merge) the first one, must be removed
  for(int colIndex = toCol; colIndex > fromCol; colIndex--) {
   table.getRow(row).getCtRow().removeTc(colIndex);
   table.getRow(row).removeCell(colIndex);
  }
 }

 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);
  }
 }

 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(9,8);

  for (int row = 0; row < 9; row++) {
   for (int col = 0; col < 8; col++) {
    table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
   }
  }

  //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*6/8; // 8 columns fits to 6 inches 
  int[] colunmWidths = new int[] {
   defaultColWidth*5/4, defaultColWidth*3/4, defaultColWidth*1/4, defaultColWidth*1/4, 
   defaultColWidth*3/2, defaultColWidth, defaultColWidth, defaultColWidth*2
  };

  //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]));
  //other columns
  for (int col = 1; col < colunmWidths.length; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(colunmWidths[col]));
  }

  //using the merge methods and setting the column widths
  mergeCellHorizontally(table, 0, 0, 2); 
  setColumnWidth(table, 0, 0, colunmWidths[0]+colunmWidths[1]+colunmWidths[2]);
  mergeCellHorizontally(table, 0, (3-2), (6-2)); // merge grid cols 3 to 6 but 2 cols are removed already 
  setColumnWidth(table, 0, 1, colunmWidths[3]+colunmWidths[4]+colunmWidths[5]+colunmWidths[6]);
  setColumnWidth(table, 0, 2, colunmWidths[7]);

  mergeCellHorizontally(table, 1, 0, 6); 
  setColumnWidth(table, 1, 0, colunmWidths[0]+colunmWidths[1]+colunmWidths[2]+colunmWidths[3]
                              +colunmWidths[4]+colunmWidths[5]+colunmWidths[6]);
  setColumnWidth(table, 1, 1, colunmWidths[7]);

  mergeCellHorizontally(table, 2, 1, 3); 
  setColumnWidth(table, 2, 0, colunmWidths[0]);
  setColumnWidth(table, 2, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]);
  setColumnWidth(table, 2, 2, colunmWidths[4]);
  setColumnWidth(table, 2, 3, colunmWidths[5]);
  setColumnWidth(table, 2, 4, colunmWidths[6]);
  setColumnWidth(table, 2, 5, colunmWidths[7]);

  mergeCellHorizontally(table, 3, 1, 3); 
  setColumnWidth(table, 3, 0, colunmWidths[0]);
  setColumnWidth(table, 3, 1, colunmWidths[1]+colunmWidths[2]+colunmWidths[3]);
  setColumnWidth(table, 3, 2, colunmWidths[4]);
  setColumnWidth(table, 3, 3, colunmWidths[5]);
  setColumnWidth(table, 3, 4, colunmWidths[6]);
  setColumnWidth(table, 3, 5, colunmWidths[7]);

  mergeCellHorizontally(table, 4, 0, 7); 
  setColumnWidth(table, 4, 0, colunmWidths[0]+colunmWidths[1]+colunmWidths[2]+colunmWidths[3]
                              +colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);

  mergeCellHorizontally(table, 5, 0, 4); 
  mergeCellHorizontally(table, 5, (5-4), (7-4)); // merge grid cols 5 to 7 but 4 cols are removed already 
  setColumnWidth(table, 5, 0, colunmWidths[0]+colunmWidths[1]+colunmWidths[2]+colunmWidths[3]+colunmWidths[4]);
  setColumnWidth(table, 5, 1, colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);

  mergeCellHorizontally(table, 6, 0, 1); 
  mergeCellHorizontally(table, 6, (2-1), (4-1)); // merge grid cols 2 to 4 but 1 cols are removed already 
  mergeCellHorizontally(table, 6, (5-3), (6-3)); // merge grid cols 5 to 6 but 3 cols are removed already 
  setColumnWidth(table, 6, 0, colunmWidths[0]+colunmWidths[1]);
  setColumnWidth(table, 6, 1, colunmWidths[2]+colunmWidths[3]+colunmWidths[4]);
  setColumnWidth(table, 6, 2, colunmWidths[5]+colunmWidths[6]);
  setColumnWidth(table, 6, 3, colunmWidths[7]);

  mergeCellHorizontally(table, 7, 0, 1); 
  mergeCellHorizontally(table, 7, (2-1), (4-1)); 
  mergeCellHorizontally(table, 7, (5-3), (6-3)); 
  setColumnWidth(table, 7, 0, colunmWidths[0]+colunmWidths[1]);
  setColumnWidth(table, 7, 1, colunmWidths[2]+colunmWidths[3]+colunmWidths[4]);
  setColumnWidth(table, 7, 2, colunmWidths[5]+colunmWidths[6]);
  setColumnWidth(table, 7, 3, colunmWidths[7]);

  mergeCellHorizontally(table, 8, 0, 7); 
  setColumnWidth(table, 8, 0, colunmWidths[0]+colunmWidths[1]+colunmWidths[2]+colunmWidths[3]
                              +colunmWidths[4]+colunmWidths[5]+colunmWidths[6]+colunmWidths[7]);

  paragraph = document.createParagraph();

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

 }
}

结果:

【讨论】:

    猜你喜欢
    • 2014-01-20
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多