Apache POI编号列表 [英] Apache POI numbered list

查看:824
本文介绍了Apache POI编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache-poi在MS Word文档中写入一些数据.我已经花了几个小时试图找出如何创建编号列表,但是我没有取得任何结果.

I am using apache-poi to write some data in a MS Word document. I've already spent several hours trying to figure out how to create a numbered list but I haven't achieved any results.

我已经经历过以及其他多个问题.考虑到apache-poi是我见过的最糟糕的文档(基本上根本没有文档),并且它们的类和方法绝对是疯子,这对我来说很难理解这样的复杂示例.

I've gone through this, that and multiple other questions. Taking into account that apache-poi has the worst kind of documentation I've ever seen (basically there's no documentation at all) and their classes and methods have absolutely mad names it's too difficult for me to understand such complex examples.

问: 任何人都可以提供简洁的代码段来在MS Word文档中创建此类列表:

  1. 一个
  2. 两个
  3. 三个

先谢谢了.

推荐答案

首先,我认为您的问题中链接的代码存在问题,因为apache poi处于高度开发阶段,有时在较早版本中可用的代码确实存在在当前版本中不再起作用.但是即使在当前的版本3.16中,简单的复制和粘贴代码也可以正常工作.

First I thought there is an issue with my code linked in your question, since apache poi is highly in development and sometimes code which has worked in earlier versions does not work any more in current versions. But simple copy&paste code leads to working code even in current version 3.16.

所以我删除了所有表内容,因为链接的问题是关于表单元格中的列表的,我得到了:

So I have removed all the table stuff, since the linked question was about a list in table cells and I got:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

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 CreateWordSimplestNumberingList {

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

  XWPFDocument document = new XWPFDocument();

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

  ArrayList<String> documentList = new ArrayList<String>(
   Arrays.asList(
    new String[] {
     "One",
     "Two",
     "Three"
    }));

  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) {
   paragraph = document.createParagraph();
   paragraph.setNumID(numID);
   run=paragraph.createRun(); 
   run.setText(string); 
  }

  paragraph = document.createParagraph();

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

 }
}

结果是:

您需要知道的是,*.docx文件只是一个ZIP文件,其中包含具有XML文件的目录结构.因此,如果需要创建特殊的Word文档,该怎么做,就是使用Word本身创建该文档的最简单形式.然后,我将*.docx文件解压缩并在/word/document.xml中找到主要故事.在这里有编号(列表):

What you need to know is, that a *.docx file is simply a ZIP file containing a directory structure with XMLfiles in it. So what I do if I have the requirement to create a special Word document, is to create the simplest form of that document using Word itself. Then I unzip the *.docx file and find the main story in /word/document.xml. With numberings (lists) I find there:

<w:numId w:val="1"/>

在段落中.这是对/word/numbering.xml中的mumId的引用.好吧,看看这个,我发现类似:

within the paragraphs. This are references to mumIds in /word/numbering.xml. Well have a look at this, I find something like:

<w:numbering>
 <w:abstractNum w:abstractNumId="0">
  <w:lvl>
   <w:start w:val="1"/>
   <w:numFmt w:val="decimal"/>
   <w:lvlText w:val="%1."/>
  </w:lvl>
 </w:abstractNum>
 <w:num w:numId="1">
  <w:abstractNumId w:val="0"/>
 </w:num>
</w:numbering>

具有编号级别(lvl)的定义的abstractNum和具有numId并引用abstractNumnum.

A abstractNum having the definition for levels (lvl) of the numbering and a num having the numId and a reference to the abstractNum.

然后,您需要知道apache poi XWPF是基于org.openxmlformats.schemas.wordprocessingml.x2006.main.*的.因此,我们有 XWPFNumbering XWPFAbstractNum ,但XWPFAbstractNum只能是使用org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum创建的,因此如何查找有关CTAbstractNum的文档.好的Google,找到

Then you need to know that apache poi XWPF bases on org.openxmlformats.schemas.wordprocessingml.x2006.main.*. So we have XWPFNumbering and XWPFAbstractNum but XWPFAbstractNum can only be created using org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum So how to find documentation about CTAbstractNum. Well Google it and find http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/wordprocessingml/x2006/main/CTAbstractNum.java

这篇关于Apache POI编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