java 生成word_Java Web 生成Word文档(freemarker方式)

首先在pom文件中加入下面这个依赖(不是Maven项目的话,把jar包导入项目即可)

org.freemarker

freemarker

2.3.23

1.创建带有格式的word文档,将该需要动态展示的数据使用变量符替换。如下:

ecf9276973d79288d1de560141fc95b1.png

2.将上述1所创建的文档另存为.xml格式。

3.编辑上述2的.xml文件去掉多余的xml标记,如下图(红圈是正确的格式,应该由${变量符}包裹着)

25d5a1f08c2f3cbda4b0edc0004ff56e.png

4.将上述3的文档另存为.ftl格式,然后复制到Web项目(下图目录)。

cdc66d98a81fdbbfbb9b2152ab4a3872.png

5.JSP页面提供生成word文档的按钮。

导出Word

JS代码:

//生成word文档

function exportWord(rightId){

layer.confirm('确认要生成word文档吗?',function(index){

//关闭窗口

layer.close(index);

window.location.href ="/biz/SysRight_exportWord.action?rightId="+rightId+""

});

}

效果如下:

b1b1fd6160a8d63ef1ca7c1c63494ea5.png

6.Action中的exportWord()方法。

/**

* 生成word文档(测试使用)

* @Description: TODO

* @param @return

* @param @throws Exception

* @return String

* @throws

* @author uug

* @date 2018年12月9日

*/

public String exportWord() throws Exception {

//需要显示的数据

sysRight = sysRightService.exportWord(sysRight.getRightId());

HttpServletResponse response = ServletActionContext.getResponse();

HttpServletRequest request = ServletActionContext.getRequest();

//创建Map

Map map = new HashMap();

//将数据put到Map中,第一个参数为.ftl文件中对应的${}名称,第二个参数为需要显示的内容

map.put("rightId",sysRight.getRightId());

map.put("rightName",sysRight.getRightName());

map.put("resourcePath",sysRight.getResourcePath());

map.put("rightPid",sysRight.getRightPid());

//需要调用的模板名称

String downloadType = "test";

//导出时的名称

String fileName = "权限列表";

WordUtils.exportMillCertificateWord(request,response,map,downloadType , fileName);

return null;

}

6.WordUtils工具类。

@SuppressWarnings("deprecation")

public class WordUtils {

//配置信息

private static Configuration configuration = null;

//这里注意的是利用WordUtils的类加载器动态获得模板文件的位置

private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "WEB-INF/asserts/templete/";

//用于存放所有的.ftl文件

private static Map allTemplates = null;

static {

configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

allTemplates = new HashMap();

try {

configuration.setDirectoryForTemplateLoading(new File(templateFolder));

//将test.ftl文件存入Map

allTemplates.put("test", configuration.getTemplate("test.ftl"));

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

private WordUtils() {

throw new AssertionError();

}

/**

*

* @Description: TODO

* @param @param request

* @param @param response

* @param @param map 写入文件的数据

* @param @param downloadType 需要调用Map allTemplates对应的那个.ftl文件

* @param @param fileName0 设置浏览器以下载的方式处理该文件名

* @param @throws IOException

* @return void

* @throws

* @author uug

* @date 2018年12月9日

*/

public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map, String downloadType, String fileName0) throws IOException {

//定义Template对象,注意模板类型名字与downloadType要一致

Template template= allTemplates.get(downloadType);

File file = null;

InputStream fin = null;

ServletOutputStream out = null;

try {

// 调用工具类的createDoc方法生成Word文档

file = createDoc(map,template);

fin = new FileInputStream(file);

response.setCharacterEncoding("utf-8");

response.setContentType("application/msword");

// 设置浏览器以下载的方式处理该文件名

String fileName = fileName0 + ".doc";

response.setHeader("Content-Disposition", "attachment;filename="

.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));

out = response.getOutputStream();

byte[] buffer = new byte[512]; // 缓冲区

int bytesToRead = -1;

// 通过循环将读入的Word文件的内容输出到浏览器中

while((bytesToRead = fin.read(buffer)) != -1) {

out.write(buffer, 0, bytesToRead);

}

} finally {

if(fin != null) fin.close();

if(out != null) out.close();

if(file != null) file.delete(); // 删除临时文件

}

}

/**

* 创建doc文档

* @Description: TODO

* @param @param dataMap

* @param @param template

* @param @return

* @return File

* @throws

* @author uug

* @date 2018年12月9日

*/

private static File createDoc(Map, ?> dataMap, Template template) {

String name = "temp" + (int) (Math.random() * 100000) + ".doc";

File f = new File(name);

Template t = template;

try {

// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开

Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");

t.process(dataMap, w);

w.close();

} catch (Exception ex) {

ex.printStackTrace();

throw new RuntimeException(ex);

}

return f;

}

}

7.效果如下:

063ef398f3b414f6d515f37e3a6294f4.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值