新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)如何使用Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
10多年的沁縣網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整沁縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“沁縣網(wǎng)站設(shè)計”,“沁縣網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
1:添加maven依賴
aspose 11.5.0 aspose words 16.4.0 aspose cell 8.9.2 aspose 11.5.0
2:添加license-excel.xml文件(Resource文件夾下)
Aspose.Total for Java Aspose.Words for Java Enterprise 20991231 20991231 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7 sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
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:文本文件轉(zhuǎn)碼
/* 將txt 轉(zhuǎn)換編碼 * @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("轉(zhuǎn)碼結(jié)束"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; }
3.3:word和txt轉(zhuǎn)換pdf
/** * 將word txt轉(zhuǎn)換成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(); // 新建一個空白文檔 file = new File(outPath); file = saveAsUTF8(file); os = new FileOutputStream(file); // InPath是將要被轉(zhuǎn)化的文檔 com.aspose.words.Document doc = new com.aspose.words.Document(inPath); /* * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉(zhuǎn)換 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉(zhuǎn)化用時 } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } if (!flag) { file.deleteOnExit(); } } }
3.4:Excel轉(zhuǎn)換pdf
/** * 將docx轉(zhuǎn)換成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(); // 新建一個空白文檔 file = new File(outPath); file = saveAsUTF8(file); os = new FileOutputStream(file); // InPath是將要被轉(zhuǎn)化的文檔 com.aspose.words.Document doc = new com.aspose.words.Document(inPath); /* * 全面支持DOC,DOCX進(jìn)行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF間轉(zhuǎn)換 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉(zhuǎn)化用時 } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } if (!flag) { file.deleteOnExit(); } } }
關(guān)于“如何使用Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
當(dāng)前名稱:如何使用Java實現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF功能
路徑分享:http://www.ef60e0e.cn/article/gihggs.html