2016-09-15 17 views
0

私はpdfに単語の文書を変換するためにPOI POIを使用しています。私はテーブルの行に動的なデータを埋めています。すべて正常に動作していますが、私は各行のデータの前に箇条書きを追加したいいくつかの強化を行いたいと思います。ここで私はテーブルの行データを埋めるために使用していますforループです:Apache 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"); 
} 
} 

私の例は、常に完全な例です。私はあなたのコードと私の補足でその部分に印を付けました。

関連する問題