中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

word導出html實現在線預覽

發布時間:2020-08-23 14:12:04 來源:網絡 閱讀:13211 作者:17099933344 欄目:軟件技術
需要的maven依賴經過編譯,必須版本配合一致:
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core -->
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
        <version>1.0.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.xhtml -->
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
        <version>1.0.5</version>
    </dependency><!-- 版本與上面一致 -->
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <!-- <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.16</version>
    </dependency> -->
    <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency><!-- poi引用的包要保持版本號一致,不然也會出現 ClassNotFoundException: org.apache.poi.wp.usermodel.Paragraph這個錯誤 -->
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

    package com.zyhao.openec.excel.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.IURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;

public class WordToHtml {
public static void main(String[] args) {
try {
wordToHtml("E:\me\2.docx", "E:\me\", "123.html");
// wordToHtml("E:\me\2.doc", "E:\me\", "12.html");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void wordToHtml(String wordPath,String htmlPath,String newFilename) throws Exception {    
        convert2Html(wordPath, htmlPath, newFilename);
}    

public static void writeFile(String content, String path) throws Exception {    
    FileOutputStream fos = null;    
    BufferedWriter bw = null;
    try {    
        File file = new File(path);
        fos = new FileOutputStream(file);    
        bw = new BufferedWriter(new OutputStreamWriter(fos));    
        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) {    
        }    
    }    
}    

/**
 * 將word轉換成html
 * 支持 .doc and .docx
 * @param fileName word文件名
 * @param outPutFilePath html存儲路徑
 * @param newFileName html名
 * @throws Exception 
 */
public static void convert2Html(String fileName, String outPutFilePath,String newFileName)    
        throws Exception {
    String substring = fileName.substring(fileName.lastIndexOf(".")+1);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    /**
     * word2007和word2003的構建方式不同,
     * 前者的構建方式是xml,后者的構建方式是dom樹。
     * 文件的后綴也不同,前者后綴為.docx,后者后綴為.doc
     * 相應的,apache.poi提供了不同的實現類。
     */
    if("docx".equals(substring)){

// writeFile(new String("<html><head> <meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/></head>對不起,.docx格式的word文檔,暫時不能生成預覽</html>".getBytes("utf-8")), outPutFilePath+newFileName);

        //step 1 : load DOCX into XWPFDocument
        InputStream inputStream = new FileInputStream(new File(fileName));
        XWPFDocument document = new XWPFDocument(inputStream);

        //step 2 : prepare XHTML options
        final String imageUrl = "";
        XHTMLOptions options = XHTMLOptions.create();
        options.setExtractor(new FileImageExtractor(new File(outPutFilePath + imageUrl)));
        options.setIgnoreStylesIfUnused(false);
        options.setFragment(true);
        options.URIResolver(new IURIResolver() {

// @Override 重寫的方法,加上這個報錯,你看看是啥問題
public String resolve(String uri) {
return imageUrl + uri;
}
});

        //step 3 : convert XWPFDocument to XHTML
        XHTMLConverter.getInstance().convert(document, out, options);

    }else{
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));    
        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 suggestedName;    
             }    
         } );    
        wordToHtmlConverter.processDocument(wordDocument);    
        //save pictures    
        List pics=wordDocument.getPicturesTable().getAllPictures();  

        if(pics!=null&&!pics.isEmpty()){

            for(int i=0;i<pics.size();i++){    
                Picture pic = (Picture)pics.get(i);    
                System.out.println();    
                try {    
                    pic.writeImageContent(new FileOutputStream(outPutFilePath    
                            + pic.suggestFullFileName()));    
                } catch (FileNotFoundException e) {    
                    e.printStackTrace();    
                }      
            }    
        }    
        Document htmlDocument = wordToHtmlConverter.getDocument();    
        DOMSource domSource = new DOMSource(htmlDocument);    
        StreamResult streamResult = new StreamResult(out);    

        TransformerFactory tf = TransformerFactory.newInstance();    //這個應該是轉換成xml的
        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()), outPutFilePath+newFileName);    
}

}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

威远县| 屯门区| 习水县| 锡林浩特市| 图木舒克市| 太谷县| 双江| 新和县| 凤冈县| 太康县| 锦州市| 屯留县| 阿勒泰市| 宜阳县| 苏尼特右旗| 库尔勒市| 临湘市| 通海县| 兴海县| 油尖旺区| 神农架林区| 沙田区| 绥中县| 广德县| 夹江县| 资中县| 监利县| 南雄市| 郧西县| 湘乡市| 会同县| 巴林左旗| 神池县| 买车| 岐山县| 韶山市| 湾仔区| 肥东县| 武宣县| 乌鲁木齐县| 枣庄市|