java怎么由html生成word,保留html样式

发布网友 发布时间:2022-04-20 04:48

我来回答

1个回答

热心网友 时间:2023-09-03 03:08

@RequestMapping("download")
public void exportWord( HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = AppContext.getLoginUser();
Student student = studentSvc.findByUserId(user.getId());
try {
//word内容
String content="<html><body></body></html>";
byte b[] = content.getBytes("utf-8"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream s = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式
*/
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", s);
//输出文件
String fileName="实习考核鉴定表";
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String( (fileName + ".doc").getBytes(),
"iso-8859-1"));

OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
s.close();
ostream.close();
}catch(Exception e){
AppUtils.logError("导出出错:%s", e.getMessage());
}
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
15.279126s