2017-02-15 658 views
1

我正在研究一個小型項目,在那裏我通過Java創建word文件並在這個word文件中輸入一些細節。 我能夠創建word文件,並能夠輸入數據。我還在Word文件中寫入一個表格並輸入一些細節。如何通過JAVA增加word文件中表格的列寬

現在我想要的,我想增加特定列的寬度。

有沒有辦法做到這一點?我使用Apache POI驅動程序來創建word文件並將數據寫入其中。

我使用以下代碼:

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(二十分之一)。

+0

瞭解Word中的表格寬度和單元格寬度有點奇怪。也就是說,它們只是建議的寬度。根據單元格內容的長度,Word可以根據需要更改它們。 – jmarkmurphy

+0

@jmarkmurphy:你的觀點是什麼?上面使用我的代碼設置的寬度正是我在代碼中評論過的寬度。 –

+0

我想我不清楚。您可以爲列設置寬度,但如果文本佔用更多空間,Word可以使用更大的列呈現表格。您可以在http://www.ecma-international.org/publications/standards/Ecma-376.htm閱讀有關設置表格和單元格寬度的信息。POI使用第一版規格。第4部分對錶格佈局如何工作有嚴格的描述。它調用寬度'首選寬度'作爲佈局引擎在決定單元格的顯示寬度時將其用作多個因素的一個組成部分。 – jmarkmurphy

相關問題