Java實現Excel文件轉PDF(無水印無限制)
前言
java處理excel轉pdf一直沒找到什麼好用的免費jar包工具,自己手寫的難度,恐怕高級程序員花費一年的事件,也不能做出來非常好用,再說誰會不賺錢,花費一年事件去研究java如何實現excel轉pdf的,於是我找到瞭Aspose公司出的aspose-cells的java的jar包來實現。之前寫過一篇技術文章,不過後來覺得實現起來有些繁瑣,因為aspose-cells沒有商業授權,轉換出來的pdf都會帶文字和圖片水印,且轉換pdf的頁數也會被受限制,之前的邏輯是自己用aspose-cells轉換pdf後,又用apache-pdfbox去實現pdf的水印去除。這樣不僅浪費瞭性能,還加長瞭處理時間。於是這個版想從aspose-cells入手,破除商業版的限制。教程如下。
一、jar破解
1.項目遠程倉庫配置
aspose-cells 這個需要配置單獨的倉庫地址才能下載,不會配置的可以去官網直接下載jar引入項目代碼中。
<repositories> <repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository> </repositories>
2.pom文件引入相關依賴
<!-- https://mvnrepository.com/artifact/com.aspose/aspose-cells --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cells</artifactId> <version>21.8</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency>
Javassist是一個開源的分析、編輯和創建Java字節碼的類庫。
3.代碼破解
import javassist.*; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; public class ExcelJarCrack { public static void main(String[] args) throws Exception { String jarPath = "C:\\Users\\liuya\\Desktop\\jar\\aspose-cells-21.8.jar"; crack(jarPath); } private static void crack(String jarName) throws NotFoundException, CannotCompileException, IOException { //這一步是完整的jar包路徑 ClassPool.getDefault().insertClassPath(jarName); CtClass LicenseClass = ClassPool.getDefault().getCtClass("com.aspose.cells.License"); CtMethod[] aMethods = LicenseClass.getDeclaredMethods("a"); for (CtMethod aMethod : aMethods) { CtClass returnType=aMethod.getReturnType(); if(returnType.getName().equals("boolean")){ aMethod.setBody("{return true;}"); break; } } //將文件名命名成備份文件 File file=new File(jarName); LicenseClass.writeFile(file.getParent()); disposeJar(jarName); } private static void disposeJar(String jarName) { List<String> deletes = new ArrayList<>(); deletes.add("META-INF/37E3C32D.SF"); deletes.add("META-INF/37E3C32D.RSA"); List<String> replaces = new ArrayList<>(); replaces.add("com/aspose/cells/License.class"); File oriFile = new File(jarName); if (!oriFile.exists()) { System.out.println("######Not Find File:" + jarName); return; } //將文件名命名成備份文件 String bakJarName = jarName.substring(0, jarName.length() - 3) + "cracked.jar"; try { //創建文件(根據備份文件並刪除部分) JarFile jarFile = new JarFile(jarName); JarOutputStream jos = new JarOutputStream(new FileOutputStream(bakJarName)); Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); if (!deletes.contains(entry.getName())) { if(replaces.contains(entry.getName())){ System.out.println("Replace:-------" +entry.getName()); JarEntry jarEntry = new JarEntry(entry.getName()); jos.putNextEntry(jarEntry); FileInputStream fin = new FileInputStream(oriFile.getParent()+ "/"+entry.getName()); byte[] bytes = readStream(fin); jos.write(bytes, 0, bytes.length); }else { jos.putNextEntry(entry); byte[] bytes = readStream(jarFile.getInputStream(entry)); jos.write(bytes, 0, bytes.length); } } else { System.out.println("Delete:-------" + entry.getName()); } } jos.flush(); jos.close(); jarFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } }
修改為你本機的aspose-cells-21.8.jar路徑,然後運行主方法,破解成功後,會再同級文件夾下生成一個aspose-cells-21.8.cracked.jar包,用這個包替換原來的aspose-pdf-21.8.jar包即可。
二、Excel轉PDF
1.代碼實現
import com.aspose.cells.License; import com.aspose.cells.SaveFormat; import com.aspose.cells.Workbook; import java.io.FileOutputStream; public class PdfUtils { public static void main(String[] args) { excelToPdf("C:\\Users\\liuya\\Desktop\\excel\\test.xlsx"); } /** * Excel文件轉換 * @param excelPath 需要被轉換的excel全路徑帶文件名 * @Return void */ public static void excelToPdf(String excelPath) { License license = new License(); license.setLicense("C:\\Users\\liuya\\Desktop\\jar\\Aspose.License.xml"); long old = System.currentTimeMillis(); try { //新建一個pdf文檔 String pdfPath=excelPath.substring(0,excelPath.lastIndexOf("."))+".pdf"; //Excel文件數據 Workbook wb = new Workbook(excelPath); FileOutputStream fileOS = new FileOutputStream(pdfPath); //保存為pdf文件 wb.save(fileOS, SaveFormat.PDF); fileOS.close(); //轉化用時 long now = System.currentTimeMillis(); System.out.println("EXCEL 轉 Pdf 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } } }
2.Aspose.License.xml 授權文件
代碼如下:
<License> <Data> <LicensedTo>Aspose Scotland Team</LicensedTo> <EmailTo>[email protected]</EmailTo> <LicenseType>Developer OEM</LicenseType> <LicenseNote>Limited to 1 developer, unlimited physical locations</LicenseNote> <OrderID>140408052324</OrderID> <UserID>94236</UserID> <OEM>This is a redistributable license</OEM> <Products> <Product>Aspose.Total for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SerialNumber>9a59547c-41f0-428b-ba72-7c4368f151d7</SerialNumber> <SubscriptionExpiry>20221231</SubscriptionExpiry> <LicenseVersion>3.0</LicenseVersion> <LicenseInstructions>http://www.aspose.com/corporate/purchase/license-instructions.aspx</LicenseInstructions> </Data> <Signature>FO3PHsblgDt8F59sMT1l1amyi9qk2V6E8dQkIP7LdTJSxDibNEFu1zOinQbqFfKv/ruttvcxoROkc1tUe0DtO6cP1Zf6J0VemgSY8i/LZECTGszRqJVQRZ0MoVnBhuPAJk5eli7fhVcF8hWd3E4XQ3LzfmJCuaj2NEteRi5Hrfg=</Signature> </License>
因為jar已破解其核心驗證方法,裡面的簽名可以隨便填寫,但是格式盡量保持一致,因為驗證其他的格式方法還在!
運行成功截圖
總結
經測試轉換時間在幾秒之內,樣式沒有錯亂,隻是當Excel的表格寬度,大於pdf的寬度時候,轉換後部分內容後不顯示。
到此這篇關於Java實現Excel文件轉PDF(無水印無限制)的文章就介紹到這瞭,更多相關Java Excel轉PDF內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- c#使用Aspose打印文件的示例
- Java實現無損Word轉PDF的示例代碼
- 關於spring 掃描不到jar中class文件的原因分析及解決
- java集合進行排序的方式總結
- 超詳細解釋Java反射