- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- 如何使用Java實(shí)現Word/Excel/TXT轉PDF功能
這篇文章將為大家詳細講解有關(guān)如何使用Java實(shí)現Word/Excel/TXT轉PDF功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1:添加maven依賴(lài)
<!--excel word txt ppt轉pdf依賴(lài)--> <dependency> <groupId>aspose</groupId> <artifactId>pdf</artifactId> <version>11.5.0</version> </dependency> <dependency> <groupId>aspose</groupId> <artifactId>words</artifactId> <version>16.4.0</version> </dependency> <dependency> <groupId>aspose</groupId> <artifactId>cell</artifactId> <version>8.9.2</version> </dependency> <dependency> <groupId>aspose</groupId> <artifactId>pdf</artifactId> <version>11.5.0</version> </dependency>
2:添加license-excel.xml文件(Resource文件夾下)
<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>
3:代碼如下:
3.1獲取License文件
public static boolean getLicense(){ boolean result = false; InputStream is = null; try{ is =UploadFiles.class.getClassLoader().getResourceAsStream("license-excel.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; }catch(Exception e){ e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; }
3.2:文本文件轉碼
/* 將txt 轉換編碼 * @param file * @author zsqing */public File saveAsUTF8(File file){ String code = "gbk"; byte[] head = new byte[3]; try { InputStream inputStream = new FileInputStream(file); inputStream.read(head); if (head[0] == -1 && head[1] == -2) { code = "UTF-16"; } else if (head[0] == -2 && head[1] == -1) { code = "Unicode"; } else if (head[0] == -17 && head[1] == -69 && head[2] == -65) { code = "UTF-8"; } inputStream.close(); System.out.println(code); if (code.equals("UTF-8")) { return file; } String str = FileUtils.readFileToString(file, code); FileUtils.writeStringToFile(file, str, "UTF-8"); System.out.println("轉碼結束"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; }
3.3:word和txt轉換pdf
/** * 將word txt轉換成pdf * @param inPath * @param outPath * @author zsqing*/public void wordAndTextToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request) { String fileToPdfUrl=""; boolean flag = false; File file = null; FileOutputStream os = null; try { //long old = System.currentTimeMillis(); // 新建一個(gè)空白文檔 file = new File(outPath); file = saveAsUTF8(file); os = new FileOutputStream(file); // InPath是將要被轉化的文檔 com.aspose.words.Document doc = new com.aspose.words.Document(inPath); /* * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉換 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗時(shí):" + ((now - old) / 1000.0) + "秒"); // 轉化用時(shí) } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } if (!flag) { file.deleteOnExit(); } } }
3.4:Excel轉換pdf
/** * 將docx轉換成pdf * @param inPath * @param outPath * @author zsqing */ public void wordToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request) { String fileToPdfUrl=""; boolean flag = false; File file = null; FileOutputStream os = null; try { //long old = System.currentTimeMillis(); // 新建一個(gè)空白文檔 file = new File(outPath); file = saveAsUTF8(file); os = new FileOutputStream(file); // InPath是將要被轉化的文檔 com.aspose.words.Document doc = new com.aspose.words.Document(inPath); /* * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉換 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗時(shí):" + ((now - old) / 1000.0) + "秒"); // 轉化用時(shí) } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } if (!flag) { file.deleteOnExit(); } } }
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站