文档展示:使用OpenOffice转换文档为HTML

上接 文档展示:使用OpenOffice转换文档为PDF
[url]http://zhuyufufu.iteye.com/admin/blogs/2009827[/url]

文档展示:PDFRender 将PDF转换为图片
[url]http://zhuyufufu.iteye.com/admin/blogs/2012236[/url]

将文档转换为HTML通过页面直接展示文档是一种非常好的思路,下面就来实现它

上代码


package com.zas.openoffice.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class Doc2HtmlUtil {
static String soffice_host = "127.0.0.1";
static String soffice_port = "8100";

/**
* 转换文件
* @param fromFileInputStream
* @throws FileNotFoundException
* */
public static String doc2Html(String inputFilePath, String outputFolder) throws FileNotFoundException {
File inputFile = new File(inputFilePath);
if (!inputFile.exists()) {
throw new FileNotFoundException("要转换的文件不存在:" + inputFilePath);
}
File toFileFolder = new File(outputFolder);
if (!toFileFolder.exists()) {
toFileFolder.mkdirs();
}

InputStream fromFileInputStream = new FileInputStream(inputFile);

// Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
// String timesuffix = sdf.format(date);

// String htmFileName = inputFile.getName().substring(0, inputFile.getName().lastIndexOf(".")) + ".html";
//
// File htmlOutputFile = new File(toFileFolder.toString() + File.separatorChar + htmFileName);
// File docInputFile = new File(toFileFolder.toString() + File.separatorChar + inputFile.getName());

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String htmFileName = "htmlfile" + timesuffix + ".html";
String docFileName = "docfile" + timesuffix + inputFilePath.substring(inputFilePath.lastIndexOf("."));

File htmlOutputFile = new File(toFileFolder.toString() + File.separatorChar + htmFileName);
File docInputFile = new File(toFileFolder.toString() + File.separatorChar + docFileName);

/**
* 由fromFileInputStream构建输入文件
* */
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();

} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fromFileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

OpenOfficeConnection connection = new SocketOpenOfficeConnection(soffice_host, Integer.parseInt(soffice_port));
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
e.printStackTrace();
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();

// 转换完之后删除word文件
// docInputFile.delete();
return htmFileName;
}

public static void main(String[] args) throws IOException {
//Doc2HtmlUtil.doc2Html("D://pdf//转换用.ppt", "D://pdf//ttttt444//");
Doc2HtmlUtil.doc2Html("D://pdf//专业与院系对应关系.xls", "D://pdf//ttttt444//");
//Doc2HtmlUtil.doc2Html("D://pdf//2010110档案管理系统需求分析说明书正式.doc", "D://pdf//ttttt444//");
}
}


转换的效果出乎我的预料,Excel和PPT的表现非常完美

doc文档效果差强人意

效率也基本能够达到我的预期

还有一个工作日,最后再研究下POI转文档为HTML
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值