Java在线预览(word转html)--强势推荐

Java--Word在线预览(word转html)--强势推荐

一、新建一个maven项目

Java在线预览(word转html)--强势推荐_第1张图片

二、pom文件


  4.0.0
  com.maven
  online-preview
  war
  0.0.1-SNAPSHOT
  online-preview Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      4.12
      test
    
     
     org.apache.poi 
     poi 
     3.14 
     
     
     org.apache.poi 
     poi-scratchpad 
     3.14 
     
     
     org.apache.poi 
     poi-ooxml 
     3.14 
     
     
     fr.opensagres.xdocreport 
     xdocreport 
     1.0.6 
     
     
     org.apache.poi 
     poi-ooxml-schemas 
     3.14 
     
     
     org.apache.poi 
     ooxml-schemas 
     1.3 
     
    

    org.apache.directory.studio
    org.apache.commons.io
    2.4

    
  
  
    online-preview
  

三、Word2007ToHtml.java区分word2007和word2003

/**
 * 
 */
package com.maven.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
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.FileUtils;
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.w3c.dom.Document;

/**
* 

Title:Word2007ToHtml

*

Company:

* @author * @date 2018年4月11日下午2:21:30 * Description: */ public class Word2007ToHtml { /*@Test public void word2007ToHtml() throws Exception { String filepath = "D:\\Tomcat8.0\\File\\"; String sourceFileName =filepath+"1.docx"; String targetFileName = filepath+"1496717486420.html"; String imagePathStr = filepath+"/image/"; OutputStreamWriter outputStreamWriter = null; try { XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName)); XHTMLOptions options = XHTMLOptions.create(); // 存放图片的文件夹 options.setExtractor(new FileImageExtractor(new File(imagePathStr))); // html中图片的路径 options.URIResolver(new BasicURIResolver("image")); outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName), "utf-8"); XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance(); xhtmlConverter.convert(document, outputStreamWriter, options); } finally { if (outputStreamWriter != null) { outputStreamWriter.close(); } } } */ public static void main(String[] args) throws Throwable { final String path = "C:\\Users\\Administrator\\Desktop\\My\\船员培训\\doxc\\题库\\船员用表\\"; final String file = "1.doc"; InputStream input = new FileInputStream(path + file); HWPFDocument wordDocument = new HWPFDocument(input); 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); List pics = wordDocument.getPicturesTable().getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get(i); try { pic.writeImageContent(new FileOutputStream(path + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(outStream); 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); outStream.close(); String content = new String(outStream.toByteArray()); FileUtils.writeStringToFile(new File(path, "1.html"), content, "utf-8"); } }

注意:pyth:本地地址。转换完之后,又在pyth的地址中保存的html文件

Java在线预览(word转html)--强势推荐_第2张图片

不懂在下面留言即可

欢迎订阅关注公众号(JAVA和人工智能)

                                       

                                                           获取更多免费书籍、资源、视频资料


                                    Java在线预览(word转html)--强势推荐_第3张图片



你可能感兴趣的:(JAVA)