美解决doc、docx格式word转换为Html
2015-09-27 17:02
656 查看
由于项目需要,需要在线预览文档,所以就想转换为htmL格式;
此项目为maven项目,引入的包可能需要一些时间;
maven项目转换为eclipse项目命令是:mvn eclipse:eclipse,然后导入到eclipse;
jar引入完成后,绝对100%能运行;
最后附上项目下载地址;
主要代码:
资源路径:http://download.csdn.net/detail/renzhehongyi/9142883
此项目为maven项目,引入的包可能需要一些时间;
maven项目转换为eclipse项目命令是:mvn eclipse:eclipse,然后导入到eclipse;
jar引入完成后,绝对100%能运行;
最后附上项目下载地址;
主要代码:
package fxma.Word2Html; 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.OutputStream; import java.io.OutputStreamWriter; import java.util.List; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; 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.model.PicturesTable; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.core.FileURIResolver; 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; /** * * @author Mafx * */ public class Word2Html { private final static String tempPath = "c:/test/"; public static void main(String argv[]) { try { doc2Html("c://bb.doc", "c://bb.html"); docx2Html("c://aa.docx", "c:aa.html"); } catch (Exception e) { e.printStackTrace(); } } /** * doc转换为html * * @param fileName * @param outPutFile * @throws TransformerException * @throws IOException * @throws ParserConfigurationException */ public static void doc2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { long startTime = System.currentTimeMillis(); HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); 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<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 { 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); System.out.println("Generate " + outPutFile + " with " + (System.currentTimeMillis() - startTime) + " ms."); } /** * 写文件 * * @param content * @param path */ 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) { } } } /** * docx格式word转换为html * * @param fileName * docx文件路径 * @param outPutFile * html输出文件路径 * @throws TransformerException * @throws IOException * @throws ParserConfigurationException */ public static void docx2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { String fileOutName = outPutFile; long startTime = System.currentTimeMillis(); XWPFDocument document = new XWPFDocument(new FileInputStream(fileName)); XHTMLOptions options = XHTMLOptions.create().indent(4); // 导出图片 File imageFolder = new File(tempPath); 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); System.out.println("Generate " + fileOutName + " with " + (System.currentTimeMillis() - startTime) + " ms."); } }
资源路径:http://download.csdn.net/detail/renzhehongyi/9142883
相关文章推荐
- HTML表格
- HTML链接
- 3.3.5 使用HtmlDiff对象
- 3.3.5 使用HtmlDiff对象
- Django模板:HTML静态模板的继承
- Ajax在html页面获取后台XML文件资源
- 编程,开始了,首先,c语言写一句,HTML写一句,hello world!
- HTML初步学习1
- HTML下拉列表的左右选择
- HTML复选框全选,全部选,反选案例
- html中的锚点
- 一个使用Html做的简单的window弹窗案例
- 【html】day05_表格_表单
- HTML meta viewport属性详解
- html网页制作小试
- HTML表单
- HTML中的实体是什么?
- HTML如何在图片上添加内容(如文字,菜单栏,按钮等)
- HTML中的框架是什么?
- HTML 5 Web 存储