文章目录
前言
在信息化日益发展的今天,文档处理与展示已经成为企业办公和个人学习工作中不可或缺的一部分。Word、Excel、PPT作为微软Office套件中的三大核心组件,广泛应用于各种场景。然而,传统的本地编辑方式在团队协作、异地办公等场景中显得力不从心。因此,实现Word、Excel、PPT的在线编辑预览功能,成为了提升工作效率、促进信息交流的迫切需求。
本文将详细介绍如何使用Java实现Word、Excel、PPT的在线编辑预览功能。首先,我们将分析在线编辑预览的需求和技术难点;接着,我们将探讨Java在处理文档方面的常用技术和框架;然后,我们将结合实际案例,逐步讲解如何实现Word、Excel、PPT的在线编辑预览功能;最后,我们将总结整个实现过程。
通过本文的学习,读者将能够掌握Java在处理Word、Excel、PPT等文档方面的基本技能,为构建高效的在线文档编辑预览系统打下坚实的基础。同时,也希望本文能够激发读者对Java文档处理技术的兴趣和热情,共同推动该领域的发展。
一、免费方案:
①采用dsoframer。
dsoframer是微软提供一款开源的用于在线编辑、调用Word、 Excel 、PowerPoint等的ActiveX控件。缺点:只支持IE浏览器,由于dsoframer是早些年发布的,一直都没有更新,可能ie10、ie11下运行会有问题;调用起来比较复杂,功能少;客户端插件安装不方便,比如会遇到浏览器阻拦不能成功安装,甚至不提示安装等问题。
②利用Office Online 实现文档在线预览
注:office online 平台需要在服务器上搭建,略有难度
利用office online 平台进行office 文档的在线查看,主旨在于获取文档的具体地址,通过Office 平台提供的链接地址指向需要预览的文档地址即可,例:https://view.officeapps.live.com/op/view.aspx?src=https://physics.nankai.edu.cn/_upload/article/files/b2/42/e1f28f964ffd8ec534034429bf79/d7a385f5-65a6-4b98-b98a-cfab9b166549.xls
这个链接分为了两部分,一部分是 http://view.officeapps.live.com/op/view.aspx?src=,后面那个是具体的文档地址,用URLEncode进行处理的链接地址
通过拼接的地址即可实现office 的在线预览
需要注意的是:office 在线预览限制
文档访问地址不能直接使用 ip,需要通过域名访问,并且端口必须是 80 端口
文档的格式(必须为以下格式之一):
Word:docx、docm、dotm、dotx
Excel:xlsx、xlsb、xls、xlsm
PowerPoint:pptx、ppsx、ppt、pps、pptm、potm、ppam、potx、ppsm
文档的大小:Word 和 PowerPoint 文档必须小于 10 兆字节;Excel 必须小于五兆字节(通过office web app 部署的本地服务器可以设置文档大小)
③将上传的docx和doc格式的Word文档进行解析,解析成html格式、或者使用前端编辑器tinymce直接预览内容(推荐)
两个方案:
1、我们可以在自己Word预览按钮上加个预览事件,这个Word文档肯定是上传到我们服务器上的,点击的时候,我们后台获取到这个Word的地址,然后使用java解析一下这个Word,然后把内容传递到前台,我们使用tinymce的setContent将Word解析的html内容导入到编辑器中,然后将编辑器设为只读(需要用到tinymce编辑器)。
2、我们把Word转换为html,将html生成到服务器上,在事件中返回html的路径,进行一个location或者其他处理,来进行Word的预览,毕竟是页面编辑器免费的,使用起来并不是那么好用 ,功能也不是很全,但是是可以满足用户日常修改,文字或图片格式之类的修改
以上两个方案的缺点就是不能百分比做到格式统一,但是可以做到百分之80以上的格式+图片的预览。
二、付费方案:
采用PageOffice
提供了服务器端Java编程对象控制客户端控件在线打开编辑保存Word、Excel、PPT;支持填充数据到Word模板,动态生成文件,这个功能对于起草文件是很有帮助的;调用代码简单;封装好的客户端安装程序;兼容所有浏览器。缺点:需要按功能付费,购买不同的版本需要支付不同的费用。
注:aspose-words技术中的jar是收费的
其实还有一种,只可预览,不可编辑,就是使用aspose-words技术,这个技术其实就是把office转换成PDF,然后在浏览器预览即可,做不到编辑效果,如果要做到编辑效果,必须用插件,浏览器技术实现不了,使用aspose-words其实就是几个jar包,使用很简单,可以把Word,Excel,PPT,TXT等转换PDF,然后在页面用PDF.js预览就行,使用过程中需要注意的就是中文乱码的情况,一般Windows本机测试和Windows服务器系统都是可以的,Linux的话会出现乱码(不分系统,centos,ubentu等),只需要更新一下Linux字体就行,苹果同上,一样操作,↓↓↓↓↓话不多说往下看↓↓↓↓↓。
三、推荐使用方案(免费):
使用aspose将Word、PPT、Excel直接转换成PDF,使用PDF.js进行预览
用aspose是最简单最方便,直接jar包,然后代码拿走直接用就行了。
在这里简单强调一下,预览Word这些文件,浏览器肯定是没有这个功能的,至少目前没有,只能通过第三方的一些服务来另辟通道,我在这里的思路是直接把Word、PPT、Excel这种文件,我直接转成PDF,然后前端使用PDF.js来进行预览,达到想要的预览效果,手机APP,以及PC端都支持PDF.js预览,目前测试过一些最新的国产手机操作系统都可以适配,大胆放心用,如果有问题私信我或在文章评论区留言,大家一起解决业务问题。
jar包,跟相关xml我都放到我主页的下载资源中了,放到我服务器上的话,未来某一天服务器不用了,我这个文章岂不作废了,大家可以去我的主页下载资源,就是使用aspose将office转换为PDF这个jar原本是收费的,所以我把这段列在付费方案中,我这里是破解之后的,需要在工程中引入一个xml文件,也可以不引入xml,具体使用方式都在我代码块儿中,以下代码是我自己的业务环境代码,合并到一起了,大家拿走之后可以自行修改。
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.is.flywings.frame.util.Sysout;
public class AsposeUtil {
/**
* 获取license
*
* @return
*/
public static boolean getLicense(int type) {
boolean result = false;
try {
//可以将 InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
//这段代码干掉,直接把license.xml内容复制到代码中搞成字符串,然后再转成stream流,这样系统就会少一个xml文件
//我这里给一个实例,引用我这个注释里的代码就不需要在工程中放这个授权文件了(license.xml)
//String lic = "<License>"
// +"<Data>"
// +"<Products>"
// +"<Product>Aspose.Total for Java</Product>"
// +"<Product>Aspose.Words for Java</Product>"
// +"</Products>"
// +"<EditionType>Enterprise</EditionType>"
// +"<SubscriptionExpiry>20991231</SubscriptionExpiry>"
// +"<LicenseExpiry>20991231</LicenseExpiry>"
// +"<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>"
// +"</Data>"
// +"<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU="
// +"</Signature>"
// +"</License>";
//InputStream is = new ByteArrayInputStream(lic.getBytes("utf-8"));
InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
if (type == 1) {//excel
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} else if (type == 2) {//word
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} else {//ppt
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void Excel2Pdf(String officePath,String OutPutPath,String officeName) {
// 验证License
if (!getLicense(1)) {
return;
}
try {
File file = new File(OutPutPath);
if (!file.exists()) {
file.mkdirs();
}
//long old = Sysout.currentTimeMillis();
Workbook wb = new Workbook(officePath);// 原始excel路径
File pdfFile = new File(OutPutPath+officeName);// 输出路径
FileOutputStream fileOS = new FileOutputStream(pdfFile);
//wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
wb.save(fileOS, pdfSaveOptions);
//long now = Sysout.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Word2Pdf(String officePath,String OutPutPath,String officeName) {
// 验证License
if (!getLicense(2)) {
return;
}
try {
File file = new File(OutPutPath);
if (!file.exists()) {
file.mkdirs();
}
Document doc = new Document(officePath);// 原始word路径
File pdfFile = new File(OutPutPath+officeName);// 输出路径
FileOutputStream fileOS = new FileOutputStream(pdfFile);
doc.save(fileOS, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void PPT2Pdf(String officePath,String OutPutPath,String officeName) {
// 验证License
if (!getLicense(3)) {
return;
}
try {
File PathFile = new File(OutPutPath);
if (!PathFile.exists()) {
PathFile.mkdirs();
}
InputStream slides = new FileInputStream(new File(officePath));// 原始ppt路径
Presentation pres = new Presentation(slides);
File file = new File(OutPutPath+officeName);// 输出pdf路径
FileOutputStream fileOS = new FileOutputStream(file);
pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String OfficeToPdf(String officePath) {
//G:/product/WebApp/fwis_develop/com/is/flywings/oa/attchfile/1000000000/i0002/101951.docx⌒101951.docx⌒feiyu.docx
//这里大家自己处理自己的附件路径即可
String[] split = officePath.split("⌒");
int lastIndex = split[0].lastIndexOf(".");
int lastNameIndex = split[0].lastIndexOf("/");
String officeType = split[0].substring(lastIndex+1);
String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf";
String OutPutPath = split[0].substring(0,lastNameIndex+1)+"office/";
File file = new File(split[0]);
File pdfFile = new File(OutPutPath+officeName);
//判断当前office文件是否已经转为PDF,如果已转为PDF就不需要再次转换。
if(pdfFile.exists()){
return OutPutPath+officeName;
}
if (file.exists()) {
double bytes = file.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
String MB = df.format(megabytes);
Double Size = Double.parseDouble(MB);
if(Size>10){
return Size+"MB";
}
//"doc", "docx", "xls","xlsx", "ppt", "pptx"
try {
if(officeType.equals("doc")||officeType.equals("docx")){
Word2Pdf(split[0],OutPutPath,officeName);
}else if(officeType.equals("xls")||officeType.equals("xlsx")){
Excel2Pdf(split[0],OutPutPath,officeName);
}else if(officeType.equals("ppt")||officeType.equals("pptx")){
PPT2Pdf(split[0],OutPutPath,officeName);
}else{
System.out.println("无法识别该文件!");
return "Error";
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
return "NotExists";
}
return OutPutPath+officeName;
}
}
license.xml
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
</License>
四、总结
使用aspose的优点就是转换快,可以跨平台,在Linux服务器下需要注意的就是中英文乱码,极少个别的Windows服务器也有乱码情况,乱码的根本是在于服务器没有你这个文章中引用的字体,所以转换会乱码,如果出现乱码,大家直接百度搜索aspose转换中文乱码即可,就是把Windows上的字体导入到系统中,教程简单无脑,直接替换字体就行,无伤大雅。