当前位置: 首页 > 知识库问答 >
问题:

如何在中的表之间添加段落或文本。带有apachepoi的docx文件

公良信然
2023-03-14

我试图设置一些段落或文本在. docx文件使用Apache POI,我正在读取一个. docx文件作为模板从WEB-INF/资源/模板文件夹内我的战争文件,一旦读取,我想创建动态更多的表后,第9表用作模板,我可以添加更多的表格,但其他类型的内容(段落)被安排在文档的其他部分?有必要的形式来做这件事吗?

XWPFDocument doc = null;
try {
    doc = new XWPFDocument(OPCPackage.open(request.getSession().getServletContext().getResourceAsStream("/resources/templates/twd.docx")));
} catch (Exception e) {
    e.printStackTrace();
} 

XWPFParagraph parrafo = null;
XWPFTable table=null;
org.apache.xmlbeans.XmlCursor cursor = null;
XWPFParagraph newParagraph = null;
XWPFRun run = null;

for(int j=0; j < 3; j++) { //create 3 more tables
    table = doc.getTables().get(9);
    cursor = table.getCTTbl().newCursor();
    cursor.toEndToken();

    if (cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
    {
        table = doc.insertNewTbl(cursor);               

        table.getRow(0).getCell(0).addParagraph().createRun()
        .setText("Name");
        table.createRow().getCell(0).addParagraph().createRun().setText("Version");
        table.createRow().getCell(0).addParagraph().createRun().setText("Description");
        table.createRow().getCell(0).addParagraph().createRun().setText("Comments");
        table.createRow().getCell(0).addParagraph().createRun().addCarriageReturn();        

        table.getRow(0).createCell().addParagraph().createRun().setText("some text");
        table.getRow(1).createCell().addParagraph().createRun().setText("some text");
        table.getRow(2).createCell().addParagraph().createRun().setText("some text");
        table.getRow(3).createCell().addParagraph().createRun().setText("some text");

        table.getRows().get(0).getCell(0).setColor("183154");
        table.getRows().get(1).getCell(0).setColor("183154");
        table.getRows().get(2).getCell(0).setColor("183154");
        table.getRows().get(3).getCell(0).setColor("183154");           
        table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(4000));
        table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(4000));
    }

    //OTHER CONTENT BETWEEN CREATED TABLES (PARAGRAPHS, BREAK LINES,ETC)
    doc.createParagraph().createRun().setText("text after table");
}

共有1个答案

管弘
2023-03-14

如果您曾经使用过光标,那么如果您想在光标所在的文档部分中进一步插入内容,则必须使用该光标。不要相信,文档会自动记录您创建的光标。

所以举个例子:

import java.io.FileOutputStream;
import java.io.FileInputStream;

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

public class WordTextAfterTable {

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

  XWPFDocument document = new XWPFDocument(new FileInputStream("WordTextAfterTable.docx"));

  XWPFTable table = document.getTables().get(9);

  org.apache.xmlbeans.XmlCursor cursor = table.getCTTbl().newCursor();
  cursor.toEndToken(); //now we are at end of the CTTbl
  //there always must be a next start token after the table. Either a p or at least sectPr.
  while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START); //we loop over the tokens until next TokenType.START
  //now we are at next TokenType.START and insert the new table
  //note: This is immediately after the table. So both tables touch each other.
  table = document.insertNewTbl(cursor);     
  table.getRow(0).getCell(0).addParagraph().createRun().setText("Name");
  table.createRow().getCell(0).addParagraph().createRun().setText("Version");
  table.createRow().getCell(0).addParagraph().createRun().setText("Description");
  table.createRow().getCell(0).addParagraph().createRun().setText("Comments");
  table.createRow().getCell(0).addParagraph().createRun().addCarriageReturn();        
  //...
System.out.println(cursor.isEnd()); //cursor is now at the end of the new table
  //there always must be a next start token after the table. Either a p or at least sectPr.
  while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START); //we loop over the tokens until next TokenType.START
  XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
  XWPFRun run = newParagraph.createRun(); 
  run.setText("text after table");

  document.write(new FileOutputStream("WordTextAfterTableNew.docx"));
  document.close();
 }
}
 类似资料:
  • 在所示的报告中,黑色的文本是静态的,红色的文本是将由Java动态替换为某些值的字段。 在上图中,第1行和第2行我没有问题。 但对于这一段,我不知道该用什么?我应该使用静态文本框还是文本字段?如果我使用部分静态文本框和部分动态,它变得笨拙和难以保持行间距。 因此,请建议如何在jasper studio 5.6中设计以下报告。

  • 我正在使用PythonDocx将Word docx文件转换为专有的XML格式。 我对项目符号/枚举列表有问题。在许多Word文档中,当我用python-docx打开它们并查看项目符号/枚举列表的段落样式时,列表中的一些项目将是“列表段落”,但其中许多将是“正常”。 假设它们都应该是“列表段落”,有没有一种方法可以验证这是Word文档还是python-docx包的问题? 此外,当段落样式不符合预期时

  • 我正在尝试创建一个包含多列的word文档。这样做(而不是使用表)的原因是,数据将跨越多个页面,在添加到新页面之前,我只能用列填充整个页面。 可以用ApachePOI实现吗?谢谢

  • 我无法使用ApachePOI删除docx文件中的所有注释。有没有其他方法可以使用docx4j api删除注释?

  • 我试图用处理docx文件。只是简单地读取然后写入文件(现在)。这是我的简单代码: 问题是输入文件的头文件中有一个小图像。因此,在使用POI处理输入文件并在Microsoft Word中打开输出文件后,我收到损坏的文件错误: 一切工作在OO Writer,但不是在办公室。 问题是:怎么了?apache POI是否不处理头文件中包含图像的文件?你知道解决这个问题的方法吗? 我需要使用,我不考虑其他工具

  • [![我想点击Male,它反映在文本区域的Gender旁边:Male] 例如,我得到了这个脚本,我想要一个选项,当我点击按钮Male,它显示在gender旁边:在里面,如果我点击Male,它就会显示在Male旁边,就像这个Male,faleme? 文本框的图像: