easyExcel导出excel时指定列样式
代码如下:
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_)
easyExcel导出excel时指定列样式相关推荐
最新文章
热门文章
|