文章目錄
- aspose-*
- 一、依賴--maven
- 二、需求
- 1、word------>pdf
- 2、doc------>docx
- 2、xls------>xlsx
aspose-*
一、依賴–maven
備注:第三方的jar包可以從資源中下載,有上傳的
<!--aspose依賴--><dependency><groupId>aspose-words</groupId><artifactId>aspose-words</artifactId><version>1.0</version><scope>system</scope><systemPath>${basedir}/lib/aspose-words-20.12-jdk17-crack.jar</systemPath></dependency><dependency><groupId>aspose-cells</groupId><artifactId>aspose-cells</artifactId><version>1.0</version><scope>system</scope><systemPath>${basedir}/lib/aspose-cells-8.5.2.jar</systemPath></dependency>------------------------------------------------------------------------------------------------------------<!--設置maven-war-plugins插件,否則外部依賴無法打進war包--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.1.0</version><configuration><webResources><!-- 多模塊第三方jar引入配置,一個模塊一個resource--><resource><directory>../hxlinks-common/lib</directory><targetPath>WEB-INF/lib/</targetPath><includes><include>**/*.jar</include></includes></resource></webResources></configuration></plugin></plugins>
二、需求
1、word------>pdf
package com.hxlinks.hxiot.controller;import com.aspose.words.*;public class WordToPdf {public static void main(String[] args) throws Exception {// 創建Document對象Document doc = new Document("D:\\50\\templateFilePath\\目前問題.doc");// 初始化PDF保存選項PdfSaveOptions options = new PdfSaveOptions();// 保存為PDFdoc.save("D:/目前問題.pdf", options);}
}
2、doc------>docx
/*** 將doc的輸入流轉為docx的輸入流*/public static InputStream convertDoc2Docx(InputStream docInputStream) {try {byte[] inBytes = FileCopyUtils.copyToByteArray(docInputStream);byte[] docxBytes = convertDocStream2docxStream(inBytes);return new ByteArrayInputStream(docxBytes);} catch (IOException e) {e.printStackTrace();log.error("服務異常", e);return null;}}private static byte[] convertDocStream2docxStream(byte[] arrays) {byte[] docxBytes = new byte[1];if (arrays != null && arrays.length > 0) {try (ByteArrayOutputStream os = new ByteArrayOutputStream();InputStream sbs = new ByteArrayInputStream(arrays)) {Document doc = new Document(sbs);doc.save(os, SaveFormat.DOCX);docxBytes = os.toByteArray();} catch (Exception e) {e.printStackTrace();log.error("服務異常", e);}}return docxBytes;}
2、xls------>xlsx
/*** 將xls的輸入流轉為xlsx的輸入流*/public static InputStream convertXls2Xlsx(InputStream xlsInputStream) {try {byte[] inBytes = FileCopyUtils.copyToByteArray(xlsInputStream);byte[] xlsxBytes = convertXlsStream2XlsxStream(inBytes);return new ByteArrayInputStream(xlsxBytes);} catch (IOException e) {e.printStackTrace();log.error("服務異常", e);return null;}}/*** 轉換 xlsC轉xlsx*/private static byte[] convertXlsStream2XlsxStream(byte[] arrays) {byte[] xlsxBytes = new byte[1];XSSFWorkbook xwb = null;if (arrays != null && arrays.length > 0) {try (ByteArrayOutputStream os = new ByteArrayOutputStream();InputStream sbs = new ByteArrayInputStream(arrays);ByteArrayOutputStream hssfOs = new ByteArrayOutputStream()) {Workbook workbook = new Workbook(sbs);workbook.save(os, com.aspose.cells.SaveFormat.XLSX);xlsxBytes = os.toByteArray();xwb = new XSSFWorkbook(new ByteArrayInputStream(xlsxBytes));// 刪掉aspose生成的試用標記xwb.removeSheetAt(xwb.getNumberOfSheets() - 1);// 設置顯示excel第一頁xwb.setActiveSheet(0);xwb.write(hssfOs);return hssfOs.toByteArray();} catch (Exception e) {e.printStackTrace();log.error("服務異常", e);} finally {try {if (xwb != null) {xwb.close();}} catch (IOException e) {e.printStackTrace();log.error("服務異常", e);}}}return xlsxBytes;}