EasyExcel--基础--4.2--写Excel--自定义表头

EasyExcel–基础–4.2–写Excel–自定义表头


代码位置

https://gitee.com/DanShenGuiZu/learnDemo/tree/master/easyExcel_learn

1、代码

EasyExcel--基础--4.2--写Excel--自定义表头_第1张图片

package fei.zhou.easyexcel_learn.business.demo8;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import org.assertj.core.util.Lists;

import java.io.File;
import java.util.List;


public class Test {

    public static void main(String[] args) throws Exception {

        //文件路径
        File file = new File("src\\main\\java\\fei\\zhou\\easyexcel_learn\\business\\demo8\\test1.xlsx");

        String filePath = file.getAbsolutePath();
        //设置列名
        List> heads = Lists.newArrayList();
        heads.add(Lists.newArrayList("表头1"));
        heads.add(Lists.newArrayList("表头2"));
        heads.add(Lists.newArrayList("表头3"));
        heads.add(Lists.newArrayList("表头4"));
        heads.add(Lists.newArrayList("表头5"));

        //设置 行列数据
        List> contents = Lists.newArrayList();
        for (int i = 0; i <= 10; i++) {
            List content = Lists.newArrayList();
            for (int j = 0; j < 5; j++) {
                content.add("第" + i + "行,第" + j + "例内容");
            }
            contents.add(content);
        }


        ExcelWriterBuilder write = EasyExcel.write(filePath);
        // 构建列表
        write.head(heads);

        // 构建sheet
        ExcelWriterSheetBuilder sheet = write.sheet("销售订单");
        // 写数据
        sheet.doWrite(contents);


    }

}

2、结果

EasyExcel--基础--4.2--写Excel--自定义表头_第2张图片

你可能感兴趣的:(EasyExcel,excel)