2016-09-15 1 views
0

나는 워드 문서를 pdf로 변환하기 위해 아파치 POI를 사용하고 있습니다. 동적 데이터로 테이블 행을 채 웁니다. 모든 것이 잘 작동하지만, 각 행 데이터 앞에 글 머리 기호를 추가하려는 일부 기능을 향상시키고 자합니다. 다음은 for 루프는 내가 테이블의 행 데이터를 채우기 위해 사용하고 있습니다 :아파치 poi 글 머리 기호 및 번호 매기기

for (String string : documentList) { 
     XWPFTableRow lnewRow = ltable.createRow(); 
     XWPFTableCell lnewCell = lnewRow.getCell(0); 
     XWPFParagraph lnewPara =lnewCell.getParagraphs().get(0); 
     XWPFRun lnewRun = lnewPara.createRun(); 
     lnewRun.setText(string); 
    } 

내가 각 행의 데이터 전에 총알을 추가하는 방법을 사람이 말해 주시겠습니까?

답변

2

이미 XWPFNumbering을 만드는 데 여러 가지 예가 있습니다. 그들 대부분은 불필요한 복잡한 의견입니다. 그러므로 가장 간단한 해결책을 찾으십시오.

import java.io.File; 
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 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat; 

import java.util.ArrayList; 
import java.util.Arrays; 

import java.math.BigInteger; 

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

    XWPFDocument document = new XWPFDocument(); 

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

    XWPFTable ltable = document.createTable(1,1); 

    ltable.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(5000)); 
    CTTblWidth tblWidth = ltable.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW(); 
    tblWidth.setW(BigInteger.valueOf(5000)); 
    tblWidth.setType(STTblWidth.DXA); 

    ltable.getRow(0).getCell(0).getParagraphs().get(0).createRun().setText("The list:"); 

    ArrayList<String> documentList = new ArrayList<String>(
    Arrays.asList(
    new String[] { 
    "documentList item 1", 
    "documentList item 2", 
    "documentList item 3" 
    })); 


//your code with supplements 

    CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance(); 
    //Next we set the AbstractNumId. This requires care. 
    //Since we are in a new document we can start numbering from 0. 
    //But if we have an existing document, we must determine the next free number first. 
    cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0)); 

///* Bullet list 
    CTLvl cTLvl = cTAbstractNum.addNewLvl(); 
    cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET); 
    cTLvl.addNewLvlText().setVal("•"); 
//*/ 

/* Decimal list 
    CTLvl cTLvl = cTAbstractNum.addNewLvl(); 
    cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL); 
    cTLvl.addNewLvlText().setVal("%1."); 
    cTLvl.addNewStart().setVal(BigInteger.valueOf(1)); 
*/ 

    XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum); 

    XWPFNumbering numbering = document.createNumbering(); 

    BigInteger abstractNumID = numbering.addAbstractNum(abstractNum); 

    BigInteger numID = numbering.addNum(abstractNumID); 

    for (String string : documentList) { 
     XWPFTableRow lnewRow = ltable.createRow(); 
     XWPFTableCell lnewCell = lnewRow.getCell(0); 
     XWPFParagraph lnewPara =lnewCell.getParagraphs().get(0); 
     lnewPara.setNumID(numID); 
     XWPFRun lnewRun = lnewPara.createRun(); 
     lnewRun.setText(string); 
    } 

//your code end 

    paragraph = document.createParagraph(); 

    FileOutputStream out = new FileOutputStream("CreateWordTableWithBulletList.docx");  
    document.write(out); 

    System.out.println("CreateWordTableWithBulletList written successully"); 
} 
} 

예제는 항상 완전한 예제입니다. 나는 그 부분에 당신의 코드와 나의 보조제를 표시했다.

관련 문제