您的位置:首页 > Web前端 > HTML

word转换html

2016-06-06 11:05 489 查看
这是我在工作中碰到的一个需求,一开始我做的比较死,直接返回排版好的页面,后来客户需要修改这个页面的一些东西,问题来了,我不能改变一次就对页面进行一次修改吧。

后来琢磨着把这个功能做成活的,可以让用户自己上传一个word,在页面查看的时候,自动生成html,这样会更人性化一点,于是乎有了下面的代码。

前提是需要引入jar包,poi 3.8及以上的版本。

public class Word2HtmlUtil {

private static String deamonStr = "";

private static String realPathStr = "";

public static void main(String argv[]) {
try {
convert2Html("C:\\Users\\admin\\Documents\\Tencent Files\\917997617\\FileRecv\\学院职业技能鉴定工作流程(修改).doc",
"C:\\Users\\admin\\Documents\\Tencent Files\\917997617\\FileRecv\\学院职业技能鉴定工作流程(修改).html", "", null, false);
}
catch (Exception e) {
e.printStackTrace();
}
}

/**
* 把内容写到html中
* @param content
* @param path
* @param replace
*/
public static void writeFile(String content, String path, boolean replace) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File(path);
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
if (replace) {// 如果需要取消标题设为true
try {
String beginStr = "<span";
String endStr = "</span>";
int startIndex = content.indexOf(beginStr, 0) + beginStr.length();
int startIndex_1 = content.indexOf(">", startIndex);
if (startIndex_1 > startIndex) {
beginStr = "<span class=\"s1\"";
}
int endIndex = content.indexOf(endStr, startIndex_1);
String title = content.substring(startIndex_1, endIndex);
content = content.replace(beginStr + title + endStr, "");
}
catch (Exception e) {
}
}
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) {
}
}
}

/**
* word转换为html,目前只支持2003
* @param fileName:读取的word路径
* @param outPutFile:输入路径
* @param deamon:域名
* @param realPath:项目路径
* @param replace:是否去除标题
*/
public static void convert2Html(String fileName, String outPutFile, String deamon, String realPath, boolean replace) {
deamonStr = deamon;
realPathStr = realPath;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
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) {
String imgPath = realPathStr + "picTmp" + File.separator + suggestedName;// 图片保存路径
File file = new File(imgPath);
try {
if (!file.exists()) {
if (!file.getParentFile().exists()) {
if (file.getParentFile().mkdirs()) {
file.createNewFile();
}
}
else if (!file.exists()) {
file.createNewFile();
}
}
OutputStream os = new FileOutputStream(file);
os.write(content);
os.close();
}
catch (Exception e) {
e.printStackTrace();
}
return "http://" + deamonStr + "/picTmp/" + suggestedName;// 图片在页面显示的路径
}
});
wordToHtmlConverter.processDocument(wordDocument);
// save pictures
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {

for (int i = 0; i < pics.size(); i++) {
Picture pic = pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(realPathStr + "picTmp" + File.separator + pic.suggestFullFileName()));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
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, replace);
}
catch (Exception e) {
e.printStackTrace();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  word转换html