ここでは、JavaアプリケーションでWordドキュメント内の表のセルの結合と分割を紹介します
セルの結合:
Spire.Doc結合セルは、水平結合と垂直結合の2つに分類されます。水平結合の場合applyVerticalMerge(int a , int a ,int a) 列の指定、結合したい開始行と終了行. 垂直結合時に、applyHorizontalMerge(int a, int a, int a) 行の指定、結合したい開始列、終了列を指定します。
import com.spire.doc.*;
public class MergeTableCell {
public static void main(String[] args) throws Exception {
String output = "output/MergeTableCells.docx";
//新しいwordの例
Document document = new Document();
// 4*4のテーブルを追加します
Section section = document.addSection();
Table table = section.addTable(true);
table.resetCells(4, 4);
//水平結合
table.applyHorizontalMerge(0, 0, 3);
// 垂直結合
table.applyVerticalMerge(0, 2, 3);
// 文書を保存
document.saveToFile(output, FileFormat.Docx);
}
}
セルの分割:
セルを分割するには、まずセルを取得し、セルの分割された列の数と行の数を指定します。
import com.spire.doc.*;
public class SplitTableCell {
public static void main(String[] args) throws Exception {
String output = "output/SplitTableCells.docx";
//新しいwordの例
Document document = new Document();
//4*4のテーブルを追加
Section section = document.addSection();
Table table = section.addTable(true);
table.resetCells(4, 4);
//セルの分割
table.getRows().get(3).getCells().get(3).splitCell(2, 2);
//文書を保存
document.saveToFile(output, FileFormat.Docx);
}
}