代码如下:
public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {
//表头样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置表头居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setDataFormat((short) 49);
//设置内容靠左对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(getOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy).doWrite(data);
}
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
return response.getOutputStream();
}
第一段:Excel
的单元格格式
图中的数据有数值、货币、时间、日期、文本等格式。这些数据格式在POI中的HSSFDataFormat
类里都有相应的定义。
HSSFDataFormat
是HSSF
子项目里面定义的一个类。类HSSFDataFormat
允许用户新建数据格式类型。HSSFDataFormat
类包含静态方法
static java.lang.String getBuiltinFormat(short index)
它可以根据编号返回内置数据类型。
static short getBuiltinFormat(java.lang.String format)
方法则可以根据数据类型返回其编号,
static java.util.List getBuiltinFormats()
可以返回整个内置的数据格式列表。
在HSSFDataFormat
里一共定义了49
种内置的数据格式,如下面所示。
HSSFDataFormat的数据格式
内置数据类型 | 编号 |
---|---|
“General” | 0 |
“0” | 1 |
“0.00” | 2 |
“#,##0” | 3 |
“#,##0.00” | 4 |
“(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0_);(#,##0)” | 5 |
“(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0_);[Red](#,##0)” | 6 |
“(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0.00);(#,##0.00)” | 7 |
“(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0.00_);[Red]…#,##0.00)” | 8 |
“0%” | 9 |
“0.00%” | 0xa |
“0.00E+00” | 0xb |
“# ?/?” | 0xc |
“# ??/??” | 0xd |
“m/d/yy” | 0xe |
“d-mmm-yy” | 0xf |
“d-mmm” | 0x10 |
“mmm-yy” | 0x11 |
“h:mm AM/PM” | 0x12 |
“h:mm:ss AM/PM” | 0x13 |
“h:mm” | 0x14 |
“h:mm:ss” | 0x15 |
“m/d/yy h:mm” | 0x16 |
保留为过国际化用 | 0x17 - 0x24 |
“(#,##0_)😭#,##0)” | 0x25 |
“(#,##0_);Red” | 0x26 |
“(#,##0.00_)😭#,##0.00)” | 0x27 |
“(#,##0.00_);Red” | 0x28 |
“(KaTeX parse error: Expected 'EOF', got '#' at position 2: *#̲,##0_);_(*(#,##0);($* “-”);(@_)” | 0x29 |
“(*#,##0.00);(*(#,##0.00);(*”-"??);(@_)" | 0x2a |
“(KaTeX parse error: Expected 'EOF', got '#' at position 2: *#̲,##0.00_);_(*(#,##0.00);($*”-"??);(@_)" | 0x2b |
“(KaTeX parse error: Expected 'EOF', got '#' at position 2: *#̲,##0.00_);_(*(#,##0.00);($*”-"??);(@_)" | 0x2c |
“mm:ss” | 0x2d |
“[h]:mm:ss” | 0x2e |
“mm:ss.0” | 0x2f |
“##0.0E+0” | 0x30 |
“@” - This is text format | 0x31 |
在上面表中,字符串类型所对应的是数据格式为"@
"(最后一行),也就是HSSFDataFormat
中定义的值为0x31
(49
)的那行。在本例中,全部使用文本格式,不过只能对写入的单元格生效,对于没有写入的还是常规。
参考:
easyexcel源码地址
POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】
阿里开源Easy-Excel单元格样式调整
EasyExcel最新版本 使用感悟
使用poi解决导出excel内下拉框枚举项较多的问题
注解实现阿里Easy Excel自定义样式设置
java POI生成excel设置列格式并写入文件 SpringBoot项目
easyexcel 给单元格设置格式,通过自定义注解方式实现对每一列格式的精确控制
java使用poi自定义excel标题头并导出(springmvc+poi)
springboot+poi导出指定格式Excel模板详解+Demo
easyexcel 列宽、行高、样式
easyexcel注解