/** * 功能描述: 设置单元格样式 * @Param: [wb, cellIndex] * @Return: org.apache.poi.xssf.usermodel.XSSFCellStyle * @Author: shibaobei * @Date: 2020/8/14 0014 10:02 */ private XSSFCellStyle setBorderStyle(XSSFWorkbook wb,int cellIndex,String backWidth){ XSSFCellStyle cellStyle = wb.createCellStyle(); XSSFFont font = null; switch (cellIndex){ case 0: cellStyle.setBorderBottom(BorderStyle.DASHED); //下边框 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.DASHED);//上边框 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 break; case 1: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.MEDIUM_DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.MEDIUM_DASHED);//右边框 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置前景填充样式 cellStyle.setFillForegroundColor(HSSFColor.BLACK.index);//前景填充色 break; case 2: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 break; case 3: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 font = wb.createFont(); font.setFontName("宋体"); if("20".equals(backWidth)){ font.setFontHeightInPoints((short) 14);//设置字体大小 }else if("30".equals(backWidth)){ font.setFontHeightInPoints((short) 16);//设置字体大小 }else if("40".equals(backWidth)){ font.setFontHeightInPoints((short) 18);//设置字体大小 }else if("50".equals(backWidth)){ font.setFontHeightInPoints((short) 20);//设置字体大小 }else if("60".equals(backWidth)){ font.setFontHeightInPoints((short) 22);//设置字体大小 }else{ font.setFontHeightInPoints((short) 14);//设置字体大小 } cellStyle.setFont(font);//选择需要用到的字体格式 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 break; case 4: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 14);//设置字体大小 cellStyle.setFont(font);//选择需要用到的字体格式 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 cellStyle.setWrapText(true);//设置自动换行 break; case 5: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 font = wb.createFont(); font.setFontName("宋体"); if("20".equals(backWidth)){ font.setFontHeightInPoints((short) 12);//设置字体大小 }else if("30".equals(backWidth)){ font.setFontHeightInPoints((short) 14);//设置字体大小 }else if("40".equals(backWidth)){ font.setFontHeightInPoints((short) 16);//设置字体大小 }else if("50".equals(backWidth)){ font.setFontHeightInPoints((short) 18);//设置字体大小 }else if("60".equals(backWidth)){ font.setFontHeightInPoints((short) 20);//设置字体大小 }else { font.setFontHeightInPoints((short) 12);//设置字体大小 } cellStyle.setFont(font);//选择需要用到的字体格式 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 break; case 6: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 font = wb.createFont(); font.setFontName("宋体"); if(StringUtils.isBlank(backWidth) || "20".equals(backWidth)){ font.setFontHeightInPoints((short) 14);//设置字体大小 }else{ font.setFontHeightInPoints((short) 16);//设置字体大小 } cellStyle.setFont(font);//选择需要用到的字体格式 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 cellStyle.setWrapText(true);//设置自动换行 cellStyle.setRotation((short)255); //设置文字竖向排列 break; case 7: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 break; case 8: cellStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框 cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //下边框颜色 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); //上边框颜色 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置前景填充样式 cellStyle.setFillForegroundColor(HSSFColor.BLACK.index);//前景填充色 break; case 9: cellStyle.setBorderBottom(BorderStyle.DASHED); //下边框 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.DASHED);//上边框 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 break; default: cellStyle.setBorderBottom(BorderStyle.DASHED); //下边框 cellStyle.setBorderLeft(BorderStyle.DASHED);//左边框 cellStyle.setBorderTop(BorderStyle.DASHED);//上边框 cellStyle.setBorderRight(BorderStyle.DASHED);//右边框 break; } return cellStyle; }