poi xlsx转换html,4.2 html转Excel更神奇的导出

#### 4.2 html转Excel更神奇的导出

这个是一个MM提出的需求,需求原因是,她要导出一个比较复杂的Excel,无论用模板还是注解都比较难实现,所以她想到了这个方案,然后就实现了如下的方法,我的使用方法如下 自己搞个html,然后用模板引擎,beetl,freemark等生成html,然后调用autopoi提供的方法转换成Excel,因为html的标签以及规则大家比Excel要熟悉的多,更容易编写复杂的table,然后autopoi转换成Excel再导出,麻烦了点,但是可以处理一些特定的情况,也同样生成两个版本的Excel都支持 使用demo

~~~

@Test

public void htmlToExcelByStr() throws Exception {

StringBuilder html = new StringBuilder();

Scanner s = new Scanner(getClass().getResourceAsStream("/html/sample.html"), "utf-8");

while (s.hasNext()) {

html.append(s.nextLine());

}

s.close();

Workbook workbook = ExcelXorHtmlUtil.htmlToExcel(html.toString(), ExcelType.XSSF);

File savefile = new File("D:\\home\\lemur");

if (!savefile.exists()) {

savefile.mkdirs();

}

FileOutputStream fos = new FileOutputStream("D:\\home\\lemur\\htmlToExcelByStr.xlsx");

workbook.write(fos);

fos.close();

workbook = ExcelXorHtmlUtil.htmlToExcel(html.toString(), ExcelType.HSSF);

fos = new FileOutputStream("D:\\home\\lemur\\htmlToExcelByStr.xls");

workbook.write(fos);

fos.close();

}

@Test

public void htmlToExcelByIs() throws Exception {

Workbook workbook = ExcelXorHtmlUtil.htmlToExcel(getClass().getResourceAsStream("/html/sample.html"), ExcelType.XSSF);

File savefile = new File("D:\\home\\lemur");

if (!savefile.exists()) {

savefile.mkdirs();

}

FileOutputStream fos = new FileOutputStream("D:\\home\\lemur\\htmlToExcelByIs.xlsx");

workbook.write(fos);

fos.close();

workbook = ExcelXorHtmlUtil.htmlToExcel(getClass().getResourceAsStream("/html/sample.html"), ExcelType.HSSF);

fos = new FileOutputStream("D:\\home\\lemur\\htmlToExcelByIs.xls");

workbook.write(fos);

fos.close();

}

~~~

提供了流或者字符串的入参,内部都多了缓存,多次生成不会重复解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值