html table 转 excel

html table 转 excel输出

今天有一个需求,将html中的table输出到excel文件,
在csdn找了半天真的绝望,很多都是复制粘贴别人的代码,还居然复制不全,东缺一个类,西缺一个方法,依赖也不清不楚,复制不全就算了,源帖子的链接又不放上来,测试方法都通过不了都不知道复制来干嘛。最后在gayhub找到一个开源库,几行代码搞定,开源万岁,gayhub yyds!


1、maven依赖

        <dependency>
            <groupId>com.github.liaochong</groupId>
            <artifactId>myexcel</artifactId>
            <version>3.11.4</version>
        </dependency>

2、测试方法

import com.github.liaochong.myexcel.core.HtmlToExcelFactory;
import com.github.liaochong.myexcel.utils.FileExportUtil;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.IOException;

/**
 * @author chow
 * @date 2021/5/11 16:10
 */
public class ExcelTest {

    public static void main(String[] args) throws IOException {
        String html = "<table><tr class=\"titlebg\"><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">序号</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">计划</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">部门名称</td><td align=\"center\" nowrap=\"nowrap\" colspan=\"6\">线上缴费</td><td align=\"center\" nowrap=\"nowrap\" colspan=\"3\">线下缴费</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">总计</td></tr><tr class=\"titlebg\"><td align=\"center\" nowrap=\"nowrap\">线上总计</td><td align=\"center\" nowrap=\"nowrap\">快钱</td><td align=\"center\" nowrap=\"nowrap\">支付宝</td><td align=\"center\" nowrap=\"nowrap\">平台余款</td><td align=\"center\" nowrap=\"nowrap\">激活卡</td><td align=\"center\" nowrap=\"nowrap\">其他</td><td align=\"center\" nowrap=\"nowrap\">线下总计</td><td align=\"center\" nowrap=\"nowrap\">本地缴费</td><td align=\"center\" nowrap=\"nowrap\">中心收费</td></tr><tr class=\"whbg\" orgPath=\"01.25.01.\" planId=\"9e508516-5409-4b5d-a0d6-f86ba77eb79f\" ><td align=\"center\" nowrap=\"nowrap\">1</td><td align=\"center\" nowrap=\"nowrap\">盐城2013年培训计划</td><td align=\"center\" nowrap=\"nowrap\">盐城市</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\"><a id=\"0-12\" href=\"javascript:showDetail('0-12');\">3</a></td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\"><a id=\"0-15\" href=\"javascript:showDetail('0-15');\">3</a></td></tr><tr class=\"whbg\" orgPath=\"all\" planId=\"all\"><td align=\"center\"  nowrap=\"nowrap\" colspan=\"3\" >总计</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" ><a id=\"4-6\" href=\"javascript:showDetail('4-6');\">3</a></td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" ><a id=\"4-9\" href=\"javascript:showDetail('4-9');\">3</a></td></tr></table>";
        HtmlToExcelFactory htmlToExcelFactory = HtmlToExcelFactory.readHtml(html);
        Workbook build = htmlToExcelFactory.build();
        File os = new File("C:/Users/DELL/Desktop/aa");
        FileExportUtil.export(build, os);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值