博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache POI XWPF 爬坑指南之二特定位置插入表格、段落、图片
阅读量:5167 次
发布时间:2019-06-13

本文共 5084 字,大约阅读时间需要 16 分钟。

转载: 

 

package com.yxg;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

 

import org.apache.poi.POIXMLDocument;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

 

public class TestWord {
public static void main(String[] args) throws IOException, InvalidFormatException {
String filepath = "C:\\Users\\Administration\\Desktop\\test.docx";
String destpath = "C:\\Users\\Administration\\Desktop\\test_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();
URL url = new URL("https://gd1.alicdn.com/imgextra/i2/758877415/O1CN0124e7IIJkzKJrYwp_!!758877415.jpg");
newParaRun.addPicture(url.openStream(),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200));
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}
}

 

write(xwpfDocument,destpath);

}

 

private static void write(XWPFDocument document, String destpath) {
try {
OutputStream stream = new FileOutputStream(destpath);
document.write(stream);
stream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

 

/**
* 设置单元格水平位置和垂直位置
*
* @param xwpfTable
* @param verticalLoction 单元格中内容垂直上TOP,下BOTTOM,居中CENTER,BOTH两端对齐
* @param horizontalLocation 单元格中内容水平居中center,left居左,right居右,both两端对齐
*/
public static void setCellLocation(XWPFTable xwpfTable, String verticalLoction, String horizontalLocation) {
List<XWPFTableRow> rows = xwpfTable.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
CTTc cttc = cell.getCTTc();
CTP ctp = cttc.getPList().get(0);
CTPPr ctppr = ctp.getPPr();
if (ctppr == null) {
ctppr = ctp.addNewPPr();
}
CTJc ctjc = ctppr.getJc();
if (ctjc == null) {
ctjc = ctppr.addNewJc();
}
ctjc.setVal(STJc.Enum.forString(horizontalLocation)); //水平居中
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.valueOf(verticalLoction));//垂直居中
}
}
}

 

/**

* 设置表格位置
*
* @param xwpfTable
* @param location 整个表格居中center,left居左,right居右,both两端对齐
*/
public static void setTableLocation(XWPFTable xwpfTable, String location) {
CTTbl cttbl = xwpfTable.getCTTbl();
CTTblPr tblpr = cttbl.getTblPr() == null ? cttbl.addNewTblPr() : cttbl.getTblPr();
CTJc cTJc = tblpr.addNewJc();
cTJc.setVal(STJc.Enum.forString(location));
}

 

}

 

 

转载于:https://www.cnblogs.com/yxgmagic/p/10260904.html

你可能感兴趣的文章
Confluence 6 系统运行信息中的 JVM 内存使用情况
查看>>
Confluence 6 升级以后
查看>>
用JS实现版面拖拽效果
查看>>
二丶CSS
查看>>
《avascript 高级程序设计(第三版)》 ---第二章 在HTML中使用Javascript
查看>>
JS一些概念知识及参考链接
查看>>
TCP/IP协议原理与应用笔记24:网际协议(IP)之 IP协议的简介
查看>>
SAP HANA开发中常见问题- 基于SAP HANA平台的多团队产品研发
查看>>
游戏中的心理学(一):认知失调有前提条件
查看>>
WHAT I READ FOR DEEP-LEARNING
查看>>
【Ruby】Ruby在Windows上的安装
查看>>
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
吴恩达机器学习笔记 —— 3 线性回归回顾
查看>>