Java實(shí)現(xiàn)Excel文件轉(zhuǎn)PDF(無水印無限制)
前言
java處理excel轉(zhuǎn)pdf一直沒找到什么好用的免費(fèi)jar包工具,自己手寫的難度,恐怕高級(jí)程序員花費(fèi)一年的事件,也不能做出來非常好用,再說誰會(huì)不賺錢,花費(fèi)一年事件去研究java如何實(shí)現(xiàn)excel轉(zhuǎn)pdf的,于是我找到了Aspose公司出的aspose-cells的java的jar包來實(shí)現(xiàn)。之前寫過一篇技術(shù)文章,不過后來覺得實(shí)現(xiàn)起來有些繁瑣,因?yàn)閍spose-cells沒有商業(yè)授權(quán),轉(zhuǎn)換出來的pdf都會(huì)帶文字和圖片水印,且轉(zhuǎn)換pdf的頁數(shù)也會(huì)被受限制,之前的邏輯是自己用aspose-cells轉(zhuǎn)換pdf后,又用apache-pdfbox去實(shí)現(xiàn)pdf的水印去除。這樣不僅浪費(fèi)了性能,還加長(zhǎng)了處理時(shí)間。于是這個(gè)版想從aspose-cells入手,破除商業(yè)版的限制。教程如下。
一、jar破解
1.項(xiàng)目遠(yuǎn)程倉庫配置
aspose-cells 這個(gè)需要配置單獨(dú)的倉庫地址才能下載,不會(huì)配置的可以去官網(wǎng)直接下載jar引入項(xiàng)目代碼中。
<repositories> <repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository> </repositories>
2.pom文件引入相關(guān)依賴
<!-- 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是一個(gè)開源的分析、編輯和創(chuàng)建Java字節(jié)碼的類庫。
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 { //創(chuàng)建文件(根據(jù)備份文件并刪除部分) 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(); } }
修改為你本機(jī)的aspose-cells-21.8.jar路徑,然后運(yùn)行主方法,破解成功后,會(huì)再同級(jí)文件夾下生成一個(gè)aspose-cells-21.8.cracked.jar包,用這個(gè)包替換原來的aspose-pdf-21.8.jar包即可。
二、Excel轉(zhuǎn)PDF
1.代碼實(shí)現(xiàn)
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文件轉(zhuǎn)換 * @param excelPath 需要被轉(zhuǎn)換的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 { //新建一個(gè)pdf文檔 String pdfPath=excelPath.substring(0,excelPath.lastIndexOf("."))+".pdf"; //Excel文件數(shù)據(jù) Workbook wb = new Workbook(excelPath); FileOutputStream fileOS = new FileOutputStream(pdfPath); //保存為pdf文件 wb.save(fileOS, SaveFormat.PDF); fileOS.close(); //轉(zhuǎn)化用時(shí) long now = System.currentTimeMillis(); System.out.println("EXCEL 轉(zhuǎn) Pdf 共耗時(shí):" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } } }
2.Aspose.License.xml 授權(quán)文件
代碼如下:
<License> <Data> <LicensedTo>Aspose Scotland Team</LicensedTo> <EmailTo>billy.lundie@aspose.com</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>
因?yàn)閖ar已破解其核心驗(yàn)證方法,里面的簽名可以隨便填寫,但是格式盡量保持一致,因?yàn)轵?yàn)證其他的格式方法還在!
運(yùn)行成功截圖
總結(jié)
經(jīng)測(cè)試轉(zhuǎn)換時(shí)間在幾秒之內(nèi),樣式?jīng)]有錯(cuò)亂,只是當(dāng)Excel的表格寬度,大于pdf的寬度時(shí)候,轉(zhuǎn)換后部分內(nèi)容后不顯示。
到此這篇關(guān)于Java實(shí)現(xiàn)Excel文件轉(zhuǎn)PDF(無水印無限制)的文章就介紹到這了,更多相關(guān)Java Excel轉(zhuǎn)PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Java搭建個(gè)簡(jiǎn)單的Netty通信實(shí)例教程
這篇文章主要給大家介紹了關(guān)于如何利用Java搭建個(gè)簡(jiǎn)單的Netty通信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05基于HTTP協(xié)議實(shí)現(xiàn)簡(jiǎn)單RPC框架的方法詳解
RPC全名(Remote?Procedure?Call),翻譯過來就是遠(yuǎn)程過程調(diào)用,本文將為大家介紹如何基于HTTP協(xié)議實(shí)現(xiàn)簡(jiǎn)單RPC框架,感興趣的小伙伴可以了解一下2023-06-06Tornadofx學(xué)習(xí)筆記之IconTextFx開源庫整合5000+個(gè)字體圖標(biāo)
這篇文章主要介紹了Tornadofx學(xué)習(xí)筆記之IconTextFx開源庫整合5000+個(gè)字體圖標(biāo)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Windows中在IDEA上安裝和使用JetBrains Mono字體的教程
這篇文章主要介紹了Windows IDEA上安裝和使用JetBrains Mono字體的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03