使用java向world中填充模板数据
1、world 模板准备
${name} 中的内容先使用记事本写,然后在复制到world 文档中。world 格式docx
2、java工具类使用
2.1 world 模板填充工具类
import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* word 模板填充工具类
*/
public class WorderToNewWordUtils {
/**
* 根据模板生成word文档
* @param inputUrl 模板路径
* @param textMap 需要替换的文本内容
* @param mapList 需要动态生成的内容
* @return
*/
public static XWPFDocument changWord(String inputUrl, Map<String, Object> textMap, List<Object> mapList,int[] placeList) {
XWPFDocument document = null;
try {
//获取docx解析对象
document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
//解析替换文本段落对象
WorderToNewWordUtils.changeText(document, textMap);
//解析替换表格对象
WorderToNewWordUtils.changeTable(document, textMap, mapList,placeList);
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
/**
* 替换段落文本
* @param document docx解析对象
* @param textMap 需要替换的信息集合
*/
public static void changeText(XWPFDocument document, Map<String, Object> textMap){
//获取段落集合
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
//判断此段落时候需要进行替换
String text = paragraph.getText();
if(checkText(text)){
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
//替换模板原来位置
Object ob = changeValue(run.toString(), textMap);
// System.out.println("段落:"+run.toString());
if (ob instanceof String){
run.setText((String)ob,0);
}
}
}
}
}
/**
* 替换表格对象方法
* @param document docx解析对象
* @param textMap 需要替换的信息集合
* @param mapList 需要动态生成的内容
*/
public static void changeTable(XWPFDocument document, Map<String, Object> textMap, List<Object> mapList,int[] placeList){
//获取表格对象集合
List<XWPFTable> tables = document.getTables();
//循环所有需要进行替换的文本,进行替换
for (int i = 0; i < tables.size(); i++) {
XWPFTable table = tables.get(i);
if(checkText(table.getText())){
List<XWPFTableRow> rows = table.getRows();
// System.out.println("简单表格替换:"+rows);
//遍历表格,并替换模板
eachTable(document,rows, textMap);
}
}
int index=0;
//操作word中的表格
for (int i = 0; i < tables.size(); i++) {
//只处理行数大于等于2的表格,且不循环表头
XWPFTable table = tables.get(i);
if(placeList[index]==i){
List<String[]> list = (List<String[]>) mapList.get(index);
//第二个表格使用daList,插入数据
if (null != list && 0 < list.size()){
insertTable(table,list,document);
List<Integer[]> indexList = startEnd(list);
for (int c=0;c<indexList.size();c++){
//合并行
mergeCellVertically(table,0,indexList.get(c)[0]+1,indexList.get(c)[1]+1);
}
}
index++;
}
}
}
/**
* 遍历表格
* @param rows 表格行对象
* @param textMap 需要替换的信息集合
*/
public static void eachTable(XWPFDocument document, List<XWPFTableRow> rows , Map<String, Object> textMap){
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
//判断单元格是否需要替换
if(checkText(cell.getText())){
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
Object ob = changeValue(run.toString(), textMap);
if (ob instanceof String){
run.setText((String)ob,0);
}
}
}
}
}
}
}
/**
* 为表格插入数据,行数不够添加新行
* @param table 需要插入数据的表格
* @param daList 插入表格的数据
* @param document
*/
public static void insertTable(XWPFTable table, List<String[]> daList, XWPFDocument document){
//创建行和创建需要的列
for(int i = 1; i < daList.size(); i++){
//添加一个新行
XWPFTableRow row = table.insertNewTableRow(1);
for(int k=0; k<daList.get(0).length;k++){
row.createCell();//根据String数组第一条数据的长度动态创建列
}
}
//创建行,根据需要插入的数据添加新行,不处理表头
for(int i = 0; i < daList.size(); i++){
List<XWPFTableCell> cells = table.getRow(i+1).getTableCells();
for(int j = 0; j < cells.size(); j++){
//获取当前单元格
XWPFTableCell cell02 = cells.get(j);
//获取单元格里面的段落(每一个单元格相当于一个document文档)
XWPFParagraph xwpfParagraph = cell02.getParagraphs().get(0);//获取单元格中第一个段落位置
XWPFRun run = xwpfParagraph.createRun();//创建一段文本对象(就是一段文字)
run.setFontFamily("楷体");//设置文字的格式,解决word转PDF。宋体中文不显示
run.setText(daList.get(i)[j]);//设置文字的内容
}
}
}
/**
* 判断文本中时候包含$
* @param text 文本
* @return 包含返回true,不包含返回false
*/
public static boolean checkText(String text){
boolean check = false;
if(text.indexOf("$")!= -1){
check = true;
}
return check;
}
/**
* 匹配传入信息集合与模板
* @param value 模板需要替换的区域
* @param textMap 传入信息集合
* @return 模板需要替换区域信息集合对应值
*/
public static Object changeValue(String value, Map<String, Object> textMap){
Set<Map.Entry<String, Object>> textSets = textMap.entrySet();
Object valu = "";
for (Map.Entry<String, Object> textSet : textSets) {
//匹配模板与替换值 格式${key}
String key = "${"+textSet.getKey()+"}";
if(value.indexOf(key)!= -1){
valu = textSet.getValue();
}
}
return valu;
}
/**
* 将输入流中的数据写入字节数组
* @param in
* @return
*/
public static byte[] inputStream2ByteArray(InputStream in, boolean isClose){
byte[] byteArray = null;
try {
int total = in.available();
byteArray = new byte[total];
in.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(isClose){
try {
in.close();
} catch (Exception e2) {
// System.out.println("关闭流失败");
}
}
}
return byteArray;
}
/**
* 合并行
* @param table
* @param col 需要合并的列
* @param fromRow 开始行
* @param toRow 结束行
*/
public static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++){
CTVMerge vmerge = CTVMerge.Factory.newInstance();
if(rowIndex == fromRow){
vmerge.setVal(STMerge.RESTART);
} else {
vmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setVMerge(vmerge);
} else {
tcPr = CTTcPr.Factory.newInstance();
tcPr.setVMerge(vmerge);
cell.getCTTc().setTcPr(tcPr);
}
}
}
/**
* 获取需要合并单元格的下标
* @return
*/
public static List<Integer[]> startEnd(List<String[]> daList){
List<Integer[]> indexList = new ArrayList<Integer[]>();
List<String> list = new ArrayList<String>();
for (int i=0;i<daList.size();i++){
list.add(daList.get(i)[0]);
}
Map<Object, Integer> tm = new HashMap<Object, Integer>();
for (int i=0;i<daList.size();i++){
if (!tm.containsKey(daList.get(i)[0])) {
tm.put(daList.get(i)[0], 1);
} else {
int count = tm.get(daList.get(i)[0]) + 1;
tm.put(daList.get(i)[0], count);
}
}
for (Map.Entry<Object, Integer> entry : tm.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if (list.indexOf(key) != (-1)){
Integer[] index = new Integer[2];
index[0] = list.indexOf(key);
index[1] = list.lastIndexOf(key);
indexList.add(index);
}
}
return indexList;
}
}
2.2 world 转PDF工具类
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.charset.Charset;
/**
* 使用poi+itextpdf进行word转pdf
* 先将word转成html,再将html转成pdf
*
* @author :hewie
* @date :Created in 2020/2/27 22:41
*/
public class OfficeUtil {
/**
* 将doc格式文件转成html
*
* @param docPath doc文件路径
* @param imageDir doc文件中图片存储目录
* @return html
*/
public static String doc2Html(String docPath, String imageDir) {
String content = null;
ByteArrayOutputStream baos = null;
try {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(docPath));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
@Override
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
File file = new File(imageDir + suggestedName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return imageDir + suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
baos = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(baos);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
content = new String(baos.toByteArray(), "utf-8");
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
/**
* 将docx格式文件转成html
*
* @param docxPath docx文件路径
* @param imageDir docx文件中图片存储目录
* @return html
*/
public static String docx2Html(String docxPath, String imageDir) {
String content = null;
FileInputStream in = null;
ByteArrayOutputStream baos = null;
try {
// 1> 加载文档到XWPFDocument
in = new FileInputStream(new File(docxPath));
XWPFDocument document = new XWPFDocument(in);
// 2> 解析XHTML配置(这里设置IURIResolver来设置图片存放的目录)
XHTMLOptions options = XHTMLOptions.create();
// 存放word中图片的目录
options.setExtractor(new FileImageExtractor(new File(imageDir)));
options.URIResolver(new BasicURIResolver(imageDir));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 3> 将XWPFDocument转换成XHTML
baos = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, baos, options);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (baos != null) {
content = new String(baos.toByteArray(), "utf-8");
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return content;
}
/**
* 使用jsoup规范化html
*
* @param html html内容
* @return 规范化后的html
*/
public static String formatHtml(String html) {
org.jsoup.nodes.Document doc = Jsoup.parse(html);
// 去除过大的宽度
String style = doc.attr("style");
if (StringUtils.isNotEmpty(style) && style.contains("width")) {
doc.attr("style", "");
}
Elements divs = doc.select("div");
for (Element div : divs) {
String divStyle = div.attr("style");
if (StringUtils.isNotEmpty(divStyle) && divStyle.contains("width")) {
div.attr("style", "");
}
}
// jsoup生成闭合标签
doc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
return doc.html();
}
/**
* html转成pdf
*
* @param html html
* @param outputPdfPath 输出pdf路径
*/
public static void htmlToPdf(String html, String outputPdfPath) {
com.itextpdf.text.Document document = null;
ByteArrayInputStream bais = null;
try {
// 纸
document = new com.itextpdf.text.Document(PageSize.A4);
// 笔
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPdfPath));
document.open();
// html转pdf
bais = new ByteArrayInputStream(html.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais,
Charset.forName("UTF-8"), new FontProvider() {
@Override
public boolean isRegistered(String s) {
return false;
}
@Override
public Font getFont(String s, String s1, boolean embedded, float size, int style, BaseColor baseColor) {
// 配置字体
Font font = null;
try {
// 方案一:使用本地字体(本地需要有字体)
// BaseFont bf = BaseFont.createFont("c:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/seguisym.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 方案二:使用jar包:iTextAsian,这样只需一个jar包就可以了
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
font = new Font(bf, size, style, baseColor);
font.setColor(baseColor);
} catch (Exception e) {
e.printStackTrace();
}
return font;
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
if (bais != null) {
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* docx 文件转pdf
* @param inPath docx 文件地址
* @param outputPdfPath 输出PDF 文件地址
*/
public static void docxToPdf(String inPath, String outputPdfPath){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream = new FileInputStream(inPath);
XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
fileOutputStream = new FileOutputStream(outputPdfPath);
PdfOptions pdfOptions = PdfOptions.create();
PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、测试代码书写
public class TestWord {
public static void main(String[] args) throws Exception {
//需要进行文本替换的信息
Map<String, Object> data = new HashMap<String, Object>();
data.put("date", "2021-03-11");
data.put("name", "东方明珠");
data.put("address", "北京华777ggggfSSS发鬼地方个东院");
//需要进行动态生成的信息
List<Object> mapList = new ArrayList<Object>();
//第一个动态生成的数据列表
List<String[]> list01 = new ArrayList<String[]>();
list01.add(new String[]{"A","已还款","22.36","22.02"});
list01.add(new String[]{"A","22222222222","33","22"});
list01.add(new String[]{"B","33333333333","44","22"});
list01.add(new String[]{"C","44444444444","55","22"});
//第二个动态生成的数据列表
List<String[]> list02 = new ArrayList<String[]>();
list02.add(new String[]{"A","11111111111","广发银行","22222222222222222222222222","张三","未还款","就这些还得用我还吗"});
list02.add(new String[]{"d","22222222222","33","22","11111111111","22","22"});
list02.add(new String[]{"B","33333333333","44","22","11111111111","22","22"});
list02.add(new String[]{"C","44444444444","55","22","11111111111","22","22"});
mapList.add(list01);
mapList.add(list02);
//需要动态改变表格的位置;第一个表格的位置为0
int[] placeList = {1,2};
String sourceFile = "C:\\Users\\Administrator\\Desktop\\工作笔记\\20210602\\还款记录\\还款记录.docx";
String sourceTOFile = "C:\\Users\\Administrator\\Desktop\\工作笔记\\20210602\\还款记录\\还款记录1.docx";
XWPFDocument doc = WorderToNewWordUtils.changWord(sourceFile,data,mapList,placeList);
FileOutputStream fopts = new FileOutputStream(sourceTOFile);
doc.write(fopts);
// 测试docx转pdf
String pdfPath = "C:\\Users\\Administrator\\Desktop\\工作笔记\\20210602\\还款记录\\呵呵11.pdf";
OfficeUtil.docxToPdf(sourceTOFile,pdfPath);
fopts.close();
}
}
4、生成文件结果
word格式 PDF格式