java Springboot富文本编辑器ueditor内容使用poi导出为word文件

转自:https://blog.csdn.net/weixin_42259631/article/details/80996198

Spring boot + Undertow + 腾讯云https证书配置SSL访问项目( 多个项目使用SSL也可以,需要利用Nginx ): https://blog.csdn.net/jie873440996/article/details/80801446

创建spring boot项目时无法访问https://start.spring.io的解决方法: https://blog.csdn.net/cslucifer/article/details/83573594

Spring boot 项目 配置https添加服务端客户端证书认证: https://blog.csdn.net/qq_26531719/article/details/82378477

 

本文讲解在springboot环境下,将ueditor保存到数据库中的html内容使用poi导出为word文件,亲测导出的文件在word和wps上打开均正常显示。

首先,在pom.xml文件中引入poi包

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
</dependency>
然后在Service层中Impl下写导出函数

    public void exportWord(HttpServletRequest request, HttpServletResponse response, String title, String text) {
 
        try {
            //word内容
            String content="<html><body>" +
                    "<p style=\"text-align: center;\"><span style=\"font-family: 黑体, SimHei; font-size: 24px;\">"
                    + title + "</span></p>" + text + "</body></html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤在Spring Boot中集成UEditor富文本编辑器: 1.下载UEditor插件,将其解压缩到项目的静态资源目录(如:src/main/resources/static/ueditor)中。 2.在Spring Boot的配置文件application.properties或application.yml中添加如下配置: ```properties spring.resources.static-locations=classpath:/static/ ``` 或 ```yaml spring: resources: static-locations: classpath:/static/ ``` 3.编写一个Controller来访问UEditor插件,代码如下: ```java @Controller @RequestMapping("/ueditor") public class UeditorController { @GetMapping("/") public String index() { return "ueditor/index"; } @PostMapping("/upload") @ResponseBody public String upload(HttpServletRequest request) { String result = ""; try { // 获取UEditor上传的文件 MultipartFile file = ((MultipartHttpServletRequest) request).getFile("upfile"); // 获取文件名 String fileName = file.getOriginalFilename(); // 获取文件后缀 String suffix = fileName.substring(fileName.lastIndexOf(".")); // 生成新的文件名 String newFileName = UUID.randomUUID().toString() + suffix; // 获取文件保存路径 String savePath = "src/main/resources/static/ueditor/upload/"; File saveFile = new File(savePath + newFileName); // 保存文件 file.transferTo(saveFile); // 返回文件访问路径 result = "{\"state\":\"SUCCESS\",\"url\":\"/ueditor/upload/" + newFileName + "\",\"title\":\"" + fileName + "\",\"original\":\"" + fileName + "\"}"; } catch (Exception e) { e.printStackTrace(); result = "{\"state\":\"ERROR\"}"; } return result; } } ``` 4.在静态资源目录中创建一个index.html文件,代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>UEditor Demo</title> <!-- 引入UEditor --> <script type="text/javascript" src="/ueditor/ueditor.config.js"></script> <script type="text/javascript" src="/ueditor/ueditor.all.min.js"></script> <script type="text/javascript" src="/ueditor/lang/zh-cn/zh-cn.js"></script> </head> <body> <!-- 编辑器容器 --> <script id="editor" type="text/plain"></script> <!-- 实例化编辑器 --> <script type="text/javascript"> var editor = UE.getEditor('editor'); </script> </body> </html> ``` 5.启动应用,访问http://localhost:8080/ueditor/,即可看到UEditor富文本编辑器。 注意:在上传文件时,需要先创建一个文件夹(如:upload),并将其放在静态资源目录中。同时,需要对文件保存路径进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值