java解析word模板为xml,动态填充数据到xml,最后输出word⽂档
java 解析word模板为xml, 动态填充数据到xml,最后输出word⽂档
在项⽬中, ⼀开始是使⽤java Apache poi 根据word模板⽣成word报表, 后⾯发现框架有个低版本的poi 3.0.1, 由于这个版本太低, 新版本poi整合不了旧版本的poi, 所以使⽤了另外⼀种⽅式:
1,先将word⽂档另存为 : Word 2003 XML⽂档(*.xml) 格式的模板⽂件;
2.使⽤demo4j 在xml动态填充数据
3.把.xml模板后缀改成.doc(⼀开始是使⽤jacob, 后⾯觉得使⽤这个办法更⽅便)
上代码…
package com.csp.basic.util.word;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.*;
import Matcher;
import Pattern;
/**
* Created by Rosing on 2019/3/21.
*/
public class ExportDocUtil {
/
**
* @return
* @Description: 根据图⽚地址转换为base64编码字符串
* @Author:
* @CreateTime:
*/
public static String getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
de(data).trim();
}
public static void main(String[] args) {
List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
/*Map<String, Object> map = new HashMap<String, Object>();
map.put("rowNo","张三");
map.put("targetCode","男");
map.put("targetName","⼴州市");
map.put("specifications", "12121@qq");
map.put("specifications", "12121@qq");
map.put("currency", "打篮球");
map.put("unit", "⼈长得帅");
map.put("num", "12121@qq");
map.put("taxIncludedPrice", "打篮球");
map.put("taxIncludedAmount", "⼈长得帅");
Map<String, Object> map1 = new HashMap<String, Object>();        map1.put("rowNo","张三");
map1.put("targetCode","男");
map1.put("targetName","⼴州市");
map1.put("specifications", "12121@qq");
map1.put("currency", "打篮球");
map1.put("unit", "⼈长得帅");
map1.put("num", "12121@qq");
map1.put("taxIncludedPrice", "打篮球");
map1.put("taxIncludedAmount", "⼈长得帅");
datas.add(map);
datas.add(map1);*/
Map<String, Object> map1 = new HashMap<String, Object>();        map1.put("rowNo", "1");
map1.put("name", "男");
map1.put("sex", "⼴州市");
map1.put("intesting", "12121@qq");
map1.put("phone", "打篮球");
map1.put("remark", "⼈长得帅");
datas.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();        map2.put("rowNo", "2");
map2.put("name", "男");
map2.put("sex", "⼴州市");
map2.put("intesting", "12121@qq");
map2.put("phone", "打篮球");
map2.put("remark", "⼈长得帅");
datas.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();        map3.put("rowNo", "3");
map3.put("name", "男");
map3.put("sex", "⼴州市");
map3.put("intesting", "12121@qq");
map3.put("phone", "打篮球");
map3.put("remark", "⼈长得帅");
datas.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>();        map4.put("rowNo", "4");
map4.put("name", "男");
map4.put("sex", "⼴州市");
map4.put("intesting", "12121@qq");
map4.put("phone", "打篮球");
map4.put("remark", "⼈长得帅");
datas.add(map4);
Map<String, Object> map5 = new HashMap<String, Object>();        map5.put("rowNo", "5");
map5.put("name", "男");
map5.put("sex", "⼴州市");
map5.put("intesting", "12121@qq");
map5.put("phone", "打篮球");
map5.put("remark", "⼈长得帅");
datas.add(map5);
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
Map<String, Object> mapph = new HashMap<String, Object>();
mapph.put("eiName", "⼴州市新东⽅集团");
mapph.put("address", "⼴州市⼴州塔");
mapph.put("representative", "张三");
mapph.put("TableRemark", "总重量不超总重量吨,切板头板尾,宽度负差: -0mm/+2mm ");
mapph.put("mobile", "埃斯普咨询有限公司");
mapph.put("phone", "⼴州市番禺区海伦堡1座");
mapph.put("fax", "2222");
listMap.add(mapph);
List<Map<String, Object>> url_imgs = new ArrayList<Map<String, Object>>();
Map<String, Object> imgUrl = new HashMap<String, Object>();
imgUrl.put("picture", "G:\\poi\\srm\\新建⽂件夹\\新建⽂件夹\\图⽚\\IMG_9954_mr1461893871549.jpg");
imgUrl.put("picture2", "G:\\poi\\srm\\新建⽂件夹\\新建⽂件夹\\图⽚\\IMG20160418194124.jpg");
url_imgs.add(imgUrl);
使用dom4j解析xml文件// portDoc(datas,listMap,url_imgs,"G:\\poi\\srm\\新建⽂件夹\\合同模板.doc","G:\\poi\\srm\\新建⽂件夹\\123.doc","164","121");        exportDoc2Table(datas, listMap, "G:\\poi\\不锈钢\\moudue.doc", "G:\\poi\\不锈钢\\111.doc");
/*String base = getImageStr("D:\\picture\\picture\\DCIM\\DCIM\\IMG20160419143404.jpg");
GenerateImage(base,"");*/
// System.out.println(getImageStr("G:\\poi\\srm\\picture.jpg"));
/*try {
addImg2Word(listMap);
} catch (DocumentException e) {
e.printStackTrace();
}*/
//GetTagContent();
//  ateDoc();
/*
try {
changePatagraphText(listMap);
} catch (DocumentException e) {
e.printStackTrace();
}*/
}
/**
* 到{***} 标签内容
*
* @return
*/
public static Map GetTagContent(Document doc) {
Map<String, Object> map = new HashMap<String, Object>();
int num = 0;
try {
List<Element> nodeList = doc.selectNodes("//w:tbl//w:tr//w:tc//w:p//w:r//w:t");
if (nodeList.size() > 0) {
for (Element node : nodeList) {
String Text = "";
String regEx = "\\{.+?\\}";
Pattern pattern = Patternpile(regEx);
Matcher matcher = pattern.Text());//正则匹配字符串{****}
if (matcher.find()) {
String valText = Text();
int beginIndex = valText.indexOf("{");
int endIndex = valText.indexOf("}");
int length = valText.length();
Text = valText.substring(1, endIndex);
map.put(String.valueOf(num), Text);
num++;
// System.out.Text() + "--------------------------------------------------------------");
}
//  System.out.Name()+":"+node.valueOf("@name")+","+node.valueOf("@age") + Text());                }
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 删除标签 {***}
*
* @param doc
*/
public static void deleteElement2tr(Document doc) {
List<Element> nodeList = doc.selectNodes("//w:tbl//w:tr");
if (nodeList != null && nodeList.size() > 0) {
<(1).getParent().(1));
}
/* if (nodeList.size() > 0) {
Iterator<Element> (1).elementIterator();
while(childIter.hasNext()) {
Element childElement = ();
String tag = Name();
}
}*/
}
/**
* 改变段落内的⽂字 ${***}
*/
public static void changePatagraphText(List<Map<String, Object>> Paragraph, Document doc) {
List<Element> elements = doc.selectNodes("//w:t");
for (Element element : elements) {
String name = Text();
String regEx = "\\$\\{.+?\\}";
Pattern pattern = Patternpile(regEx);
Matcher matcher = pattern.matcher(name);//正则匹配字符串${****}
//  System.out.Text() + "--------------------");
if (matcher.find()) {
for (Map<String, Object> map : Paragraph) {
int endIndex = name.indexOf("}");
String key = name.substring(2, endIndex);
if (ainsKey(key)) {
if (ainsKey(key)) {
element.(key).toString());
}
}
}
}
}
/**
* 添加图⽚    ,Document doc
*/
public static void addImg2Word(List<Map<String, Object>> list_imgUrl, Document doc, String Width, String Height) throws DocumentException {
int num_index = 0;  //末尾有多少多余的图⽚, 删除
List<Element> parent_elements = new ArrayList<Element>();  //图⽚的w:p 集合
List<Element> sect_element = doc.selectNodes("//wx:sect");//body下⾯的标签, ⼀般只有⼀个
List<Element> elements = doc.selectNodes("//w:t");
Element element_parent = null;
for (Element element : elements) {
String name = Text();
String regEx = "\\{\\{.+?\\}\\}";
Pattern pattern = Patternpile(regEx);
Matcher matcher = pattern.matcher(name);//正则匹配字符串${****}
if (matcher.find()) {
for (Map<String, Object> map : list_imgUrl) {
int endIndex = name.indexOf("}}");
String key = name.substring(2, endIndex);
if (ainsKey(key)) {
/
/ System.out.println(key + "--------------------");
String img_url = getValueBykey(key, map);//图⽚路径
String base64_img = getImageStr(img_url);  //base64
element_parent = Parent().getParent();//到⽗级的⽗级===> w:p
Element element_parent_parent = Parent();到 w:p 的⽗级===> wx:sect
List<Element> list = element_t();//到 wx:sect下⾯的元素集合
int Element_index = list.indexOf(element_parent);//到下标\
//22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
Element r_element = Parent();      //=====>w:r
/*Iterator<Element> childIter=r_element.elementIterator();
while(childIter.hasNext())
{
Element ();
String Name();
if("name".equals(tag)&&name.Text()))
{
}
}*/
//222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
//====================================================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。