持續(xù)創(chuàng)作,加速成長!這是我參與「掘金日新計劃 · 10 月更文挑戰(zhàn)」的第11天,點擊查看活動詳情
今天這是篇粗糙的文章,文字也較少,整理了個Java將Excel 轉換為 PDF 文件的工具類。(還比較粗糙粗糙~)
用的是?aspose
?和pdfbox
實現(xiàn)的。
aspose
是沒辦法在公開的Maven
倉庫下載的。得去它官網下載,或者是去互聯(lián)網上搜一搜~
獲取 Aspose
官網地址:aspose
我是在網上沖浪拿到的~ ,有需求的私我就好~
其中里面的?license.xml
?文件,是參考下面文章獲得:
Java操作excel轉pdf工具類???這篇文章中的代碼是失敗的,我?guī)湍泸炞C了...
?<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>
復制代碼
如果不加這個license
,在PDF文件的頂部會出現(xiàn)水印~
工具類的實現(xiàn)
導入相關依賴:
?<dependencies>
? ? ?<dependency>
? ? ? ? ?<groupId>com.lowagie</groupId>
? ? ? ? ?<artifactId>itext</artifactId>
? ? ? ? ?<version>2.1.7</version>
? ? ?</dependency>
? ? ?<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
? ? ?<dependency>
? ? ? ? ?<groupId>org.apache.pdfbox</groupId>
? ? ? ? ?<artifactId>pdfbox</artifactId>
? ? ? ? ?<version>2.0.24</version>
? ? ?</dependency>
? ? ?<dependency>
? ? ? ? ?<groupId>com.aspose</groupId>
? ? ? ? ?<artifactId>aspose-cells</artifactId>
? ? ? ? ?<version>8.6.2</version>
? ? ?</dependency>
?</dependencies>
復制代碼
注意
:此處的aspose-cells
是由我手動放入我本地的Maven倉庫后,再手動導入至項目中的。
不知道怎么手動導入Jar的小伙伴,互聯(lián)網沖浪一下即可~
編寫代碼~
?package com.utils;
??
?import com.aspose.cells.License;
?import com.aspose.cells.PdfCompliance;
?import com.aspose.cells.PdfSaveOptions;
?import com.aspose.cells.Workbook;
?import org.apache.pdfbox.pdmodel.PDDocument;
??
??
?import java.io.*;
?import java.util.List;
??
?public class ExcelToPdf {
??
? ? ?private static License asposeLic;
??
? ? ?public static void main(String[] args) throws IOException {
? ? ? ? ?//convertPDFImage("d:\demo.xls", null);
? ? ? ? ?// 直接在相同目錄輸出pdf文件
? ? ? ? ?ExcelToPdf.convertPDF("E:\456.xlsx");
? ? }
??
? ? ?public static String convertPDF(String excelFileName) {
? ? ? ? ?return convertPDF(false, excelFileName);
? ? }
??
? ? ?/**
? ? ? * 是否每頁大小自適應(超頁不分頁)
? ? ? *
? ? ? * @param onePagePerSheet excel每sheet頁生成一頁pad
? ? ? * @param excelFileName excel 文件
? ? ? * @return pdf
? ? ? */
? ? ?public static String convertPDF(boolean onePagePerSheet, String excelFileName) {
? ? ? ? ?String pdfFileName = "";
? ? ? ? ?PDDocument pdfDocument = null;
? ? ? ? ?try {
? ? ? ? ? ? ?Workbook workbook = new Workbook(excelFileName);
? ? ? ? ? ? ?getLicense();
? ? ? ? ? ? ?PdfSaveOptions saveOptions = new PdfSaveOptions();
? ? ? ? ? ? ?saveOptions.setCompliance(PdfCompliance.PDF_A_1_B);
? ? ? ? ? ? ?saveOptions.setOnePagePerSheet(onePagePerSheet);
? ? ? ? ? ? ?pdfFileName = excelFileName.substring(0, excelFileName.lastIndexOf(".")) + ".pdf";
? ? ? ? ? ? ?workbook.save(pdfFileName, saveOptions);
? ? ? ? ? ? ?pdfDocument = PDDocument.load(new File(pdfFileName));
? ? ? ? ? ? ?// Output file name
? ? ? ? ? ? ?pdfDocument.save(pdfFileName);
? ? ? ? ? ? ?editPDF(pdfFileName);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? }finally {
? ? ? ? ? ? ?if (pdfDocument != null) {
? ? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ? ?pdfDocument.close();
? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ?return pdfFileName;
? ? }
??
? ? ?/**
? ? ? * 獲取license
? ? ? *
? ? ? * @return
? ? ? */
? ? ?public static boolean getLicense() {
? ? ? ? ?boolean result = false;
? ? ? ? ?InputStream license = null;
? ? ? ? ?try {
? ? ? ? ? ? ?license = ExcelToPdf.class.getClassLoader().getResourceAsStream("\license.xml");
? ? ? ? ? ? ?asposeLic = new License();
? ? ? ? ? ? ?asposeLic.setLicense(license);
? ? ? ? ? ? ?result = true;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ?if (license != null) {
? ? ? ? ? ? ? ? ? ? ?license.close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ?return result;
? ? }
??
? ? ?public static boolean delete(String strFileName) {
? ? ? ? ?File fileDelete = new File(strFileName);
??
? ? ? ? ?if (!fileDelete.exists() || !fileDelete.isFile()) {
? ? ? ? ? ? ?System.out.println("錯誤: " + strFileName + "不存在!");
? ? ? ? ? ? ?return false;
? ? ? ? }
??
? ? ? ? ?return fileDelete.delete();
? ? }
??
?}
復制代碼
測試結果:
測試控制臺是沒有輸出的,只要正常結束就表示成功了。
后續(xù)的擴展還沒有想好~,還可以使excel轉成圖片,也可以輸出到瀏覽器。
今天只是個開端~
后記
今天就寫到了這里啦~ 感覺自己還好菜啊~ 一起努力哦~文章來源:http://www.zghlxwxcb.cn/news/detail-483265.html
希望你是滿載而歸的~文章來源地址http://www.zghlxwxcb.cn/news/detail-483265.html
到了這里,關于Java 將 Excel 轉換為 PDF 文件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!