1. 程式人生 > 其它 >doc以及docx文件轉html檔案(同時解析圖片、音訊和視訊)

doc以及docx文件轉html檔案(同時解析圖片、音訊和視訊)

目錄

話就不多說了,就是用到了doc/docx轉html的需求了,例如:檔案預覽,可以把doc/docx檔案轉html然後預覽!

1,加入poi依賴解析word

 		<!--docx轉html-->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>

2,程式碼實現

	@Value("${file.upload}") // D:\test\files
    private String path;

    @RequestMapping("/testDocx")
    public void testDocx() throws ParserConfigurationException, TransformerException, IOException {
        docx2Html("D:\\工作\\表單電子化\\02-test.docx","D:\\工作\\表單電子化\\02-test.html");
    }

    @RequestMapping("/testDoc")
    public void testDoc() throws ParserConfigurationException, TransformerException, IOException {
        doc2Html("D:\\工作\\表單電子化\\test(王大剛_java).doc","D:\\工作\\表單電子化\\test(王大剛_java).html");
    }

    /**
     * docx轉html
     * @param docxFileUrl
     * @param htmlOutPutUrl
     */
    public void docx2Html(String docxFileUrl, String htmlOutPutUrl) throws TransformerException, IOException, ParserConfigurationException {
        String fileOutName = htmlOutPutUrl;
        XWPFDocument document = new XWPFDocument(new FileInputStream(docxFileUrl));
        XHTMLOptions options = XHTMLOptions.create().indent(4);
        // 匯出圖片
        File imageFolder = new File(path);
        options.setExtractor(new FileImageExtractor(imageFolder));
        // URI resolver
        options.URIResolver(new FileURIResolver(imageFolder));
        File outFile = new File(fileOutName);
        outFile.getParentFile().mkdirs();
        OutputStream out = new FileOutputStream(outFile);
        XHTMLConverter.getInstance().convert(document, out, options);
    }

    /**
     * doc轉html
     * @param docFileUrl
     * @param htmlOutPutUrl
     */
    public void doc2Html(String docFileUrl, String htmlOutPutUrl) throws IOException, ParserConfigurationException, TransformerException {
        File file=new File(docFileUrl);
        FileInputStream fileInputStream=null;
        try {
             fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            logger.error("===>docx轉輸入流失敗");
        }
        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) {
                //src="test/0.png
                return path+File.separator + suggestedName;
            }
        });
        wordToHtmlConverter.processDocument(wordDocument);
        // 儲存圖片
        List<Picture> 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 {
                    //儲存圖片到path路徑下
                    pic.writeImageContent(new FileOutputStream(path+File.separator + 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()), htmlOutPutUrl);
    }

    /**
     * 寫檔案
     */
    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) {
            }
        }
    }