POI word文件转html
package com.feiruo.officeConvert;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import org.apache.poi.hwpf.usermodel.Picture; public abstract class OfficeConvert { // 图片的存放地址
private String imgPath = null;
// 文件存放的地址
private String parentPath = null;
// 文件内容
private String fileContent = null;
private String encode = "UTF-8"; /**
* 将指定的doc文档进行格式转换
*
* @param docPath
* *.doc文档地址
*
* @throws FileNotFoundException
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public abstract void convert(String docPath) throws FileNotFoundException,
IOException, ParserConfigurationException, TransformerException; /**
* 将文件内容写入到磁盘
*
* @param filepath
* 保存转换文件的地址
*/
public void writeFile(String filepath) {
FileOutputStream fos = null;
BufferedWriter bw = null;
File f=new File(this.parentPath); if(!f.exists()){
f.mkdirs();
}
try {
File file = new File(filepath);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, encode));
bw.write(fileContent);
} 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) {
}
}
}
public String checkSetPath(String path){
path=path.trim();
if(path.lastIndexOf("/")<path.length()-1) path+="/";
if(path.indexOf("\"")>0)path=path.replaceAll("\"", "");
if(path.indexOf(">")>0)path=path.replaceAll(">", "&gt;");
if(path.indexOf("<")>0)path=path.replaceAll("<", "&lt;");
//TODO if(path.indexOf("*")>0)path=path.replaceAll("/*", "");
return path;
}
public String getEncode() {
return encode;
} public void setEncode(String encode) {
this.encode = encode;
} /**
* 获取图片存放地址
*
* @return <strong>java.lang.String</strong>
*/
public String getImgPath() {
return imgPath;
} /**
* 设置图片的存放地址文件夹路径
*
* @param imgPath
* 设置图片的存放文件夹名称
*/
public void setImgPath(String imgPath) {
this.imgPath = checkSetPath(imgPath);
} /**
* 获取存放文件的目录地址
*
* @return <strong>java.lang.String</strong>
*/
public String getParentPath() {
return parentPath;
} /**
* 设置文件存放的路径
*
* @param parentPath
* 文件地址
*/
public void setParentPath(String parentPath) {
this.parentPath = checkSetPath(parentPath);
} /**
* 获取文件内容
*
* @return <strong>java.lang.String</strong>
*/
public String getFileContent() {
return fileContent;
}
public void setFileContent(String content){
this.fileContent=content;
}
}
package com.feiruo.officeConvert;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.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; /**
* 将*.doc文档转换为*.html文件格式
*
* @author Jdk.feiruo.
* @since JDK 1.7 POI 3.8
* @version 1.0
*/
public class DocToHtml extends OfficeConvert implements IOfficeConvert {
private List<Picture> pics = null; /**
* @param parentPath
* html文件存放地址
* @param imageppth
* html图片存放地址
* @param encoding
* 设置html的编码格式
*/
public DocToHtml(String parentPath, String imageppth, String encoding) {
setParentPath(checkSetPath(parentPath));
setImgPath(checkSetPath(imageppth));
this.setEncode(encoding);
} public DocToHtml() { } /**
* 将*doc文档转为*html文件
*
* @param docPath
* *doc文档的所在地址
*
* @throws FileNotFoundException
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public void convert(String docPath) throws FileNotFoundException,
IOException, ParserConfigurationException, TransformerException {
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(
docPath));
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);
pics = wordDocument.getPicturesTable().getAllPictures(); 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, this.getEncode());
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult); out.close(); String htmlContent = new String(out.toByteArray());
if(htmlContent.indexOf("<img src=\"") > 0){
htmlContent=htmlContent.replaceAll("<img src=\"", "<img src=\"" + getImgPath());
}
setFileContent(htmlContent);
} @Override
public void writeWithName(String fileName) {
// 先保存文档中的图片
if (pics != null) {
File imgfile = new File(this.getParentPath() + this.getImgPath());
// 如果当前文件夹不存在,则创建新文件夹
if (!imgfile.exists())
imgfile.mkdirs();
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(imgfile + "//"
+ pic.suggestFullFileName()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 保存html源码文件
this.writeFile(getParentPath()+fileName+".html");
}
}
package com.feiruo.Test;

import java.io.FileNotFoundException;
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import com.yinhai.officeConvert.DocToHtml; public class Test{
public static void main(String[] args) {
Test t=new Test();
}
public Test(){
DocToHtml dth=new DocToHtml("C://test", "f", "UTF-8");
try {
dth.convert("D://test//test.doc");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
dth.writeWithName("feiruo");
}
}
package com.feiruo.officeConvert;

public interface IOfficeConvert {
/**
* 将文件写入到磁盘
* @param fileName 要写入文件的名称
*/
public void writeWithName(String fileName);
}

