xwpftemplate的时间设置_在内容之间插入XWPFTable

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 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 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);

paragraph = cell.getParagraphs().get(0);

run = paragraph.createRun();

run.setText("Table Cell " + i);

}

}

FileOutputStream out = new FileOutputStream("WordTableExampleNew.docx");

document.write(out);

out.close();

document.close();

}

}

Result:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值