在工作生活中,有很多时候需要将文件转换成相应的文件格式进行文件上传,然而这样在本地进行转换然后进行上传,无疑增加了工作时间,影响了用户的体验,不需要用户进行转换,这样提高了用户的体验,闲话不多说,上代码
代码如下:
转换文件的包放在代码最后
import java.io.EOFException;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import .URL;import org.apache.log4j.Logger;import com.aspose.cells.PdfSaveOptions;import com.aspose.cells.Workbook;import com.aspose.slides.Presentation;import com.aspose.words.Document;public class FileToPdf {private static Logger logger = Logger.getLogger(FileToPdf.class);/*** * @Title: getExcelLicense* @Description: 校验excel license 去除水印** @return** @author lxs* @date -09-04 03:38:29 **/private static boolean getExcelLicense() {boolean result = false;try {InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("/license.xml");com.aspose.cells.License aposeLic = new com.aspose.cells.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** * @Title: getPptLicense* @Description: 校验ppt license 去除水印** @return** @author lxs* @date -09-04 03:38:14 **///校验licenseprivate static boolean getPptLicense() {boolean result = false;try {InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("/license.xml");com.aspose.slides.License aposeLic = new com.aspose.slides.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** * @Title: getWordLicense* @Description: 校验word license 去除水印** @return** @author lxs* @date -09-04 03:40:04 **/private static boolean getWordLicense() {boolean result = false;try {InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("/license.xml");com.aspose.words.License aposeLic = new com.aspose.words.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*----------excel转成pdf --------------------------------start*//*** excel 转为pdf 输出。** @param sourceFilePath excel文件* @param desFilePathd pad 输出文件目录*/public static void excel2pdf(String sourceFilePath, String desFilePathd ){if (!getExcelLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {Workbook wb = new Workbook(sourceFilePath);// 原始excel路径FileOutputStream fileOS = new FileOutputStream(desFilePathd);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(fileOS, pdfSaveOptions);fileOS.flush();fileOS.close();} catch (Exception e) {e.printStackTrace();}}/*** 设置打印的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);}}}/*--------excel转成pdf --------------------------------end*//*--------word转成pdf --------------------------------start*/public static void wordToPdf(String sourceFilePath, String desFilePathd ){if (!getWordLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生return;}FileOutputStream os = null;Document doc;try {File file = new File(desFilePathd);os = new FileOutputStream(file);doc = new Document(sourceFilePath);doc.save(os, com.aspose.words.SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();} finally {try {os.close();} catch (IOException e) {e.printStackTrace();}}}/*--------word转成pdf --------------------------------end*//*--------ppt转成pdf --------------------------------start*//**** @param args*/public static void pptToPdf(String sourceFilePath, String desFilePathd) {// 验证Licenseif (!getPptLicense()) {return;}try {// long old = System.currentTimeMillis();File file = new File(desFilePathd);// 输出pdf路径Presentation pres = new Presentation(sourceFilePath);//输入pdf路径FileOutputStream fileOS = new FileOutputStream(file);pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);fileOS.close();} catch (Exception e) {e.printStackTrace();}}/*--------ppt转成pdf --------------------------------end*/}
链接: /s/1v7196XiN71lpa2Ij5Bm-_g .
提取码:QSAD。
总结
之前找了好几个转换的,不是付费就是只能在win上面转换。不喜勿喷,第一次写博客,请大佬们多指教
如果觉得《Wps文档文件转换成pdf 支持doc-docx-ppt-pptx-xls》对你有帮助,请点赞、收藏,并留下你的观点哦!