package com.gsy.tool.utils;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
public class WordS {
/*public static void main(String[] args) throws Exception {
InputStream in1 = null;
InputStream in2 = null;
OPCPackage src1Package = null;
OPCPackage src2Package = null;
OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");
in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
in2 = new FileInputStream("D:\\aaaaa\\wwww2.docx");
//src1Package = OPCPackage.open(in1);
src2Package = OPCPackage.open(in2);
XWPFDocument src1Document;// = new XWPFDocument(src1Package);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("birthday", "SDFASDFADSF");
XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
src1Document = render.getXWPFDocument();
CTBody src1Body = src1Document.getDocument().getBody();
XWPFParagraph p = src1Document.createParagraph();
//设置分页符
p.setPageBreak(true);
XWPFDocument src2Document = new XWPFDocument(src2Package);
CTBody src2Body = src2Document.getDocument().getBody();
appendBody(src1Body, src2Body);
src1Document.write(dest);
}*/
/* public static void main(String[] args) throws Exception {
OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");
ArrayList<XWPFDocument> xwpfDocuments = new ArrayList<>();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("birthday", "SDFASDFADSF");
InputStream in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
XWPFDocument src1Document = render.getXWPFDocument();
xwpfDocuments.add(src1Document);
}
XWPFDocument appendbodys = appendbodys(xwpfDocuments);
appendbodys.write(dest);
}*/
public static void main(String[] args) throws Exception {
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");
for (int i = 0; i < 10; i++) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("birthday", "SDFASDFADSF " + i);
arrayList.add(hashMap);
}
getWords(arrayList, dest);
}
public static void getWords(ArrayList<HashMap<String, Object>> arrayList, OutputStream dest) throws Exception {
ArrayList<XWPFDocument> xwpfDocuments = new ArrayList<>();
for (HashMap<String, Object> hashMap : arrayList) {
InputStream in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
XWPFDocument src1Document = render.getXWPFDocument();
xwpfDocuments.add(src1Document);
}
XWPFDocument appendbodys = appendXWPFDocuments(xwpfDocuments);
appendbodys.write(dest);
}
public static XWPFDocument appendXWPFDocuments(ArrayList<XWPFDocument> arrayList) throws Exception {
XWPFDocument xwpfDocument = arrayList.get(0);
if (arrayList.size() == 0) {
return xwpfDocument;
}
for (int i = 1; i < arrayList.size(); i++) {
XWPFDocument xwpfDocument1 = arrayList.get(i);
XWPFParagraph p = xwpfDocument.createParagraph();
//设置分页符
p.setPageBreak(true);
xwpfDocument = appendXWPFDocument(xwpfDocument, xwpfDocument1);
}
return xwpfDocument;
}
private static XWPFDocument appendXWPFDocument(XWPFDocument src, XWPFDocument append) throws Exception {
CTBody body = src.getDocument().getBody();
CTBody body1 = append.getDocument().getBody();
appendBody(body, body1);
return src;
}
private static void appendBody(CTBody src, CTBody append) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String sufix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
src.set(makeBody);
}
}
效果
前
未 分页 后
分页