2017-02-15 3 views
1

私はJavaで単語ファイルを作成している小さなプロジェクトに取り組み、この単語ファイルに詳細を入力します。 私はワードファイルを作成することができ、それにデータを入力することもできます。私はまた、単語ファイルにテーブルを書き、いくつかの詳細を入力します。JAVAによる単語ファイルの表の列幅を増やす方法

私が欲しいのは、特定の列の幅を広げたいと思っています。

これを実行する方法はありますか?私はワードファイルを作成し、そこにデータを書き込むためにApache POIドライバを使用しています。

私は、コードの下に使用しています:

XWPFDocument document= new XWPFDocument(); 
try{ 
FileOutputStream out = new FileOutputStream(
new File("d:\\createparagraph.docx")); 

XWPFParagraph paragraph = document.createParagraph(); 
    XWPFTable table = document.createTable(); 
    XWPFTableRow tableRowOne = table.getRow(0); 
    tableRowOne.getCell(0).setText("CLientID"); 
    tableRowOne.addNewTableCell().setText(txtCID.getText()); 
    //create second row 
    XWPFTableRow tableRow2 = table.createRow(); 
    tableRow2.getCell(0).setText("AccountID"); 
    tableRow2.getCell(1).setText(txtAID.getText()); 
document.write(out); 
out.close(); 
} 

このコードは正常に動作し、通常のテーブルを生成するが、私は、特定の列(2列目)の幅を増やすことをお勧めします。

助けてください。

答えて

3

私の知る限り、列幅の設定はXWPFTableに(POIバージョン3.15の最終版では)実装されていません。そのため、低レベルのオブジェクトを使用する必要があります。

例:

import java.io.FileOutputStream; 

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; 

import java.math.BigInteger; 

public class CreateWordTableColumnWidth { 

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

    XWPFDocument document= new XWPFDocument(); 

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

    paragraph = document.createParagraph(); 

    XWPFTable table = document.createTable(1, 2); 

    //values are in unit twentieths of a point (1/1440 of an inch) 
    table.setWidth(5*1440); //should be 5 inches width 

    //create CTTblGrid for this table with widths of the 2 columns. 
    //necessary for Libreoffice/Openoffice to accept the column widths. 
    //first column = 2 inches width 
    table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440)); 
    //other columns (only one in this case) = 3 inches width 
    for (int col = 1 ; col < 2; col++) { 
    table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3*1440)); 
    } 

    //set width for first column = 2 inches 
    CTTblWidth tblWidth = table.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW(); 
    tblWidth.setW(BigInteger.valueOf(2*1440)); 
    //STTblWidth.DXA is used to specify width in twentieths of a point. 
    tblWidth.setType(STTblWidth.DXA); 

    //set width for second column = 3 inches 
    tblWidth = table.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW(); 
    tblWidth.setW(BigInteger.valueOf(3*1440)); 
    tblWidth.setType(STTblWidth.DXA); 

    XWPFTableRow tableRowOne = table.getRow(0); 
    tableRowOne.getCell(0).setText("CLientID"); 
    tableRowOne.getCell(1).setText("CID001"); 
    //create second row 
    XWPFTableRow tableRow2 = table.createRow(); 
    tableRow2.getCell(0).setText("AccountID"); 
    tableRow2.getCell(1).setText("ACCID001"); 

    paragraph = document.createParagraph(); 

    document.write(new FileOutputStream("CreateWordTableColumnWidth.docx")); 
    document.close(); 

} 
} 

コードは、それが何をするかを説明するためにコメントしています。特に特別な測定単位Twip(ポイントの20分の1)が必要です。

+0

Wordの表の幅とセルの幅が多少違っていることを理解してください。つまり、それらは推奨幅のみです。 Wordは、セルの内容の長さに基づいて必要と思われるように、表示上でそれらを変更することができます。 – jmarkmurphy

+0

@jmarkmurphy:あなたのポイントは何ですか?上記の私のコードを使って設定された幅は、私がコードでコメントした幅です。 –

+0

私は明確ではなかったと思います。列の幅を設定することはできますが、テキストがより多くの領域を占める場合、Wordはテーブルを大きな列で表示できます。仕様のテーブルとセルの幅の設定については、http://www.ecma-international.org/publications/standards/Ecma-376.htmで読むことができます。POIは初版の仕様を使用しています。第4部では、テーブルレイアウトの仕組みを厳密に説明しています。レイアウトエンジンがセルの表示幅を決定する際の複数の要素の1つの要素としてそれを使用するので、幅「優先幅」を呼び出します。 – jmarkmurphy

関連する問題