如何使用Java实现Word/Excel/TXT转PDF功能
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章将为大家详细讲解有关如何使用Java实现Word/Excel/TXT转PDF功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1:添加maven依赖 aspose pdf 11.5.0 aspose words 16.4.0 aspose cell 8.9.2 aspose pdf 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:文本文件转码
/* 将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(); // 新建一个空白文档 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进行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF间转换 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } 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(); // 新建一个空白文档 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进行OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF间转换 */ doc.save(os, SaveFormat.PDF); flag = true; //long now = System.currentTimeMillis(); //System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } if (!flag) { file.deleteOnExit(); } } }关于"如何使用Java实现Word/Excel/TXT转PDF功能"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
文件
文档
篇文章
UTF-8
功能
更多
空白
支持
不错
实用
代码
内容
文件夹
文本
文章
知识
编码
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
云南咖啡
上海睿民互联网科技有限公司
互联网科技公司经营范围
互联网科技工作委员会毛军
互联网是不是新科技
没上市的科技互联网公司
互联网公司 科技公司
左右互联网络科技
互联网 科技服务平台
成都仁贤领冠互联网科技有限公司
互联网巨头投资的硬核科技
华服互联网科技官网
湖北码尼互联网科技有限公司电话
宝灵互联网科技有限公司
移动互联网年度科技创新
辽宁互联网新科技
佛山市翊博互联网络科技有限公司
每天都有哪些好的互联网科技
云南省自由互联网科技有限公司
科技互联网经济论坛
互联网科技园
喔噻互联网科技有限公司
A股互联网金融科技公司
互联网与科技发展
重庆互联网络科技
互联网络科技有限公司
碳银互联网科技
央行互联网金融科技公司
华服互联网科技官网
互联网科技股为什么大涨