Word の表は、データの整理や表示に便利な機能です。デフォルトの表は、各列や行に同じ数のセルがありますが、ヘッダーを作成するために複数のセルを結合したり、追加のデータを収容するためにセルを分割したりする必要がある場合もあります。この記事では、Spire.Doc for Java を使用して Word で表のセルを結合または分割する方法を示します。 Word で表のセルを結合する Word で表のセルを分割する Spire.Doc for Java をインストールします まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.7.0</version> </dependency> </dependencies> Word で表のセルを結合する Spire.Doc for .NET では、Table.applyHorizontalMerge() メソッドまたは Table.applyVerticalMerge() メソッドを使用して、隣接する 2 つ以上のセルを水平方向または垂直方向に結合できます。詳細な手順は次のとおりです。 Document インスタンスを作成します。 Document.loadFromFile() メソッドを使用して Word 文書をロードします。 Document.getSections().get() メソッドを使用して、文書内の指定されたセクションを取得します。 Section.addTable() メソッドを使用してセクションに表を追加します。 Table.resetCells(int rowsNum, int columnsNum) メソッドを使用して、表の行数と列数を指定します。 Table.applyHorizontalMerge(int rowIndex, int startCellIndex, int endCellIndex) メソッドを使用して、表内の指定されたセルを水平方向に結合します。 Table.applyVerticalMerge(int columnIndex, int startRowIndex, int endRowIndex) メソッドを使用して、表内の指定されたセルを垂直方向に結合します。 表にデータを追加します。 Document.saveToFile() メソッドを使用して結果文書を保存します。 Java import com.spire.doc.*; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.VerticalAlignment; public class MergeTableCell { public static void main(String[] args) throws Exception { //Documentインスタンスを作成する Document document = new Document(); //Word文書をロードする document.loadFromFile("input.docx"); //最初のセクションを取得する Section section = document.getSections().get(0); //セクションに4 x 4の表を追加する Table table = section.addTable(true); table.resetCells(4, 4); //最初の行のセルを水平方向に結合する table.applyHorizontalMerge(0, 0, 3); //最初の列のセル3と4を垂直方向に結合する table.applyVerticalMerge(0, 2, 3); //表にデータを追加する for (int row = 0; row < table.getRows().getCount(); row++) { for (int col = 0; col < table.getRows().get(row).getCells().getCount(); col++) { TableCell cell = table.get(row, col); cell.getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); Paragraph paragraph = cell.addParagraph(); paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); paragraph.setText("Text"); } } //結果文書を保存する document.saveToFile("MergeTableCells.docx", FileFormat.Docx); } } Word で表のセルを分割する Word セルを複数のセルに分割するために、Spire.Doc for .NET には TableCell.splitCell(int columnNum, int rowNum) メソッドが用意されています。詳細な手順は次のとおりです。 Document インスタンスを作成します。 Document.loadFromFile() メソッドを使用して Word 文書をロードします。 Document.getSection().get() メソッドを使用して、文書内の指定されたセクションを取得します。 Section.getTables().get() メソッドを使用して、セクション内の指定された表を取得します。 Table.getRows().get().getCells().get() メソッドを使用して指定されたセルを取得します。 TableCell.splitCell(int columnNum, int rowNum) メソッドを使用して、セルを分割します。 Document.saveToFile() メソッドを使用して結果文書を保存します。 Java import com.spire.doc.*; public class SplitTableCell { public static void main(String[] args) throws Exception { //Documentインスタンスを作成する Document document = new Document(); //Word文書をロードする document.loadFromFile("MergeTableCells.docx"); //最初のセクションを取得する Section section = document.getSections().get(0); //セクションの最初の表を取得する Table table = section.getTables().get(0); //指定されたセルを取得する TableCell cell1 = table.getRows().get(3).getCells().get(3); //セルを2列2行に分割する cell1.splitCell(2, 2); //結果文書を保存する document.saveToFile("SplitTableCells.docx", FileFormat.Docx); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。