java生成word文档

摘要:
可以重servlet,classpath,数据库装载。

这是工具类代码

package common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

public class DocUtil {
private Configuration configure=null;
public DocUtil(){
configure=new Configuration();
configure.setDefaultEncoding("utf-8");
}
/**
* 根据Doc模板生成word文件
* @param dataMap 需要填入模板的数据
* @param downloadType 文件名称
* @param savePath 保存路径
*/
public void createDoc(Map<String,Object> dataMap,String downloadType,String savePath){
try {
//加载需要装填的模板
Template template=null;
//设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
//加载模板文件,放在testDoc下
configure.setClassForTemplateLoading(this.getClass(), "testDoc");
//设置对象包装器
// configure.setObjectWrapper(new DefaultObjectWrapper());
//设置异常处理器
configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
//定义Template对象,注意模板类型名字与downloadType要一致
template=configure.getTemplate(downloadType+".xml");
File outFile=new File(savePath);
Writer out=null;
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
template.process(dataMap, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}

这里模板路径为

java生成word文档第1张

public String getImageStr(String imgFile){
InputStream in=null;
byte[] data=null;
try {
in=new FileInputStream(imgFile);
data=new byte[in.available()];
in.read(data);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(data);
}

}

方法调用

@Override
public String exportWord(TeachMaterialDealInfo teachMaterialDealInfo,
String titleName) {
String s = UUID.randomUUID().toString();
String filepath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/");
String zippath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/week-job.zip");
String notice = ServletActionContext.getServletContext().getRealPath("upload/notice/");
createfile(notice);
createfile(filepath);
List<TeachMaterialDealInfo> teachMaterial = teachMaterialDealInfoDao.findDealInfo(teachMaterialDealInfo);
TeachMaterial teachMaterial1 = applicationDao.getMaterInfoByMaterId(teachMaterialDealInfo.getMaterid());
DocUtil docUtil=null;
Map<String, Object> dataMap=null;
....给dataMap赋值如dataMap.put("writername", “呵呵呵”);调用xml文件是就是通过这个来获取值得这里我用的是freemarker方法

docUtil.createDoc(dataMap, "basedoc", filepath+"/"+teachMaterial.get(i).getWritername()+".doc");

}

xml文件为先写好word文件后转存为xml

java生成word文档第2张

java生成word文档第3张

java生成word文档第4张

免责声明:文章转载自《java生成word文档》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇macOS Sierra 10.12.6 odoo 10.0 开发环境配置微信退款和支付宝退款接口调用(java版)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

python爬取图片遇见src乱码: data:image/png;base64

Python抓取图像并遇到src乱码:data:image/png;Base64会抓取您喜欢的图像,但图像的src在抓取的代码中出现了乱码:data:image/png;base64.)“”头1,编码=字符串。splitdata=b64decodedwithopenasf:f.写入。close()注意:我还没有成功。。。。。。...

CSS-顶部滚动进度条

Documentbody{background-image:linear-gradient(torighttop,#f0050%,#ece50%);background-repeat:no-repeat;height:300vh;position:relative;background-size:100%calc(100%-100vh+5px);}body:...

Spring Boot 核心配置文件 bootstrap &amp;amp; application

boostrap由父ApplicationContext加载,比applicaton优先加载boostrap里面的属性不能被覆盖3、bootstrap/application的应用场景application配置文件这个容易理解,主要用于SpringBoot项目的自动化配置。这个父级的SpringApplicationContext是先加载的,在加载appli...

【JVM】元空间详解 Metaspace

nocs。JpgNoKlassisMetaspaceNoKlassinMetaspaces专用于存储其他与klass相关的内容,如方法、常量池等。它可以由多个不连续的存储器组成。在元空间GC之后,还将调整阈值。默认情况下,MaxMetaspaceSize基本上是无限的,因为大多数元空间都是在本地内存中分配的,但它仍然受到本地内存大小的限制。为了防止元空间的无...

eeprom AT24C512 i2c总线的特点 来自eeleader的博客

今天,我花了一天时间研究i2c总线的特性。因为我想用FPGA来设计i2c总线的操作时序i2c总线是一种常用的串行总线。我想操作串行eepromAT24C512。EEPROM的容量为512KBIT,内部按照每页128字节进行组织,共512页。第一点:芯片工作速度的选择:根据AT24C512手册,芯片可以在1.8V-5.0V的电压下工作;I2C总线的最大运行速度...

WPF 制作圆角按钮

在程序对应坐置插入以下代码,或是先拖一个按钮控件到窗体中,再替换对应的代码。...