0

我想创建一个具有多列的 XWPFTable。我将值插入从 0 开始的行号。但问题是,第一行的内容被附加到第 0 行的内容。另一件事是,当我只写第一行时,它在第一行。那么我是否需要假设 getRow(0) 和 getRow(1) 都指向最上面的行?提前致谢。

    XWPFDocument doc = new XWPFDocument();
    XWPFTable table=doc.createTable(3,3);
    table.getRow(0).getCell(0).setText("A");
    table.getRow(0).getCell(1).setText("B");
    table.getRow(0).getCell(2).setText("C");

    table.getRow(1).getCell(0).setText("one");
    table.getRow(1).getCell(1).setText("two");
    table.getRow(1).getCell(2).setText("three");

    table.getRow(2).getCell(0).setText("XXX");
    table.getRow(2).getCell(1).setText("YYYYY");
    table.getRow(2).getCell(2).setText("ZZZZZZ");

生成的表是:

Aone Btwo Cthree
XXX YYYYY ZZZZZZ

4

1 回答 1

0

我尝试了您的代码,我看到了应该在 MS Office (2010) 中的表格。OpenOffice 渲染存在一些布局差异,但仍然是正确的。那是你试过的代码吗?您使用的是什么版本的 POI?您使用的是哪种 Office 套件?

这是我测试的。

package com.ecinev.poitest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class TestPoi2 {

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

        XWPFDocument doc = new XWPFDocument();

        XWPFTable table = doc.createTable(3, 3);
        table.getRow(0).getCell(0).setText("A");
        table.getRow(0).getCell(1).setText("B");
        table.getRow(0).getCell(2).setText("C");

        table.getRow(1).getCell(0).setText("one");
        table.getRow(1).getCell(1).setText("two");
        table.getRow(1).getCell(2).setText("three");

        table.getRow(2).getCell(0).setText("XXX");
        table.getRow(2).getCell(1).setText("YYYYY");
        table.getRow(2).getCell(2).setText("ZZZZZZ");

        doc.write(new FileOutputStream(new File(
                "/Users/myfolder/Documents/empty.docx")));
    }
}
于 2015-02-07T00:01:07.333 回答