EasyExcel–基础–4.2–写Excel–自定义表头
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/easyExcel_learn
1、代码
![在这里插入图片描述](https://img-blog.csdnimg.cn/aa9b0941f4934680b9361f746993d079.png#pic_center)
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<List<String>> 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<List<String>> contents = Lists.newArrayList();
for (int i = 0; i <= 10; i++) {
List<String> 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、结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/f6e421138a9d4b52bde524784f36a230.png#pic_center)