如何使用poi XWPF在指定位置插入table?
how to insert the table at the specified location with poi XWPF?
我在 .docx 文件中做了一个标记,例如 ${table}
,现在我想在这个标记的地方插入一个带有 apache poi xwpf 的 table。
换句话说,我想用 table 替换这个标记 ${table}
。
这是我的代码
List<XWPFParagraph> parasList = document.getParagraphs();
for(XWPFParagraph paragraph:parasList){
String paraText=paragraph.getText();
if(paraText.equals("${table}")) {
System.out.println("find:"+paraText);
int pos=document.getPosOfParagraph(paragraph);
XWPFTable newT=document.createTable();
document.insertTable(pos,newT);
}
}
然而,table可以创建,但它出现在word文档的底部。
我该怎么办?
这是代码
public static void main(String[] args) throws IOException, InvalidFormatException {
String filepath = "D:\users\IDEA\POIUtils\doc\测试文档.docx";
String destpath = "D:\users\IDEA\POIUtils\doc\测试文档_result.docx";
OPCPackage opcPackage = POIXMLDocument.openPackage(filepath);
XWPFDocument xwpfDocument = new XWPFDocument(opcPackage);
List<XWPFParagraph> xwpfParas = xwpfDocument.getParagraphs();
int num=0;
for(int i=0;i<xwpfParas.size();i++){
if(num==3) break;
XWPFParagraph xwpfParagraph = xwpfParas.get(i);
String text=xwpfParagraph.getText();
//插入段落
if(text.equals("${mark_newParagraph}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.BOTH);//两端对齐
newPara.setIndentationFirstLine(480);//首行缩进24磅
XWPFRun newParaRun = newPara.createRun();
newParaRun.setText("这是新插入的段落!");
newParaRun.setFontFamily("宋体");
newParaRun.setFontSize(12);
newParaRun.setBold(false);
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
} //插入表格 if(text.equals("${mark_newTable}")){
XmlCursor cursor= xwpfParagraph.getCTP().newCursor();
XWPFTable table = xwpfDocument.insertNewTbl(cursor);
XWPFTableRow row_0 = table.getRow(0);
row_0.getCell(0).setText("姓名");
row_0.addNewTableCell().setText("年龄");
XWPFTableRow row_1 = table.createRow();
row_1.getCell(0).setText("隔壁老王");
row_1.getCell(1).setText("48");
setTableLocation(table,"center");
setCellLocation(table,"CENTER","center");
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}
//插入图片 if(text.equals("${mark_newPicture}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.CENTER);//居中
XWPFRun newParaRun = newPara.createRun();
newParaRun.addPicture(new FileInputStream("./doc/bus.png"),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200)); xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph)); } } FileOutputStream outStream = null; try {
outStream = new FileOutputStream(destpathString);
xwpfDocument.write(outStream);
outStream.flush();
outStream.close(); } catch (IOException e) {
e.printStackTrace(); } }
我在 .docx 文件中做了一个标记,例如 ${table}
,现在我想在这个标记的地方插入一个带有 apache poi xwpf 的 table。
换句话说,我想用 table 替换这个标记 ${table}
。
这是我的代码
List<XWPFParagraph> parasList = document.getParagraphs();
for(XWPFParagraph paragraph:parasList){
String paraText=paragraph.getText();
if(paraText.equals("${table}")) {
System.out.println("find:"+paraText);
int pos=document.getPosOfParagraph(paragraph);
XWPFTable newT=document.createTable();
document.insertTable(pos,newT);
}
}
然而,table可以创建,但它出现在word文档的底部。
我该怎么办?
这是代码
public static void main(String[] args) throws IOException, InvalidFormatException {
String filepath = "D:\users\IDEA\POIUtils\doc\测试文档.docx";
String destpath = "D:\users\IDEA\POIUtils\doc\测试文档_result.docx";
OPCPackage opcPackage = POIXMLDocument.openPackage(filepath);
XWPFDocument xwpfDocument = new XWPFDocument(opcPackage);
List<XWPFParagraph> xwpfParas = xwpfDocument.getParagraphs();
int num=0;
for(int i=0;i<xwpfParas.size();i++){
if(num==3) break;
XWPFParagraph xwpfParagraph = xwpfParas.get(i);
String text=xwpfParagraph.getText();
//插入段落
if(text.equals("${mark_newParagraph}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.BOTH);//两端对齐
newPara.setIndentationFirstLine(480);//首行缩进24磅
XWPFRun newParaRun = newPara.createRun();
newParaRun.setText("这是新插入的段落!");
newParaRun.setFontFamily("宋体");
newParaRun.setFontSize(12);
newParaRun.setBold(false);
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
} //插入表格 if(text.equals("${mark_newTable}")){
XmlCursor cursor= xwpfParagraph.getCTP().newCursor();
XWPFTable table = xwpfDocument.insertNewTbl(cursor);
XWPFTableRow row_0 = table.getRow(0);
row_0.getCell(0).setText("姓名");
row_0.addNewTableCell().setText("年龄");
XWPFTableRow row_1 = table.createRow();
row_1.getCell(0).setText("隔壁老王");
row_1.getCell(1).setText("48");
setTableLocation(table,"center");
setCellLocation(table,"CENTER","center");
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}
//插入图片 if(text.equals("${mark_newPicture}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.CENTER);//居中
XWPFRun newParaRun = newPara.createRun();
newParaRun.addPicture(new FileInputStream("./doc/bus.png"),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200)); xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph)); } } FileOutputStream outStream = null; try {
outStream = new FileOutputStream(destpathString);
xwpfDocument.write(outStream);
outStream.flush();
outStream.close(); } catch (IOException e) {
e.printStackTrace(); } }