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

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

问题描述

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

我已经完成了

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

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

<w:abstractNum w:abstractNumId=0"><w:lvl><w:start w:val=1"/><w:numFmt w:val=十进制"/><w:lvlText w:val=%1."/></w:lvl></w:abstractNum><w:num w:numId=1"><w:abstractNumId w:val=0"/></w:num></w:编号>

具有编号级别 (lvl) 定义的 abstractNum 和具有 numIdnum以及对 abstractNum 的引用.

那么你需要知道apache poi XWPF 基于org.openxmlformats.schemas.wordprocessingml.x2006.main.*.所以我们有 XWPFNumberingXWPFAbstractNumXWPFAbstractNum 只能使用 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum 创建 那么如何找到关于 CTAbstractNum 的文档.谷歌一下,找到 http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/wordprocessingml/x2006/main/CTAbstractNum.java

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.

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.

Q: Could anyone provide a concise code snippet to create such a list in MS Word document:

  1. One
  2. Two
  3. Three

Thank's in advance.

解决方案

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.setIlvl(BigInteger.valueOf(0)); // set indent level 0
  cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
  cTLvl.addNewLvlText().setVal("•");
*/

///* Decimal list
  CTLvl cTLvl = cTAbstractNum.addNewLvl();
  cTLvl.setIlvl(BigInteger.valueOf(0)); // set indent level 0
  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();

 }
}

This results in:

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"/>

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>

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

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天全站免登陆