apache poi在Word文档中添加表 [英] apache poi add table in word document

查看:130
本文介绍了apache poi在Word文档中添加表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java代码可以使用Apache POI创建一个表和一些文本到Word文档,但是它在最后一个文档中添加了表.我想写一些文本,然后添加表格并再次写一些文本.

I have Java code to create a table and some texts to a Word document using Apache POI, but it adds table in last document. I want to write some text, then add table and write some text again.

当前,它在表格的第一和最后一个文档中添加2个文本(再见)

Currently it adds table first and last document add 2 texts (Hi & Bye)

我的代码:

public static void main(String[] args)throws Exception {
        //Blank Document
        XWPFDocument document= new XWPFDocument();

        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(
        new File("create_table.docx"));

        //create table
        XWPFTable table    = document.createTable();
        XWPFParagraph para = document.createParagraph();
        XWPFRun run        = para.createRun();

        run.setText("Hi");
        //create first row
        XWPFTableRow tableRowOne = table.getRow(0);
        tableRowOne.getCell(0).setText("col one, row one");
        tableRowOne.addNewTableCell().setText("col two, row one");
        tableRowOne.addNewTableCell().setText("col three, row one");
        //create second row
        XWPFTableRow tableRowTwo = table.createRow();
        tableRowTwo.getCell(0).setText("col one, row two");
        tableRowTwo.getCell(1).setText("col two, row two");
        tableRowTwo.getCell(2).setText("col three, row two");
        //create third row
        XWPFTableRow tableRowThree = table.createRow();
        tableRowThree.getCell(0).setText("col one, row three");
        tableRowThree.getCell(1).setText("col two, row three");
        tableRowThree.getCell(2).setText("col three, row three");

        run.setText("Bye");

        document.write(out);
        out.close();
        System.out.println("create_table.docx written successully");
}

我如何在页面的第一页打印Hi并添加表格并在表格后打印Bye?

How can i print Hi first of page and add table and print Bye after table?

当我想向其中添加内容并最终写入并打开它时,如何保存文档?

And how can i save document every time when i want add content to it and finally write it and open it ?

谢谢

推荐答案

您必须保持正确的顺序,更重要的是:您必须创建一个新段落.

You have to keep the right order and more importantly : You have to create a new paragraph.

这是代码,您需要:

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

    //Blank Document
    XWPFDocument document = new XWPFDocument();

    //Write the Document in file system
    FileOutputStream out = new FileOutputStream(new File("create_table.docx"));

    //Write first Text in the beginning
    XWPFParagraph para = document.createParagraph();
    XWPFRun run = para.createRun();
    run.setText("Hi");

    //create table
    XWPFTable table = document.createTable();

    //create first row
    XWPFTableRow tableRowOne = table.getRow(0);
    tableRowOne.getCell(0).setText("col one, row one");
    tableRowOne.addNewTableCell().setText("col two, row one");
    tableRowOne.addNewTableCell().setText("col three, row one");
    //create second row
    XWPFTableRow tableRowTwo = table.createRow();
    tableRowTwo.getCell(0).setText("col one, row two");
    tableRowTwo.getCell(1).setText("col two, row two");
    tableRowTwo.getCell(2).setText("col three, row two");
    //create third row
    XWPFTableRow tableRowThree = table.createRow();
    tableRowThree.getCell(0).setText("col one, row three");
    tableRowThree.getCell(1).setText("col two, row three");
    tableRowThree.getCell(2).setText("col three, row three");

    //Write second Text after the table (by creating a new paragraph)
    XWPFParagraph para2 = document.createParagraph();
    XWPFRun run2 = para2.createRun();
    run2.setText("Bye");

    document.write(out);
    out.close();
    System.out.println("create_table.docx written successully");
}

这是输出,您将得到:

这篇关于apache poi在Word文档中添加表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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