word导入富文本编辑器 ( 带格式,带图片)

public String download(MultipartFile file, String url) throws Exception {
        String fileName = file.getOriginalFilename();
        //获取文件类型
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        String doc = "doc";
        String docx = "docx";
        String html = "html";
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        String wordName = IdUtil.fastSimpleUUID();
        //        要转换的临时文件
        String targetFileName ="E:/" year + "/" + month + "/" + day + "/" + wordName + "/";
        String saveUrl = targetFileName + wordName + "." + html;
        //文件类型如果不是doc或者docx返回错误
        if (doc.equals(fileType) || docx.equals(fileType)) {
            try {
                //去水印
                if (!PdfUtils1.getLicense()) {
                    throw new Exception("com.aspose.words lic ERROR!");
                }
                //读取word
                try (InputStream fileInputStream = file.getInputStream()) {
                    Document document = new Document(fileInputStream);
                    //生成临时html
                    document.save(saveUrl, new HtmlSaveOptions(SaveFormat.HTML));
                    //读取文件内容
//                    targetFileName:文件夹地址
                    //saveUrl:文件的保存路径
//                    url:url是从前端获取的服务器端口加读取文件返回留的接口
//                    window.location.origin +
//                            "/zhbgapi?fileurl=",
                    String fileContent = htmlPath(targetFileName, saveUrl, url);

                    return fileContent;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

 这个方法是将word转成html然后读取html文本  然后遍历文件中所有文件

如果有图片存在将src=“图片路径'换成ser=“你的文件文件流接口+文件地址”代码如下

public String htmlPath(String targetFileName, String htmlPath, String url) {

        File htmlFile = new File(htmlPath);
//         读取 HTML 文件内容
        try (FileReader fileReader = new FileReader(htmlFile);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            StringBuilder stringBuilder = new StringBuilder();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuilder.append(line);
                line = bufferedReader.readLine();
            }

            String htmlContent = stringBuilder.toString();
            //遍历html所在的文件夹获取所有图片名称
            File file = new File(targetFileName);
            File[] files = file.listFiles();
            if (files.length > 0) {
                for (File file1 : files) {
                    String fileExtension = "";
                    String fileName = file1.getName();
                    int dotIndex = fileName.lastIndexOf(".");
                    //获取文件后缀
                    if (dotIndex != -1 && dotIndex < fileName.length() - 1) {
                        fileExtension = fileName.substring(dotIndex + 1);
                    }
                    //如果后缀是图片的话将原html中的文件路径置换成可以访问的路径
                    boolean existed = "png".equals(fileExtension) || ("jpg".equals(fileExtension) || ("jpeg".equals(fileExtension)));
                    if (existed) {
                        htmlContent = htmlContent.replace(fileName, url + targetFileName + fileName);
                    }
                }
            }
            bufferedReader.close();
            fileReader.close();
            return htmlContent;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

将返回的字符串直接放到富文本上就可以回显啦,打完收工!!!!

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue中,文本编辑器通常用于编辑和展示HTML格式文本。虽然Vue文本编辑器可以处理和生成HTML格式的内容,但是直接导入Word文件并进行编辑是一个复杂的任务。这是因为Word文件和HTML之间存在着很大的差异,包括样式、布局、图像等方面。 如果你需要在Vue中导入Word文件并进行编辑,可以考虑以下步骤: 1. 将Word文件转换为HTML格式:使用第三方库(例如`mammoth.js`)将Word文件转换为HTML格式。这样你就可以将HTML内容加载到Vue的文本编辑器中进行编辑。 2. 在Vue中使用文本编辑器:使用Vue文本编辑器(例如`vue-quill-editor`)将转换后的HTML内容加载到编辑器中。用户可以对内容进行编辑、样式调整等操作。 3. 导出编辑后的内容:当用户完成编辑后,你可以将编辑后的HTML内容导出为Word文件。使用第三方库(例如`html-docx-js`)将HTML转换为可下载的Word文件。 需要注意的是,由于Word和HTML之间的差异较大,转换和导出过程可能会引入一些格式方面的不一致。因此,在实现导入和导出功能时,可能需要进行一些额外的处理和调整,以确保最终结果符合预期。 总之,实现在Vue中导入Word文件并进行编辑是一个相对复杂的任务,需要借助一些第三方库来完成。以上提到的库只是其中的一些示例,你可以根据自己的需求选择适合的库来实现该功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值