关于Java项目中,word和Excel类型文件的预览功能实现
- 背景
- Aspose说明
- Aspose.Words
- Aspose.Cells
- pom依赖引入
- 引入license.xml(授权文件)
- 创建工具类
- 创建预览接口
背景
在公司之前的一个项目中,在领导的强烈要求下(要啥自行车。。。),要实现关于文件的在线预览功能。人在屋檐下,没办法,后来查了好多资料,项目集成了OpenOffice,才得以实现,不过当时时间紧,所以做的比较粗糙。这次又新开发了别的项目,领导再次要求,用了一天时间,好好查了一些资料,发现aspose(真香,嘿嘿)。正好也记录一下,如有错误之处,请多多包含。
Aspose说明
Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,我们可以有计划地操纵一些商业中最流行的文件格式:Word, Excel, PowerPoint, Project,等office文档以及PDF文档。 除了强大的文件操纵组件之外,Aspose.Total 还提供了用于制图、写电子邮件、拼写检查、创建条形码、生成ad hoc 查询、重现格式以及工作流等组件,运用它我们可以整理一个完整的文档管理方案(这段是网上抄的,不好意思;具体信息,大家可以去官网查询);这里仅对Aspose.Words和Aspose.Cells做使用说明,如有其他需要,请参考官网资料,谢谢。
官网:https://www.aspose.com/
Aspose.Words
Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务。Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。使用Aspose.Words,您可以生成,更改,转换,渲染和打印文档而不使用Microsoft Word。
jar包下载地址:https://products.aspose.com/words/java/
Aspose.Cells
Aspose.Cells是一个广受赞誉的电子表格组件,支持所有Excel格式类型的操作,用户无需依靠Microsoft Excel也可为其应用程序嵌入读写和处理Excel数据表格的功能。Aspose.Cells可以导入和导出每一个具体的数据,表格和格式,在各个层面导入图像,应用复杂的计算公式,并将Excel的数据保存为各种格式等等—完成所有的这一切功能都无需使用Microsoft Excel 和Microsoft Office Automation。
jar包下载地址:https://products.aspose.com/words/java/
pom依赖引入
项目结构resources下新建lib文件夹,将下载好的jar包复制过来
引入license.xml(授权文件)
新建license.xml文件,放在resources根目录下
创建工具类
/**
* word转PDF 获取PDF文件流
* 全面支持DOC, DOCX相互转换
* @param
* @param docInputStream word源文件输入流
* @return
* @Author chencg
* @Date 17:57 2020/8/15
**/
public static ByteArrayOutputStream doc2pdfOutStream(InputStream docInputStream) {
try {
//pdfBaos是转化后的字节流
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
Document doc = new Document(docInputStream);
doc.save(pdfBaos, com.aspose.words.SaveFormat.PDF);
return pdfBaos;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* excel转PDF 获取PDF文件流
*
* @param
* @param docInputStream excel源文件输入流
* @return
* @Author chencg
* @Date 17:57 2020/8/15
**/
public static ByteArrayOutputStream excel2pdfOutStream(InputStream docInputStream) {
try {
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
Workbook wb = new Workbook(docInputStream);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int[] autoDrawSheets={3};
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
autoDraw(wb,autoDrawSheets);
int[] showSheets={0};
//隐藏workbook中不需要的sheet页。
printSheetPage(wb,showSheets);
wb.save(pdfBaos, pdfSaveOptions);
return pdfBaos;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 设置打印的sheet 自动拉伸比例
* @param wb
* @param page 自动拉伸的页的sheet数组
*/
public static void autoDraw(Workbook wb,int[] page){
if(null!=page&&page.length>0){
for (int i = 0; i < page.length; i++) {
wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
}
}
}
/**
* 隐藏workbook中不需要的sheet页。
* @param wb
* @param page 显示页的sheet数组
*/
public static void printSheetPage(Workbook wb,int[] page){
for (int i= 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if(null==page||page.length==0){
wb.getWorksheets().get(0).setVisible(true);
}else{
for (int i = 0; i < page.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
创建预览接口
@RequestMapping("/filePreview")
public void filePreview(HttpServletRequest request, HttpServletResponse response) {
String affixAddr = request.getParameter("affixAddr");
String affixName = request.getParameter("affixName");
try {
// 将文件从服务器上下载下来,获取到字节流;
byte[] downloadBytes = client.download(affixAddr.replaceAll(FastProperties.getFileServer() + "/", ""));
// 转为InputStream
InputStream pdfIn = new ByteArrayInputStream(downloadBytes);
//判断是否有授权文件 如果没有则会认为是试用版,转换的文件会有水印
//判断文件类型,引用不同的jar包
//注意:因为word和excel的依赖不同,一定要判断后认证授权文件;否则认证会失败,导致预览文件出现水印
InputStream is = AsposeWordsUtils.class.getResourceAsStream("/license.xml");
if (affixName.endsWith(".doc") || affixName.endsWith(".docx")) {
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
} else if (affixName.endsWith(".xls") || affixName.endsWith(".xlsx")) {
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
} else {
//自定义异常,仅支持WORD和EXCEL文件预览
throw new MisException(9999, "目前系统仅支持WORD和EXCEL文件预览");
}
ByteArrayOutputStream pdfOutStream = null;
//根据文件类型,调用不同的方法,转成pdf输出流
if (affixName.endsWith(".doc") || affixName.endsWith(".docx")) {
pdfOutStream = AsposeWordsUtils.doc2pdfOutStream(pdfIn);
} else if (affixName.endsWith(".xls") || affixName.endsWith(".xlsx")) {
pdfOutStream = AsposeWordsUtils.excel2pdfOutStream(pdfIn);
}
InputStream pdfInputStream = new ByteArrayInputStream(pdfOutStream.toByteArray());
response.setContentType("application/pdf");
ServletOutputStream outputStream = response.getOutputStream();
//输出给前端
int i = IOUtils.copy(pdfInputStream, outputStream);
//关流
pdfIn.close();
is.close();
pdfOutStream.close();
pdfInputStream.close();
outputStream.close();
} catch (Exception e) {
log.error("附件预览", e);
// 如果公司环境使用加密软件,可能会导致上传的文件也处于加密状态,导致转换异常
throw new MisException(9999, "请核查文件是否为加密状态");
}
}
Aspose是收费的,从官网上下载下来的是试用版,如果想长期使用是要付费的,请各位注意。