阿帕奇poi子弹和编号

时间:2016-09-15 11:35:11

标签: java apache-poi

我正在使用apache POI将word文档转换为pdf。我用动态数据填充表行。一切都工作正常,但我想做一些增强,我想在每行数据之前添加一个子弹。这是一个for循环我用来填充表中的行数据:

  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 个答案:

答案 0 :(得分:3)

已经有多个创建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");
 }
}

我的例子总是完整的例子。我已经用你的代码和我的补充标记了部分。

答案 1 :(得分:0)

也许来晚了,但是我应用了Axel提出的解决方案,并且效果很好。

CTLvlText有点问题,方法cTLvl.addNewLvlText()返回了对象,无法在apache-poi jar中找到它。

我不是通过引入下一个级别来处理它的,而是在每次创建新段落时都将NumId设置为该段落。

private void generateVarios(XWPFDocument doc1, List<VariosDTO> varios) {
    CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
    cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
    CTLvl cTLvl = cTAbstractNum.addNewLvl();
    cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
    //CTLevelText a = cTLvl.addNewLvlText();

    XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
    XWPFNumbering numbering = doc1.createNumbering();
    BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
    BigInteger numID = numbering.addNum(abstractNumID);

    for (VariosDTO item : varios){
        XWPFParagraph bulletedPara = doc1.createParagraph();
        XWPFRun run = bulletedPara.createRun();
        run.setFontFamily(ARIAL);
        run.setFontSize(10);
        run.setText(item.getComentario());
        bulletedPara.setNumID(numID);
    }
}

希望对某人有用!