java生成word排版_java生成word(文字和图片)

java⽣成word排版_java⽣成word(⽂字和图⽚)
1、整体思路
利⽤xml模板,在模板中预留占位标识(${yourContent}),然后将xml转为ftl⽂件,通过Map传值填充对应的内容即可,word其实和html⼀样,也有⾃⼰的xml标签,表头、段落、图⽚、以及字体、标题等的标签。⽂字必须包含在段落中,如:
${yourContent}
,图⽚必须是在
${image}v槽机
其中、和有⼏个必填参数,后⾯的代码会涉及到。集合的循环遍历通过${alias.property}...#list>⽅式实现。
2、⽣成模板
新建word模板,设置⾃⼰要替换的内容。⽐如下⾯例⼦:
我的⽂档
作者:author 时间:time 内容:content
把新建好的word导出成word2003xml⽂件,然后将author改成${author},time改成${time},content改成${content}然后修改后缀名为ftl⽂件,存到项⽬⾥⾯。
3、替换内容
实际操作中,不光会遇到纯⽂字的,经常会遇到⽂字加图⽚。图⽚的实现:先转换成base64的字符串,然后填充到图⽚的标签中(⽹络图⽚,必须先下载到本地才能转换)。下⾯是转换的代码:
----------
/**
* 替换内容中的图⽚,样式
*
* @param content
* @return
* @throws Exception
*/
public String replaceImage(String content) throws Exception {
try {
Pattern p = Patternpile("]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
Matcher m = p.matcher(content);
int i = 0;硒化卡拉胶
content = StyleFilter.shieldStyle(content);
while (m.find()) {
abp-356String width = up());
String height = up());
String target = up(1), width, height);
content = up(), target);
}
} catch (ConfigurationException e) {
e.printStackTrace();
}
return content;
}
public String getWidth(String content) {
String regex = "width=['\"]?(.*?)['\"]?\\s.*?>";
Pattern p = Patternpile(regex);
Matcher m = p.matcher(content);
while (m.find()) {
up(1);
}
return null;
}
public String getHeight(String content) {
String regex = "height=['\"]?(.*?)['\"]?\\s.*?>";
Pattern p = Patternpile(regex);
Matcher m = p.matcher(content);
while (m.find()) {
up(1);
}
return null;
}
/**
* ⽣成图⽚(独⽴成段落的图⽚,仅前半部分,后半部分⼿动拼接) *
* @param imgUrl
* @param width
* @param height
* @return
* @throws Exception
*/
public String getWholeImage(String imgUrl, String width, String height) throws Exception {
String result = "";
try {
if (null != width && null != height) {
String no = Math.random() * 100 + "";
String binData1 = "";
String binData2 = "
+ ";height:" + height + "\">
";
result = binData1 + getImageStr(imgUrl) + binData2 + "";
} else {
String no = Math.random() * 100 + "";
String binData1 = "";
String binData2 = "
+ "\" type=\"#_x0000_t75\">
";
result = binData1 + getImageStr(imgUrl) + binData2 + "";
}
} catch (ConfigurationException e) {
e.printStackTrace();
}
return result;
}
其中这两块的名字必须是⼀致的![](leanote://file/getImage?fileId=57885a94128c7b03ba000000)
/**
* 图⽚转码
*
* @return 返回图⽚base64字符串
* @throws Exception
*/
public static String getImageStr(String imgUrl) throws Exception {
String imgPath = download(imgUrl, Math.random() * 100 + ".png", Config.ReadStringPropertie("pictureDownload")); InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.close();
文字拼接
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
混凝土泵送剂
de(data);
}
/**
* 下载图⽚的⽅法(图⽚下载到本地后才可转化成base64字符串)
*
* @param urlString
* @param fileName
* @param savePath
* @return
* @throws Exception
*/
public static String download(String urlString, String fileName, String savePath) throws Exception { // 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输⼊流
InputStream is = InputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的⽂件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
String imgUrl = sf.getPath() + "\\" + fileName;
OutputStream os = new Path() + "\\" + fileName);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
return imgUrl;
}
电子设备包括哪些
enter code here
调⽤replaceImage⽅法获取处理过的字符串,然后赋值到Map中,再调⽤创建word的⽅法:
public String createWord(Map dataMap) {
String path = "/myWrongBook" + Math.random() * 3 + ".doc";
try {
Template t = null;
configuration.Class(), "your template path"); // FTL⽂件所存在的位置t = Template("template.ftl");
File outFile = new File(Config.ReadStringPropertie("wrongBooks") + path);
Writer out = null;
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));

本文发布于:2024-09-23 10:27:22,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/344881.html

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

标签:内容   字符串   模板
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议