详情代码
实体类
java
@Data
public class ComplexHeadData {
@ExcelProperty({"主标题", "字符串标题"})
private String string;
@ExcelProperty({"主标题", "日期标题"})
private Date date;
@ExcelProperty({"主标题", "数字标题"})
private Double doubleData;
}
Controller层
java
@RestController
@RequestMapping("/ex/test")
public class DemoMergeDataController {
@GetMapping("/getTestDemo")
public R getTestDemo(HttpServletResponse response) throws IOException {
// extracted(response);
// 定义导出的Excel文件名
String fileName = "ComplexHeadData.xlsx";
// 设置响应的内容类型为二进制流,这是文件下载的标准设置
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// 设置响应头的Content-Disposition,使用"attachment"指示浏览器这是一个需要下载的文件,
// 并使用URLEncoder对文件名进行编码处理,确保文件名的兼容性跨平台和浏览器
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, String.valueOf(StandardCharsets.UTF_8)));
EasyExcel.write(response.getOutputStream(),ComplexHeadData.class).sheet().doWrite(data());
return R.success();
}
//写入数据生成
private List<ComplexHeadData> data() {
List<ComplexHeadData> list = new ArrayList<ComplexHeadData>();
for (int i = 0; i < 10; i++) {
ComplexHeadData data = new ComplexHeadData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
测试