由于要利用poi导出excel(XSSFWorkbook),而且要添加样式,搜索其他的结果无非都是颜色值,经查询的结果,做一下总结:
1、设置背景色,要用 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
使用 style.setFillBackgroundColor(bg);方法总是出现一个黑块,所以改为上面的写法,结果正确
颜色的问题,可以在 IndexedColors 中查到,是个枚举
2、有合并单元格的情况下,给一行直接设置背景颜色,并没有起到效果,所以在 createCell 时,包装了一个方法
1 /**
2 * 创建cell 此方法目的,是要给所有的cell加上边框和背景色3 *@paramworkbook 工作薄4 *@paramrow 行5 *@paramindex 下标6 *@return
7 */
8 public XSSFCell createCell(XSSFWorkbook workbook,XSSFRow row,intindex,String ... args) {9 CellStyle style=SheetStyle.getDefaultCellStyle(workbook);10 XSSFCell cell =row.createCell(index);11
12 //大于3,因为前边几行不需要样式
13 if(row.getRowNum()>3) {14 //偶数行为Lime,奇数行为Yellow
15 if(this.staffIndex%2==0) {16 SheetStyle.setCellStyleLime(style);17 }else{18 SheetStyle.setCellStyleYellow(style);19 }20 }21 row.setRowStyle(style);22 cell.setCellStyle(style);23 returncell;24 }
把样式同时赋值给RowStyle和CellStyle,导出的效果理想
3、需要有合并单元格的情况,必须先创建合并单元格,然后在填值
CellRangeAddress callRangeAddress5 = new CellRangeAddress(start,end,0,0);//起始行,结束行,起始列,结束列
sheet.addMergedRegion(callRangeAddress5);
4、spring mvc导出excel文件,Controller部分的代码:
主要用来设置导出的文件名
/*** excel排班
*@paramrequest
*@paramresponse
*@return*@throwsIOException*/@SuppressWarnings("resource")
@RequestMapping(value= "exportExcel")public void exportExcel(HttpServletRequest request, HttpServletResponse response) throwsIOException {
Map result=schedulingService.downloadScheduling(request.getParameter("x"));
String name="导出结果"+result.get("month");byte [] bytes=(byte[]) result.get("data");
response.setContentType("application/binary;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xlsx", "UTF-8"));
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
response.getOutputStream().close();
}
将SheetStyle完整代码贴出,有需要的可以拿来复制
importorg.apache.poi.ss.usermodel.BorderStyle;importorg.apache.poi.ss.usermodel.CellStyle;importorg.apache.poi.ss.usermodel.FillPatternType;importorg.apache.poi.ss.usermodel.Font;importorg.apache.poi.ss.usermodel.HorizontalAlignment;importorg.apache.poi.ss.usermodel.IndexedColors;importorg.apache.poi.ss.usermodel.VerticalAlignment;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;/*** 设置excel的样式类
**/
public classSheetStyle {public static voidsetColumnWidth() {
}/*** 水平居中、垂直居中
* 字体:宋体
* 字体大小:16号
* 加粗
*@paramworkbook
*@return
*/
public staticCellStyle getStyle(XSSFWorkbook workbook) {
CellStyle cellstyle=workbook.createCellStyle();
cellstyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Font font=workbook.createFont();//字体
font.setFontName("宋体");//字体
font.setFontHeightInPoints((short)16);//字号
font.setBold(true);//加粗
cellstyle.setFont(font);
setBorderStyle(cellstyle);returncellstyle;
}/*** 获取默认的cell表格样式,加边框,水平居中,垂直居中
*@paramworkbook
*@return
*/
public staticCellStyle getDefaultCellStyle(XSSFWorkbook workbook) {
CellStyle style=workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
setBorderStyle(style);returnstyle;
}/*** 边框样式
*@paramstyle*/
public static voidsetBorderStyle(CellStyle style) {
style.setBorderBottom(BorderStyle.THIN);//下边框
style.setBorderLeft(BorderStyle.THIN);//左边框
style.setBorderTop(BorderStyle.THIN);//上边框
style.setBorderRight(BorderStyle.THIN);//右边框
}/*** 奇数行
* 背景颜色为黄色
*@paramstyle*/
public static voidsetCellStyleYellow(CellStyle style) {
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}/*** 偶数行
* 背景颜色为LIME
*@paramstyle*/
public static voidsetCellStyleLime(CellStyle style) {
style.setFillForegroundColor(IndexedColors.LIME.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}/*** 字体设置红色
*@paramworkbook
*@paramstyle*/
public static voidsetFontRedColor(XSSFWorkbook workbook,CellStyle style) {
Font font=workbook.createFont();//字体
font.setColor(IndexedColors.RED.getIndex());
style.setFont(font);
}
}