2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 用JAVA如何实现word文档编辑预览的功能?

用JAVA如何实现word文档编辑预览的功能?

时间:2019-07-18 16:03:22

相关推荐

用JAVA如何实现word文档编辑预览的功能?

免费方案:

①采用dsoframer。

dsoframer是微软提供一款开源的用于在线编辑、调用Word、 Excel 、PowerPoint等的ActiveX控件。缺点:只支持IE浏览器,由于dsoframer是早些年发布的,一直都没有更新,可能ie10、ie11下运行会有问题;调用起来比较复杂,功能少;客户端插件安装不方便,比如会遇到浏览器阻拦不能成功安装,甚至不提示安装等问题。

②利用Office Online 实现文档在线预览

利用office online 平台进行office 文档的在线查看,主旨在于获取文档的具体地址,通过Office 平台提供的链接地址指向需要预览的文档地址即可,例:https://view./op/view.aspx?src=https://physics./_upload/article/files/b2/42/e1f28f964ffd8ec534034429bf79/d7a385f5-65a6-4b98-b98a-cfab9b166549.xls

这个链接分为了两部分,一部分是 http://view./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技术,这个技术其实就是把office转换成PDF,然后在浏览器预览即可,做不到编辑效果,如果要做到编辑效果,必须用插件,浏览器技术实现不了,使用aspose-words其实就是几个jar包,原本是收费的,但是现在网上有很多破解包以及教程,大家自行搜索就行,使用很简单,可以Word,Excel,PPT,TXT等转换PDF,然后在页面用Pdf.js预览就行,使用过程中需要注意的就是中文乱码的情况,一般win本机测试和win服务器系统都是可以的,Linux的话会出现乱码(不分系统,centos,ubentu等),只需要更新一下Linux字体就行,苹果同上,一样操作,大家可以试一试,妥妥的。

预览Word、PPT、Excel(直接转换成PDF,使用PDF.js进行预览)

用aspose是最简单最方便,直接jar包,然后代码抄走就行了。

jar包,跟相关xml我都放到我主页的下载资源中了,放到我服务器上的话,未来某一天服务器不用了,我这个文章岂不作废了,大家可以去我的主页下载资源,就是叫 使用aspose将office转换为PDF。

以下代码是我自己实际业务环境,大家可以自行修改,如有问题大家评论区讨论。

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");if (type == 1) {//excelcom.aspose.cells.License aposeLic = new com.aspose.cells.License();aposeLic.setLicense(is);result = true;} else if (type == 2) {//wordcom.aspose.words.License aposeLic = new com.aspose.words.License();aposeLic.setLicense(is);result = true;} else {//pptcom.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) {// 验证Licenseif (!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();// Sysout.println("共耗时:" + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();}}public static void Word2Pdf(String officePath,String OutPutPath,String officeName) {// 验证Licenseif (!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) {// 验证Licenseif (!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.docxString[] 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{Sysout.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的优点就是转换快,可以跨平台,需要注意的就是中英文乱码,如果出现乱码,大家直接百度搜索aspose转换中文乱码即可,教程简单无脑,直接替换字体就行,无伤大雅。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。