java 拼html_Html-builder

农诚
2023-12-01

public static void main(String[] args) {

collectionToTable();

collectionToExcel();

tableToExcel();

}

private static void tableToExcel() {

Table table = Table.table();

table.head(new Tr("标题", "会加粗", new Td("背景颜色"), "会变蓝"));

table.tr(new Tr(new Td("竖着占两格").rowspan(2), "本行文字居中", new Td("横着占两格").colspan(2)).alignCenter());

table.tr(new Tr("b", "c", "d"));

table.tr(new Tr("a", 3, "c", 1));

table.tr(new Tr("a", new Td(2), "c", 0));

table.tr(new Tr("a", 3, "c", 1));

table.tr(new Tr("a", 3, "c", 1));

table.tr(new Tr("a", 3, "c", 1));

System.out.println(table);

File excel = ExcelUtils.tableToExcelFile(table, "测试文件");

SXSSFWorkbook excelObject = ExcelUtils.tableToExcel(table, true);

}

private static void collectionToExcel() {

List dataList = getTestModels();

File excelFile = ExcelUtils.toExcelFile(dataList, "测试表格");

SXSSFWorkbook excel = ExcelUtils.toExcel(dataList);

}

private static void collectionToTable() {

List dataList = getTestModels();

Table table = HtmlUtil.toTable(dataList);

// address字段没有注解,不会输出到table中

// 数据集合中的email字段全是null,也不会输出到table中

System.out.println(table.html());

}

private static List getTestModels() {

List dataList = new ArrayList<>();

TestModel t1 = new TestModel(1, "小明", "13012341234", "北京市朝阳区", null, "2018-10-5");

TestModel t2 = new TestModel(2, "池子", "13512341234", "北京市海淀区", null, "2014-9-1");

dataList.add(t1);

dataList.add(t2);

return dataList;

}

public class TestModel {

@TableTag("用户ID")

private Integer id;

@TableTag("姓名")

private String name;

@TableTag("手机号")

private String phone;

// 该字段没有注解,并不会输出到table或者excel中

private String address;

@TableTag("邮箱")

private String email;

@TableTag("生日")

private String birthDay;

public TestModel(Integer id, String name, String phone, String address, String email, String birthDay) {

this.id = id;

this.name = name;

this.phone = phone;

this.address = address;

this.email = email;

this.birthDay = birthDay;

}

省略set and get...

}

 类似资料: