栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java docx 转html

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java docx 转html

java docx 转html
  • 概述
  • 具体步骤
    • 1.引入jar包
    • 2.编写代码

概述

在网上找了一些docx转html发现很多都有问题,自己在踩坑之后再次记录一下

具体步骤 1.引入jar包
       
            org.apache.poi
            poi
            3.14
        
        
            org.apache.poi
            poi-scratchpad
            3.14
        
        
            org.apache.poi
            poi-ooxml
            3.14
        
        
            org.apache.poi
            poi-ooxml-schemas
            3.14
        
        
            org.apache.poi
            ooxml-schemas
            1.3
        
         
            fr.opensagres.xdocreport
            fr.opensagres.xdocreport.document
            2.0.1
        
         
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.xhtml
            1.0.6
        
2.编写代码
public class Word2Html {

    private final static String  tempPath = "C:/Users/11826/Desktop/docPicture";

    public static void main(String argv[]) {
        try {
            String fileName="C:/Users/11826/Desktop/1417636763975913474.docx";
            String outPutFile="C:/Users/11826/Desktop/1447488304717033473.html";
            //doc2Html(fileName, outPutFile);
            docx2Html(fileName, outPutFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static void docx2Html(String fileName, String outPutFile) throws TransformerException, IOException,     ParserConfigurationException {
        XWPFdocument document = new XWPFdocument(new FileInputStream(fileName));
        XHTMLOptions options = XHTMLOptions.create();
        options.setIgnoreStylesIfUnused(true);
        // 导出图片
        File imageFolder = new File(tempPath);
        options.setExtractor(new FileImageExtractor(imageFolder));
        // URI resolver
        options.URIResolver(new FileURIResolver(imageFolder));
       
        
        //获取转化后的html字符串使用此代码
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        XHTMLConverter.getInstance().convert(document, out, options);
        System.out.println(out.toString());
    }

    
    public static void doc2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {
        File file =   new File(fileName);
        FileInputStream fileInputStream = new FileInputStream(file);
        System.out.println(fileInputStream);
        HWPFdocument worddocument = new HWPFdocument(fileInputStream);
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(documentBuilderFactory.newInstance().newdocumentBuilder().newdocument());
        wordToHtmlConverter.setPicturesManager(new PicturesManager() {
            public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
                return "test/" + suggestedName;
            }
        });
        wordToHtmlConverter.processdocument(worddocument);
        // 保存图片
        List pics = worddocument.getPicturesTable().getAllPictures();
        if (pics != null) {
            for (int i = 0; i < pics.size(); i++) {
                Picture pic = (Picture) pics.get(i);
                System.out.println();
                try {
                    pic.writeImageContent(new FileOutputStream(tempPath + pic.suggestFullFileName()));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        document htmldocument = wordToHtmlConverter.getdocument();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(htmldocument);
        StreamResult streamResult = new StreamResult(out);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        out.close();
        writeFile(new String(out.toByteArray()), outPutFile);
    }

    
    public static void writeFile(String content, String path) {
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try {
            File file = new File(path);
            fos = new FileOutputStream(file);
            bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
            bw.write(content);
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (fos != null)
                    fos.close();
            } catch (IOException ie) {
            }
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号