业务:
将富文本内容取出生成本地word文件
参考百度的方法
word本身是可以识别html标签,所以通过poi写入html内容即可
1、引用poi相关jar
2、直接生成本地doc文件(已测试)
public static Attachment createWordForHtml(String html,String fileName,String type) {
Attachment uploadAttachment = null;
try {
String dateDir = DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMdd");
String savePath = dateDir+File.separator;
File file = new File(Global.getConfig("userfiles.basedir")+File.separator+savePath);
if(!file.exists()) {
file.mkdirs();
}
savePath = Global.getConfig("userfiles.basedir")+File.separator+savePath+fileName+".doc"; //文件路径地址 :D:\apps\legislation\20210126\xxx.doc
//取出来的数据为转义的 先转一下
html = html.replace("<", "<").replace(">", ">").replace(""", "\"").replace("&", "&");
//word识别的html 必须是完整的 加上头和尾
String content="<html><body>"+html+"</body></html>";
byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //这块需要加上 直接 poifs.createDocument(stream, name) 不行
OutputStream ostream = new FileOutputStream(savePath); //导出到本地代码
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
//此处为持久化 不用看
// File dirFile = new File(savePath);
// FileInputStream fileInputStream = new FileInputStream(dirFile);
// MultipartFile multipartFile = new MockMultipartFile(dirFile.getName(), dirFile.getName(),
// ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
// uploadAttachment = FileUtils.uploadMultipartFile(multipartFile);
// uploadAttachment.setBusinessType(type);
// attachmentService.save(uploadAttachment);
// fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return uploadAttachment;
}
简化:
public static void createWordForHtml(String html,String fileName) {
try {
String savePath = "文件路径"+fileName+".doc";
html = html.replace("<", "<").replace(">", ">").replace(""", "\"").replace("&", "&");
String content="<html><body>"+html+"</body></html>";
byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
OutputStream ostream = new FileOutputStream(savePath);
poifs.writeFilesystem(ostream); //写入内容
bais.close();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3。前端直接下载(未测试)
//未测试 编码格式可能需要修改
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", "p_w_upload;filename=" +
new String( (documentEntry.getName() + ".doc").getBytes(), "iso-8859-1"));
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}catch(Exception e){
//异常处理
}
}