在内容之间插入 XWPFTable [英] Inserting XWPFTable in between contents

查看:64
本文介绍了在内容之间插入 XWPFTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在某些内容之间插入一个 XWPFTable.文件内容是固定的,文件作为输入.我需要将表插入特定字段.

像这样:

Stack Overflow 是一个私有网站,是 Stack Exchange 网络的旗舰网站,由 Jeff Atwood 和 Joel Spolsky 于 2008 年创建.这是桌子.

内容还在继续.它的创建是为了成为早期问答网站(如 Experts-Exchange)的一个更开放的替代品.

谢谢

我写的代码

public static void main(String[] args) 抛出 IOException {XWPFDocument 文档 = new XWPFDocument(new FileInputStream(new File("input.docx")));FileOutputStream out = new FileOutputStream(new File("output.docx"));XmlCursor 游标 = null;列表元素 = document.getBodyElements();for (int n = 0; n runList = p1.getRuns();StringBuilder sb = new StringBuilder();for (XWPFRun 运行:runList)sb.append(run.getText(0));if (sb.toString().contains("应在其后创建表的文本")) {游标= p1.getCTP().newCursor();休息;}}}XWPFParagraph p = document.insertNewParagraph(cursor);XWPFTable 表 = p.getBody().insertNewTbl(cursor);XWPFTableRow tableRowOne = table.createRow();//其他生成表格的代码

我在创建行时遇到空指针异常.

解决方案

现已测试.我的怀疑是对的.光标位于代码中 XWPFParagraph p = document.insertNewParagraph(cursor); 之后的错误位置.所以 XWPFTable table = p.getBody().insertNewTbl(cursor); 无法插入,然后是 null.

但是还有更多的问题.如果找到了文本,我们将位于之后的段落中,表格将放置在该段落中.所以我们需要将光标移动到下一段.但是如果没有下一段呢?然后需要创建一个新段落.幸运的是

代码:

