国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

javaword文檔轉(zhuǎn)html文件

2019-11-14 15:39:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、簡(jiǎn)介
  一般Word文件后綴有doc、docx兩種。docx是office word 2007以及以后版本文檔的擴(kuò)展名;doc是office word 2003文檔保存的擴(kuò)展名。對(duì)于這兩種格式的word轉(zhuǎn)換成html需要使用不同的方法。
對(duì)于docx格式的文檔使用xdocreport進(jìn)行轉(zhuǎn)換。依賴(lài)如下:

<dependency>    <groupId>fr.opensagres.xdocreport</groupId>    <artifactId>fr.opensagres.xdocreport.document</artifactId>    <version>1.0.5</version></dependency><dependency>      <groupId>fr.opensagres.xdocreport</groupId>      <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>      <version>1.0.5</version>  </dependency>

對(duì)于docx格式的文檔使用poi進(jìn)行轉(zhuǎn)換。依賴(lài)如下:

<dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi</artifactId>    <version>3.12</version></dependency><dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi-scratchpad</artifactId>    <version>3.12</version></dependency>

二:示例
  代碼示例如下:

  1 package com.test.word;  2   3 import java.io.File;  4 import java.io.FileInputStream;  5 import java.io.FileNotFoundException;  6 import java.io.FileOutputStream;  7 import java.io.IOException;  8 import java.io.InputStream;  9 import java.io.OutputStream; 10  11 import javax.xml.parsers.DocumentBuilderFactory; 12 import javax.xml.parsers.ParserConfigurationException; 13 import javax.xml.transform.OutputKeys; 14 import javax.xml.transform.Transformer; 15 import javax.xml.transform.TransformerException; 16 import javax.xml.transform.TransformerFactory; 17 import javax.xml.transform.dom.DOMSource; 18 import javax.xml.transform.stream.StreamResult; 19  20 import org.apache.poi.hwpf.HWPFDocument; 21 import org.apache.poi.hwpf.converter.PicturesManager; 22 import org.apache.poi.hwpf.converter.WordToHtmlConverter; 23 import org.apache.poi.hwpf.usermodel.PictureType; 24 import org.apache.poi.xwpf.converter.core.FileImageExtractor; 25 import org.apache.poi.xwpf.converter.core.FileURIResolver; 26 import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; 27 import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; 28 import org.apache.poi.xwpf.usermodel.XWPFDocument; 29 import org.junit.Test; 30 import org.w3c.dom.Document; 31  32 /** 33  * word 轉(zhuǎn)換成html 34  */ 35 public class WordToHtml { 36      37     /** 38      * 2007版本word轉(zhuǎn)換成html 39      * @throws IOException 40      */ 41     @Test  42     public void Word2007ToHtml() throws IOException { 43         String filepath = "C:/test/"; 44         String fileName = "滕王閣序2007.docx"; 45         String htmlName = "滕王閣序2007.html"; 46         final String file = filepath + fileName; 47         File f = new File(file);   48         if (!f.exists()) {   49             System.out.);   50         } else {   51             if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {   52                    53                 // 1) 加載word文檔生成 XWPFDocument對(duì)象   54                 InputStream in = new FileInputStream(f);   55                 XWPFDocument document = new XWPFDocument(in);   56    57                 // 2) 解析 XHTML配置 (這里設(shè)置IURIResolver來(lái)設(shè)置圖片存放的目錄)   58                 File imageFolderFile = new File(filepath);   59                 XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));   60                 options.setExtractor(new FileImageExtractor(imageFolderFile));   61                 options.setIgnoreStylesIfUnused(false);   62                 options.setFragment(true);   63                    64                 // 3) 將 XWPFDocument轉(zhuǎn)換成XHTML   65                 OutputStream out = new FileOutputStream(new File(filepath + htmlName));   66                 XHTMLConverter.getInstance().convert(document, out, options);   67                  68                 //也可以使用字符數(shù)組流獲取解析的內(nèi)容 69 //                ByteArrayOutputStream baos = new ByteArrayOutputStream();  70 //                XHTMLConverter.getInstance().convert(document, baos, options);   71 //                String content = baos.toString(); 72 //                System.out.println(content); 73 //                 baos.close(); 74             } else {   75                 System.out.println("Enter only MS Office 2007+ files");   76             }   77         }   78     }   79      80     /** 81      * /** 82      * 2003版本word轉(zhuǎn)換成html 83      * @throws IOException 84      * @throws TransformerException 85      * @throws ParserConfigurationException 86      */ 87     @Test  88     public void Word2003ToHtml() throws IOException, TransformerException, ParserConfigurationException { 89         String filepath = "C:/test/"; 90         final String imagepath = "C:/test/image/"; 91         String fileName = "滕王閣序2003.doc"; 92         String htmlName = "滕王閣序2003.html"; 93         final String file = filepath + fileName; 94         InputStream input = new FileInputStream(new File(file)); 95         HWPFDocument wordDocument = new HWPFDocument(input); 96         WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); 97         //設(shè)置圖片存放的位置 98         wordToHtmlConverter.setPicturesManager(new PicturesManager() { 99             public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {100                 File imgPath = new File(imagepath);101                 if(!imgPath.exists()){//圖片目錄不存在則創(chuàng)建102                     imgPath.mkdirs();103                 }104                 File file = new File(imagepath + suggestedName);105                 try {106                     OutputStream os = new FileOutputStream(file);107                     os.write(content);108                     os.close();109                 } catch (FileNotFoundException e) {110                     e.printStackTrace();111                 } catch (IOException e) {112                     e.printStackTrace();113                 }114                 return imagepath + suggestedName;115             }116         });117         118         //解析word文檔119         wordToHtmlConverter.processDocument(wordDocument);120         Document htmlDocument = wordToHtmlConverter.getDocument();121         122         File htmlFile = new File(filepath + htmlName);123         OutputStream outStream = new FileOutputStream(htmlFile);124         125         //也可以使用字符數(shù)組流獲取解析的內(nèi)容126 //        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 127 //        OutputStream outStream = new BufferedOutputStream(baos);128 129         DOMSource domSource = new DOMSource(htmlDocument);130         StreamResult streamResult = new StreamResult(outStream);131 132         TransformerFactory factory = TransformerFactory.newInstance();133         Transformer serializer = factory.newTransformer();134         serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");135         serializer.setOutputProperty(OutputKeys.INDENT, "yes");136         serializer.setOutputProperty(OutputKeys.METHOD, "html");137         138         serializer.transform(domSource, streamResult);139 140         //也可以使用字符數(shù)組流獲取解析的內(nèi)容141 //        String content = baos.toString();142 //        System.out.println(content);143 //        baos.close();144         outStream.close();145     }146 }

  運(yùn)行生存文件結(jié)果如下:

  

   


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长子县| 板桥市| 应城市| 高密市| 诸城市| 彭泽县| 虞城县| 东光县| 大英县| 盐池县| 睢宁县| 黄石市| 五常市| 南阳市| 临高县| 保山市| 永川市| 美姑县| 乐都县| 娱乐| 西吉县| 华阴市| 武鸣县| 赣州市| 广德县| 肃南| 保康县| 白玉县| 镇宁| 宁津县| 乡城县| 邢台市| 连平县| 仁布县| 连南| 中卫市| 通榆县| 札达县| 南京市| 渭源县| 牙克石市|