Java - Excel 转HTML、PDF、CSV

    技术2024-03-24  7

    Java - Excel 转HTML、PDF、CSV

    文件目录解决方法示例图片运行结果:

    最下边 gitee / github demo源码 最近做一个Excel转 html的需求,于是。 。。 用到了aspose-cells 包 ,可以选择官网下载,也可以gitte或github!!

    文件目录

    解决方法

    分别是一个获取License, 2个转换类,一个转换的方法! 最主要的代码,需要改动的!!!(32-48行)自己的文件路径

    class AsposeDemoApplicationTests { /** * 获取license 小心仔 * * @return */ public static boolean getLicense() { boolean result = false; try { InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br> * * @param args */ public static void main(String[] args) { // 验证License if (!getLicense()) { return; } try { long old = System.currentTimeMillis(); // 转换html ExcelConvertToHtml("D:\\\\BaiduNetdiskDownload\\\\aspose-demo\\\\src\\\\main\\\\resources\\\\static\\\\aaa.xlsx", "D:\\\\BaiduNetdiskDownload\\\\aspose-demo\\\\src\\\\main\\\\resources\\\\static\\\\aaa.html"); // 转换csv ExcelConvertToCSV("D:\\\\BaiduNetdiskDownload\\\\aspose-demo\\\\src\\\\main\\\\resources\\\\static\\\\aaa.xlsx", "D:\\\\BaiduNetdiskDownload\\\\aspose-demo\\\\src\\\\main\\\\resources\\\\static\\\\aaa.csv"); // 转换pdf Workbook wb = new Workbook("D:\\BaiduNetdiskDownload\\aspose-demo\\src\\main\\resources\\static\\aaa.xlsx");// 原始excel路径 File pdfFile = new File("D:\\BaiduNetdiskDownload\\aspose-demo\\src\\main\\resources\\static\\aaa.pdf");// 输出路径 FileOutputStream fileOS = new FileOutputStream(pdfFile); wb.save(fileOS, SaveFormat.PDF); long now = System.currentTimeMillis(); System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } } /** * Excel 2 csv转换类 * @param sourceFilePath 传入路径 * @param csvFilePath 输出路径 * @throws Exception 异常 */ public static void ExcelConvertToCSV(String sourceFilePath, String csvFilePath) throws Exception { com.aspose.cells.Workbook excel = null; excel = new com.aspose.cells.Workbook(sourceFilePath); excel.save(csvFilePath, com.aspose.cells.SaveFormat.CSV); } /** * Excel 2 HTML转换类 * @param sourceFilePath 传入路径 * @param htmlFilePath 输出路径 * @throws Exception 异常 */ public static void ExcelConvertToHtml(String sourceFilePath, String htmlFilePath) throws Exception { com.aspose.cells.LoadOptions loadOption = null; com.aspose.cells.Workbook excel = null; if (sourceFilePath != null && !sourceFilePath.isEmpty() && sourceFilePath .substring(sourceFilePath.lastIndexOf(".")) .toLowerCase() == ".csv") { loadOption = new com.aspose.cells.TxtLoadOptions( com.aspose.cells.LoadFormat.AUTO); } if (loadOption != null) { excel = new com.aspose.cells.Workbook(sourceFilePath, loadOption); } else { excel = new com.aspose.cells.Workbook(sourceFilePath); } excel.save(htmlFilePath, com.aspose.cells.SaveFormat.HTML); } }

    license.xml

    <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>

    示例图片

    运行

    结果:

    带有多sheet页 pdf:

    github地址:https://github.com/Beful/aspose-demo gitee地址:https://gitee.com/zhangx00098/aspose-demo

    Processed: 0.018, SQL: 8