html csv 转excel,Java - Excel 转HTML、PDF、CSV

最下边 gitee / github demo源码

最近做一个Excel转 html的需求,于是。 。。

用到了aspose-cells 包 ,可以选择官网下载,也可以gitte或github!!

文件目录

b568688b09af2ecad8df553661f7ec73.png

解决方法

分别是一个获取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等相互转换

*

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

Aspose.Total for Java

Aspose.Words for Java

Enterprise

20991231

20991231

8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7

sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

示例图片

运行

3c4102829fa719f29d0e877a36c77db4.png

1b2825aacb73a626d3f74387811388b9.png

结果:

945bef34cbb4be4254cf6fd84dcd64af.png

带有多sheet页

e929860c895641cddac6d74eaf5a7534.png

pdf:

f52f4ebc9f3ae5a53a64d70e4939d5fb.png

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值