相关文章推荐
稳重的匕首  ·  QT、Python、QTreeView、自定 ...·  1 年前    · 
安静的铅笔  ·  利用pyinstaller打包streaml ...·  1 年前    · 
痴情的数据线  ·  Kibana:如何在 Search Bar ...·  2 年前    · 
光明磊落的眼镜  ·  你不知道的vscode之扩展 | 代码定位 ...·  2 年前    · 
狂野的投影仪  ·  python第三方库的更新和安装指定版本_w ...·  2 年前    · 
Code  ›  Java实现在线预览–openOffice实现[通俗易懂]开发者社区
https://cloud.tencent.com/developer/article/2031042
慈祥的核桃
1 年前
全栈程序员站长

Java实现在线预览–openOffice实现[通俗易懂]

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
全栈程序员站长
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > Java实现在线预览–openOffice实现[通俗易懂]

Java实现在线预览–openOffice实现[通俗易懂]

作者头像
全栈程序员站长
发布 于 2022-06-27 10:31:20
2.5K 1
发布 于 2022-06-27 10:31:20
举报
文章被收录于专栏: 全栈程序员必看 全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

#Java实现在线预览–openOffice实现 之前有写了poi实现在线预览的文章,里面也说到了使用openOffice也可以做到,这里就详细介绍一下。 我的实现逻辑有两种: 一、利用jodconverter(基于OpenOffice服务)将文件( .doc、 .docx、 .xls、 .ppt)转化为html格式。 二、利用jodconverter(基于OpenOffice服务)将文件( .doc、 .docx、 .xls、 .ppt)转化为pdf格式。 转换成html格式大家都能理解,这样就可以直接在浏览器上查看了,也就实现了在线预览的功能;转换成pdf格式这点,需要用户安装了Adobe Reader XI,这样你会发现把pdf直接拖到浏览器页面可以直接打开预览,这样也就实现了在线预览的功能。 ##将文件转化为html格式或者pdf格式 话不多说,直接上代码。

代码语言: javascript
复制
package com.pdfPreview.util;
import java.io.File;
import java.io.FileInputStream;
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;
 * 利用jodconverter(基于OpenOffice服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式,
 * 使用前请检查OpenOffice服务是否已经开启, OpenOffice进程名称:soffice.exe | soffice.bin
 * @author yjclsx
public class Doc2HtmlUtil {
	private static Doc2HtmlUtil doc2HtmlUtil;
	 * 获取Doc2HtmlUtil实例
	public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
		if (doc2HtmlUtil == null) {
			doc2HtmlUtil = new Doc2HtmlUtil();
		return doc2HtmlUtil;
	 * 转换文件成html
	 * @param fromFileInputStream:
	 * @throws IOException 
	public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String timesuffix = sdf.format(date);
		String docFileName = null;
		String htmFileName = null;
		if("doc".equals(type)){
			docFileName = "doc_" + timesuffix + ".doc";
			htmFileName = "doc_" + timesuffix + ".html";
		}else if("docx".equals(type)){
			docFileName = "docx_" + timesuffix + ".docx";
			htmFileName = "docx_" + timesuffix + ".html";
		}else if("xls".equals(type)){
			docFileName = "xls_" + timesuffix + ".xls";
			htmFileName = "xls_" + timesuffix + ".html";
		}else if("ppt".equals(type)){
			docFileName = "ppt_" + timesuffix + ".ppt";
			htmFileName = "ppt_" + timesuffix + ".html";
		}else{
			return null;
		File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
		File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
		if (htmlOutputFile.exists())
			htmlOutputFile.delete();
		htmlOutputFile.createNewFile();
		if (docInputFile.exists())
			docInputFile.delete();
		docInputFile.createNewFile();
		 * 由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();
			fromFileInputStream.close();
		} catch (IOException e) {
	    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try {
			connection.connect();
		} catch (ConnectException e) {
			System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
		// convert
		DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
		converter.convert(docInputFile, htmlOutputFile);
		connection.disconnect();
		// 转换完之后删除word文件
		docInputFile.delete();
		return htmFileName;
	 * 转换文件成pdf
	 * @param fromFileInputStream:
	 * @throws IOException 
	public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String timesuffix = sdf.format(date);
		String docFileName = null;
		String htmFileName = null;
		if("doc".equals(type)){
			docFileName = "doc_" + timesuffix + ".doc";
			htmFileName = "doc_" + timesuffix + ".pdf";
		}else if("docx".equals(type)){
			docFileName = "docx_" + timesuffix + ".docx";
			htmFileName = "docx_" + timesuffix + ".pdf";
		}else if("xls".equals(type)){
			docFileName = "xls_" + timesuffix + ".xls";
			htmFileName = "xls_" + timesuffix + ".pdf";
		}else if("ppt".equals(type)){
			docFileName = "ppt_" + timesuffix + ".ppt";
			htmFileName = "ppt_" + timesuffix + ".pdf";
		}else{
			return null;
		File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
		File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
		if (htmlOutputFile.exists())
			htmlOutputFile.delete();
		htmlOutputFile.createNewFile();
		if (docInputFile.exists())
			docInputFile.delete();
		docInputFile.createNewFile();
		 * 由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();
			fromFileInputStream.close();
		} catch (IOException e) {
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
		try {
			connection.connect();
		} catch (ConnectException e) {
			System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
		// 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 coc2HtmlUtil = getDoc2HtmlUtilInstance();
		File file = null;
		FileInputStream fileInputStream = null;
		file = new File("D:/poi-test/exportExcel.xls");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");
		file = new File("D:/poi-test/test.doc");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");
		file = new File("D:/poi-test/周报模版.ppt");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
		file = new File("D:/poi-test/test.docx");
		fileInputStream = new FileInputStream(file);
//		coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
		coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");
 
推荐文章
稳重的匕首  ·  QT、Python、QTreeView、自定义小部件、setData -拖放后丢失引用-腾讯云开发者社区-腾讯云
1 年前
安静的铅笔  ·  利用pyinstaller打包streamlit移植到其他电脑上使用-CSDN博客
1 年前
痴情的数据线  ·  Kibana:如何在 Search Bar 中实现模糊查询及通配符查询_kibana字段模糊匹配-CSDN博客
2 年前
光明磊落的眼镜  ·  你不知道的vscode之扩展 | 代码定位 - 掘金
2 年前
狂野的投影仪  ·  python第三方库的更新和安装指定版本_weixin_30907523的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号