java通过Poi word转Html

gradle依赖

	implementation 'org.apache.poi:poi:3.17'
    implementation 'org.apache.poi:poi-scratchpad:3.17'
    implementation 'org.apache.poi:poi-ooxml:3.17'
    implementation 'fr.opensagres.xdocreport:xdocreport:2.0.1'
    implementation 'org.apache.poi:poi-ooxml-schemas:3.17'
    implementation 'org.apache.poi:ooxml-schemas:1.4'
import fr.opensagres.poi.xwpf.converter.core.BasicURIResolver;
import fr.opensagres.poi.xwpf.converter.core.FileImageExtractor;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

/**
 * @author yichuan@iscas.ac.cn
 * @version 1.0
 * @date 2021/5/24 16:27
 */
public class WordToHtmlUtils {
    public static void main(String [] args) throws Exception {
        String path = "C:\\Users\\x\\Desktop\\";
        String wordName = "1.docx";
        try {
            wordToHTML(path+wordName,path);
            System.out.println("转换成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void wordToHTML(String wordFile,String HTMLFile) throws Exception{
        File file = new File(wordFile);
        String wordName = file.getName();
        String wordPath = file.getAbsolutePath().replace(wordName,"");
        String htmlName = wordName.substring(0,wordName.lastIndexOf("."))+".html";
        HTMLFile += htmlName;
        File f = new File(HTMLFile);
        InputStream in = new FileInputStream(wordFile);
        InputStream result = docxToHtml(in,wordPath);
        OutputStream outputStream = new FileOutputStream(f);
        int byteWritten = 0;
        int byteCount = 0;
        byte[] bytes = new byte[1024];
        while ((byteCount = result.read(bytes)) != -1){
            outputStream.write(bytes,byteWritten,byteCount);
        }
        in.close();
        result.close();
        outputStream.close();
    }

    public static InputStream docxToHtml(InputStream in,String filePath){
        XWPFDocument document = null;
        try {
            document = new XWPFDocument(in);
            XHTMLOptions options = XHTMLOptions.create();
            options.setIgnoreStylesIfUnused(false);
            options.setFragment(true);
            filePath = filePath+"image"+File.separator;
            options.setExtractor(new FileImageExtractor(new File(filePath)));
            options.URIResolver(new BasicURIResolver("image"));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            XHTMLConverter.getInstance().convert(document,out,options);
            return new ByteArrayInputStream(out.toByteArray());
        }catch (IOException e){

        }
        return  in;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值