![这里是引用](https://i-blog.csdnimg.cn/blog_migrate/280f7f15585963fe5e3ca3ff012b8fd1.png)
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.2</version>
</dependency>
package com.example.demo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 表格标题无映射导出
* </p>
*
* @author shuai
* @since 2021/12/20
*/
@SpringBootTest
public class exportExcelTest {
@Value("${file.path}")
private String filePath;
@Test
public void exportExcel(){
String filePath = this.filePath+ File.separator + "exportExcel"+System.currentTimeMillis() + ".xlsx";
EasyExcel.write(filePath)
.head(this.getHead()) // 表格标题
.sheet("测试") // sheet页名称
.registerWriteHandler(setCellStyle()) // 样式
.doWrite(this.getData()); // 数据
}
/**
* 表格标题
* @return
*/
private static List<List<String>> getHead(){
List<List<String>> head = new ArrayList<>();
head.add(Lists.newArrayList("合并单元格","序号"));
head.add(Lists.newArrayList("合并单元格","名称"));
head.add(Lists.newArrayList("合并单元格","年龄"));
head.add(Lists.newArrayList("测试自动换行"));
return head;
}
/**
* 导出数据
* @return
*/
private static List<List<Object>> getData(){
List<List<Object>> dataList = new ArrayList<List<Object>>();
for (int i = 1; i < 6; i++) {
List<Object> data = new ArrayList<>();
data.add(i);
data.add("名称"+i);
data.add(i+10);
data.add("测试自动换行"+i);
dataList.add(data);
}
return dataList;
}
/**
* 表格样式
* @return
*/
private static HorizontalCellStyleStrategy setCellStyle() {
// 表格标题样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 设置标题居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置标题颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.getIndex());
// 标题字体
WriteFont headFont = new WriteFont();
// 设置标题字体大小
headFont.setFontHeightInPoints((short) 12);
headWriteCellStyle.setWriteFont(headFont);
headFont.setColor(IndexedColors.BLUE.getIndex());
//设置自动换行
headWriteCellStyle.setWrapped(true);
// 数据样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 设置数据居中对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//设置数据自动换行
contentWriteCellStyle.setWrapped(true);
//设置边框样式
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
// 设置数据背景色
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
return horizontalCellStyleStrategy;
}
}