import java.io.FileInputStream;导入 java.io.FileOutputStream;导入 org.apache.poi.xwpf.usermodel.*;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;导入 org.apache.xmlbeans.XmlCursor;导入 java.math.BigInteger;公共类 WordInsertTableInBody {public static void main(String[] args) 抛出异常 {XWPFDocument 文档 = new XWPFDocument(new FileInputStream("WordTableExample.docx"));XmlCursor 游标 = null;XWPFParagraph 段落 = null;XWPFRun 运行 = null;布尔 foundTablePosition = false;boolean thereWasParagraphAfter = false;for (IBodyElement 元素: document.getBodyElements()) {如果(XWPFParagraph 的元素实例){段落 = (XWPFParagraph) 元素;StringBuilder sb = new StringBuilder();for (XWPFRun irun :paragraph.getRuns()) {sb.append(irun.getText(0));System.out.println(sb);if (sb.toString().contains(应在此之后创建表的文本")) {游标=段落.getCTP().newCursor();thereWasParagraphAfter = cursor.toNextSibling();//将光标移动到下一段//因为表格应该在该段之后**//thereWasParagraphAfter 如果有下一段则为真,否则为假foundTablePosition = true;}}}如果(foundTablePosition)中断;}如果(光标!= null){如果(theWasParagraphAfter){段落 = document.insertNewParagraph(cursor);} 别的 {段落 = document.createParagraph();}游标 = 段落.getCTP().newCursor();XWPFTable 表 = document.insertNewTbl(cursor);XWPFTableRow 行 = table.getRow(0);if (row == null) row = table.createRow();int twipsPerInch = 1440;table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));for (int col = 1 ; col <4; col++) {table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));}for (int i = 0; i <4; i++) {XWPFTableCell 单元格 = row.getCell(i);if (cell == null) cell = row.createCell();CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();tblWidth.setW(BigInteger.valueOf(1 * twipsPerInch));tblWidth.setType(STTblWidth.DXA);if (cell.getParagraphs().size() > 0 ) 段落 = cell.getParagraphs().get(0);else 段落 = cell.addParagraph();运行 = 段落.createRun();run.setText(表格单元格"+ i);}}FileOutputStream out = new FileOutputStream("WordTableExampleNew.docx");document.write(out);关闭();文档.close();}}

结果:

HI I would like to insert a XWPFTable in between some contents. The file is content is fixed and file is taken as input. I need the table to be inserted in the specific field.

like this:

Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky. Here is the table.

The contents continue.It was created to be a more open alternative to earlier question and answer sites such as Experts-Exchange.

Thanks

The code i have written

public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument(new FileInputStream(new File("input.docx")));
        FileOutputStream out = new FileOutputStream(new File("output.docx"));
        XmlCursor cursor = null;
         List<IBodyElement> elements = document.getBodyElements();
         for (int n = 0; n < elements.size(); n++) {
             IBodyElement element = elements.get(n);
             if (element instanceof XWPFParagraph) {
                 XWPFParagraph p1 = (XWPFParagraph) element;
                 List<XWPFRun> runList = p1.getRuns();
                 StringBuilder sb = new StringBuilder();
                 for (XWPFRun run : runList)
                     sb.append(run.getText(0));
                 if (sb.toString().contains("Text after which table should be created")) {
                      cursor= p1.getCTP().newCursor();
                      break;
             }
         }
    } 
         XWPFParagraph p = document.insertNewParagraph(cursor);
         XWPFTable table = p.getBody().insertNewTbl(cursor);
         XWPFTableRow tableRowOne = table.createRow();

        //other codes for generating the table  

I am getting null pointer exception on creating the row.

解决方案

Have tested now. My suspicion was right. The cursor was on the wrong place in your code after XWPFParagraph p = document.insertNewParagraph(cursor);. So the XWPFTable table = p.getBody().insertNewTbl(cursor); could not be inserted and was null then.

But there are further problems. If the text was found, we are in the paragraph after which the table shall be placed. So we need moving the cursor to the next paragraph. But what if there is not a next paragraph? Then a new paragraph needs to be created. Fortunately the XmlCursor.toNextSibling flags if it was successful.

Example:

Template:

Code:

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

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import org.apache.xmlbeans.XmlCursor;
import java.math.BigInteger;


public class WordInsertTableInBody {

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

  XWPFDocument document = new XWPFDocument(new FileInputStream("WordTableExample.docx"));
  XmlCursor cursor = null;
  XWPFParagraph paragraph = null; 
  XWPFRun run = null; 

  boolean foundTablePosition = false;
  boolean thereWasParagraphAfter = false;
  for (IBodyElement element : document.getBodyElements()) {
   if (element instanceof XWPFParagraph) {
    paragraph = (XWPFParagraph) element;
    StringBuilder sb = new StringBuilder();
    for (XWPFRun irun : paragraph.getRuns()) {
     sb.append(irun.getText(0));
System.out.println(sb);
     if (sb.toString().contains("Text after which table should be created")) {
      cursor= paragraph.getCTP().newCursor();
      thereWasParagraphAfter = cursor.toNextSibling(); // move cursor to next paragraph 
       //because the table shall be **after** that paragraph
       //thereWasParagraphAfter is true if there is a next paragraph, else false
      foundTablePosition = true;
     }
    }
   }
   if (foundTablePosition) break;
  } 

  if (cursor != null) {
   if (thereWasParagraphAfter) {
    paragraph = document.insertNewParagraph(cursor);
   } else {
    paragraph = document.createParagraph();
   }
   cursor = paragraph.getCTP().newCursor();
   XWPFTable table = document.insertNewTbl(cursor);
   XWPFTableRow row = table.getRow(0); if (row == null) row = table.createRow();
   int twipsPerInch =  1440;
   table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));
   for (int col = 1 ; col < 4; col++) {
    table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));
   }
   for (int i = 0; i < 4; i++) {
    XWPFTableCell cell = row.getCell(i); if (cell == null) cell = row.createCell();
    CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
    tblWidth.setW(BigInteger.valueOf(1 * twipsPerInch));
    tblWidth.setType(STTblWidth.DXA);
    if (cell.getParagraphs().size() > 0 ) paragraph = cell.getParagraphs().get(0); else paragraph = cell.addParagraph();
    run = paragraph.createRun();
    run.setText("Table Cell " + i);
   }
  }

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

Result:

这篇关于在内容之间插入 XWPFTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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