html格式的文档转成word下载

本文介绍了一种从前端使用UEditor编辑器获取HTML格式内容,并在后端将其转换为Word文档的方法。该过程涉及对特殊字符进行转义处理、创建包含内容的临时HTML字符串、利用POI组件构建文档结构,最终通过HTTP响应将文档发送给用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  当我们前端使用ueditor插件来让用户输入数据,保存至数据库。在另一个地方需要打印用户输入的内容的时候可以用到。因为要将ueditor带格式保存下来保存的就是html格式的内容,后台转化如下:

 1      @RequestMapping("ueditorPrintAction.do")
 2     public void uePrint(HttpServletRequest request,HttpServletResponse response) throws IOException{
 3         CivilParam param=new CivilParam();
 4         param.setParamData(null);
 5         CivilResult result=ueditorPrintService.queryContext(param);
 6         map=(Map) result.getResultData();
 7                 //以上是通过pring dubbo来进行数据库查询
 8         String htmlContent=map.get("content").toString();
 9         //字符替换
10         htmlContent = htmlContent.replaceAll("`lt`", "<");
11         htmlContent = htmlContent.replaceAll("`gt`", ">");
12         htmlContent = htmlContent.replaceAll("`quot`", "\"");
13         htmlContent = htmlContent.replaceAll("`#39`", "'");
14         
15         String content = "<html>"+htmlContent+"</html>";
16         //如果出现乱码getBytes传入编码参数
17         byte b[] = content.getBytes();
18         ByteArrayInputStream bais = new ByteArrayInputStream(b);    
19         POIFSFileSystem poifs = new POIFSFileSystem();    
20         DirectoryEntry directory = poifs.getRoot();    
21         DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);    
22         //输出文件 名
23         String name=map.get("name").toString();  
24         //如何出现乱码getBytes传入编码参数
25        // HttpServletResponse response = getResponse();
26         response.setHeader("Content-Disposition",  
27                 "attachment;filename=" +  
28                 new String( (name + ".doc").getBytes(),  
29                            "iso-8859-1"));  
30         response.setContentType("application/msword");  
31         OutputStream ostream = response.getOutputStream(); 
32         poifs.writeFilesystem(ostream);
33         //return JSON;
34     }            
View Code

 

转载于:https://www.cnblogs.com/many-object/p/7761797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值