您的位置:首页 > 编程语言 > Java开发

Java使用poi把html转word保存

2017-01-14 11:05 489 查看
public void exportWord( HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
//word内容
String content="<html><body></body></html>";
byte b[] = content.getBytes("utf-8");  //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("文档名称", bais);
//输出文件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String( (documentEntry.getName() + ".doc").getBytes(),  "iso-8859-1"));
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}catch(Exception e){
//异常处理
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html word