java将html转为word导出(富⽂本内容导出word)
业务:
将富⽂本内容取出⽣成本地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 Config("userfiles.basedir")+File.separator+savePath);
if(!ists()) {
file.mkdirs();
}
savePath = Config("userfiles.basedir")+File.separator+savePath+fileName+".doc"; //⽂件路径地址:D:\apps\legislation\20210126\xxx.doc
//取出来的数据为转义的先转⼀下
html = place("<", "<").replace(">", ">").replace(""", "\"").replace("&", "&");
//word识别的html 必须是完整的加上头和尾
String content="<html><body>"+html+"</body></html>";
byte b[] = Bytes("GBK"); //这⾥是必须要设置编码的,不然导出中⽂就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地⽅
* ⽣成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = Root();
DocumentEntry documentEntry = ateDocument("WordDocument", bais); //这块需要加上直接 ateDocument(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 Name(), Name(),
// ContentType.APPLICATION_String(), fileInputStream);
// uploadAttachment = FileUtils.uploadMultipartFile(multipartFile);
// uploadAttachment.setBusinessType(type);
// attachmentService.save(uploadAttachment);
// fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return uploadAttachment;
}
简化:
getsavefilenamepublic static void createWordForHtml(String html,String fileName) {
try {
String savePath = "⽂件路径"+fileName+".doc";
html = place("<", "<").replace(">", ">").replace(""", "\"").replace("&", "&");
String content="<html><body>"+html+"</body></html>";
byte b[] = Bytes("GBK"); //这⾥是必须要设置编码的,不然导出中⽂就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/
*
* 关键地⽅
* ⽣成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = Root();
DocumentEntry documentEntry = ateDocument("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[] = Bytes("utf-8"); //这⾥是必须要设置编码的,不然导出中⽂就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地⽅
* ⽣成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = Root();
DocumentEntry documentEntry = ateDocument("⽂档名称", bais);
//输出⽂件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "p_w_upload;filename=" +
new String( (Name() + ".doc").getBytes(), "iso-8859-1"));
OutputStream ostream = OutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}catch(Exception e){
//异常处理
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论