java实现word转html

现有的需求是前端导入word文件,然后需要在浏览器上展示出来,实现方案是将前端导入的word转成html的形式,再输出给前端,废话不多说,直接上代码.

需要用到的依赖

<!-- WordToHtml .doc .odcx  poi  -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>4.1.2</version>
</dependency>

<!-- 操作excel的库 注意版本保持一致 poi poi-ooxml  poi-scratchpad -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
    <version>2.0.2</version>
</dependency>

 代码实现

public static String word2007ToHtml(MultipartFile file) throws IOException {
        String html = null;
        if (file.isEmpty() || file.getSize() <= 0) {
            log.error("Sorry File does not Exists!");
            return null;
        } else {
            /* 判断是否为docx文件 */
            if (Objects.requireNonNull(file.getOriginalFilename()).endsWith(".docx") || file.getOriginalFilename().endsWith(".DOCX")) {
                // 1)加载word文档生成XWPFDocument对象
                @Cleanup FileInputStream in = (FileInputStream) file.getInputStream();
                XWPFDocument document = new XWPFDocument(in);
                // 2)解析XHTML配置(这里设置IURIResolver来设置图片存放的目录)
                XHTMLOptions options = XHTMLOptions.create();
                //3.将word中图片保存到oss中
                options.setImageManager(SpringUtil.getBean(ImageManagerImpl.class));
                options.setIgnoreStylesIfUnused(false);
                options.setFragment(true);
                // 3)将XWPFDocument转换成XHTML
                @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
                XHTMLConverter.getInstance().convert(document, baos, options);
                html = new String(baos.toByteArray(), StandardCharsets.UTF_8);
                System.out.println(html);

            } else {
                System.out.println("Enter only as MS Office 2007+ files");
                throw new BizException("word文档必须以.docx结尾");
            }
        }
        return html;
    }

注意事项

1.这个方法只支持docx结尾的文档,doc文档大同小异,如果有需要可以尝试自己写一下

2.和图片上传有关的这个类ImageManagerImpl,我就不写出来了,涉及公司的一些东西,如果你要用,只需要写个类继承一下 ImageManager 这个类,然后重写一下它的方法,也就是写一下你图片上传逻辑之类的就行了,如:

 private byte[] picture;
    private String suffix;
    private String fileName;
    public ImageManagerImpl() {
        super(new File(""), "");
    }


    @Override
    public void extract(String imagePath, byte[] imageData){
        this.fileName = imagePath.split("\\.")[0];
        this.suffix = "." + imagePath.split("\\.")[1];
        this.picture = imageData;
    }

    @SneakyThrows
    @Override
    public String resolve(String uri) {

        @Cleanup InputStream inputStream = new ByteArrayInputStream(picture);
        MultipartFile fileResult = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        return uploadFile(fileResult,UUID.randomUUID().toString().replaceAll("-", "")+suffix);

    }
3.生成后的html这个时候不能直接给前端,因为直接给前端的话会在传输过程中格式发生一些诡异的变化,我这里处理的方法是将html给base64加密,然后让前端去解析,这样就没问题了.

4. 其实这里生成的html都是一行行的div标签,并没有头和体的标签,虽然也能展示,但有些特殊用法的时候就不太行了,如html转PDF的时候,这个时候要么在生成html的时候加上去,要么在转PDF的时候加上去

htmlStr = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
        "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
        "\t<head></head><body style=\"font-family:SimSun;\">"+htmlDecode+"\t</body>\n" +
        "</html>";

注意这里是最后将html里面的字体全部统一成一种了,为了后期转PDF方便

好了,暂时就这么多,后续有时间再补充

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值