POI实现word文档转html文件的更多相关文章

  1. POI生成WORD文档

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  2. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  3. 用java语言通过POI实现word文档的按标题提取

    最近有一个项目需要将一个word文档中的数据提取到数据库中.就去网上查了好多资料,最靠谱的就是用poi实现word文档的提取. 喝水不忘挖井人,我查了好多资料就这个最靠谱,我的这篇博客主要是借鉴htt ...

  4. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  5. Poi之Word文档结构介绍

    1.poi之word文档结构介绍之正文段落 一个文档包含多个段落,一个段落包含多个Runs,一个Runs包含多个Run,Run是文档的最小单元 获取所有段落:List<XWPFParagraph ...

  6. 微信公众号怎么添加附件?比如word文档,pdf文件等

    微信公众号怎么添加附件?比如word文档,pdf文件等   我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众 ...

  7. Java POI 解析word文档

    实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...

  8. java word文档 转 html文件

    一.简介 一般word文件后缀有doc.docx两种.docx是office word 2007以及以后版本文档的扩展名:doc是office word 2003文档保存的扩展名.对于这两种格式的wo ...

  9. poi 读取word文档

    1.导入jar包 官网下载地址: https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.zip 最开始 ...

随机推荐

  1. asp.net mvc 页面传值的方法总结

    转自:http://msprogrammer.serviciipeweb.ro/2012/01/15/usual-methods-to-transfer-data-from-page-to-page- ...

  2. C#调用MySql

    1.要连接MySql数据库必须首先下载MySql官方的连接.net的文件,文件下载地址为http://dev.mysql.com/downloads/connector/net/6.6.html#do ...

  3. eclipse导入html、js、xml报错的问题

    今天重新安装eclipse,在导入部分html.js.xml文件,报错,解决办法如下: eclipse->window->preferences->Team,点击validation ...

  4. ADF_ADF Faces系列1_使用JSF开发基于Ajax的用户界面:ADF Faces 富客户端组件简介(Part1)

    2013-05-01 Created By BaoXinjian

  5. JAVA NIO中selectedKeys返回的键集,对其中的SelectionKey执行操作之后,是否需要在selectedKeys()中对其执行remove 操作

    今天一个东西需要用到java nio的东西.在网上查了一下资料,发现有Apache的Mina,Netty等,感觉JDK中带的NIO有点鸡肋啊.之前看过这部分的内容,但好长一段时间没有用,也就忘得七七八 ...

  6. JavaScript面向对象编程指南

    引言 面向对象程序设计 基本数据类型.数组.循环及条件表达式 基本数据类型 函数 函数Function 预定义函数 变量的作用域 函数也是数据 闭包 对象 原型 原型 继承 原型链 浅拷贝与深拷贝 原 ...

  7. Maven如何手动添加依赖的jar文件到本地Maven仓库

    大家肯定遇到过想在pom文件中加入自己开发的依赖包,这些包肯定是不是在Maven仓库(http://repo1.maven.org/maven2/)的.那我们怎么将那些不存在Maven仓库中的包加入到 ...

  8. linux 查看占用端口并kill掉

    主要可以使用ps  命令 或 netstat 命令 weihong@data1:~/jd_parser/jd_parser$ ps aux | head USER PID %CPU %MEM VSZ ...

  9. arm-linux-gcc 常用参数讲解 gcc编译器使用方法

    我们需要编译出运行在ARM平台上的代码,所使用的交叉编译器为 arm-linux-gcc.下面将arm-linux-gcc编译工具的一些常用命令参数介绍给大家.在此之前首先介绍下编译器的工作过程,在使 ...

  10. java基础-final