2017-02-15 11 views
1

Java로 단어 파일을 만들고이 단어 파일에 세부 정보를 입력하는 작은 프로젝트에서 작업하고 있습니다. 단어 파일을 만들 수 있으며 데이터를 입력 할 수도 있습니다. 또한 단어 파일에 표를 작성하고 세부 정보를 입력합니다.JAVA로 단어 파일의 표의 열 너비를 늘리는 방법

이제 원하는 항목의 너비를 늘리고 싶습니다.

어떤 방법이 있습니까? 아파치 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는 초판 사양을 사용합니다. Part 4에는 테이블 레이아웃이 어떻게 작동하는지에 대한 엄격한 설명이 있습니다. 레이아웃 엔진이 셀의 표시된 너비를 결정할 때 여러 요소 중 하나의 구성 요소로 사용하므로 너비는 'preferred width'를 호출합니다. – jmarkmurphy