Forráskód Böngészése

poi工具类设置表头背景颜色

yurk 2 éve
szülő
commit
2a8c53b1be

+ 58 - 10
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/util/ExcelUtil.java

@@ -7,6 +7,9 @@ import com.management.platform.service.CorpwxJobResultService;
 import com.management.platform.service.WxCorpInfoService;
 import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.RegionUtil;
+import org.apache.poi.xssf.streaming.SXSSFRow;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.poi.xssf.usermodel.*;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -61,7 +64,7 @@ public class ExcelUtil {
             font.setFontName("宋体");
 
             //设置单元格样式
-            CellStyle headStyle = workBook.createCellStyle();
+            XSSFCellStyle  headStyle = (XSSFCellStyle) workBook.createCellStyle();
             headStyle.setFont(headFont);
             headStyle.setAlignment(HorizontalAlignment.CENTER);
             headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
@@ -82,6 +85,15 @@ public class ExcelUtil {
             //这里的9是索引
 //            palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
 
+            //设置自定义颜色
+            XSSFColor xssfColor = new XSSFColor();
+            byte[] colorRgb = { (short)9, (byte) r, (byte) g, (byte) b };
+            xssfColor.setRGB(colorRgb);
+
+            headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
+            headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
+            /*headStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());*/ //设置自带的颜色
+
             CellStyle titleStyle = workBook.createCellStyle();
             titleStyle.setFont(titleFont);
             titleStyle.setAlignment(HorizontalAlignment.CENTER);
@@ -130,7 +142,7 @@ public class ExcelUtil {
                     for(int i = 0; i < rowList.size(); i++) {
                         Cell cell = row.createCell(i);
                         if(start == 0) {
-                            cell.setCellStyle(titleStyle);
+                            cell.setCellStyle(headStyle);
                         }else {
                             cell.setCellStyle(cellStyle);
                         }
@@ -198,7 +210,7 @@ public class ExcelUtil {
             font.setFontName("宋体");
 
             //设置单元格样式
-            CellStyle headStyle = workBook.createCellStyle();
+            XSSFCellStyle  headStyle = (XSSFCellStyle) workBook.createCellStyle();
             headStyle.setFont(headFont);
             headStyle.setAlignment(HorizontalAlignment.CENTER);
             headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
@@ -208,6 +220,25 @@ public class ExcelUtil {
             headStyle.setBorderTop(BorderStyle.THIN);//上边框
             headStyle.setBorderRight(BorderStyle.THIN);//右边框
 
+            String color = "c0c0c0";    //此处得到的color为16进制的字符串
+            //转为RGB码
+            int r = Integer.parseInt((color.substring(0,2)),16);   //转为16进制
+            int g = Integer.parseInt((color.substring(2,4)),16);
+            int b = Integer.parseInt((color.substring(4,6)),16);
+
+            //自定义cell颜色
+//            HSSFPalette palette = workBook.getCustomPalette();
+            //这里的9是索引
+//            palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
+
+            //设置自定义颜色
+            XSSFColor xssfColor = new XSSFColor();
+            byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
+            xssfColor.setRGB(colorRgb);
+            headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
+            headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
+            //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
+
 
             CellStyle titleStyle = workBook.createCellStyle();
             titleStyle.setFont(titleFont);
@@ -257,7 +288,7 @@ public class ExcelUtil {
                     for(int i = 0; i < rowList.size(); i++) {
                         Cell cell = row.createCell(i);
                         if(start == 0) {
-                            cell.setCellStyle(titleStyle);
+                            cell.setCellStyle(headStyle);
                         }else {
                             cell.setCellStyle(cellStyle);
                         }
@@ -319,7 +350,7 @@ public class ExcelUtil {
             font.setFontName("宋体");
 
             //设置单元格样式
-            CellStyle headStyle = workBook.createCellStyle();
+            XSSFCellStyle  headStyle = (XSSFCellStyle) workBook.createCellStyle();
             headStyle.setFont(headFont);
             headStyle.setAlignment(HorizontalAlignment.CENTER);
             headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
@@ -340,6 +371,15 @@ public class ExcelUtil {
             //这里的9是索引
 //            palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
 
+            //设置自定义颜色
+            XSSFColor xssfColor = new XSSFColor();
+            byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
+            xssfColor.setRGB(colorRgb);
+            headStyle.setFillForegroundColor(xssfColor);
+            headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
+            headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
+            //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
+
             CellStyle titleStyle = workBook.createCellStyle();
             titleStyle.setFont(titleFont);
             titleStyle.setAlignment(HorizontalAlignment.CENTER);
@@ -387,7 +427,7 @@ public class ExcelUtil {
                     for(int i = 0; i < rowList.size(); i++) {
                         Cell cell = row.createCell(i);
                         if(start == 0) {
-                            cell.setCellStyle(titleStyle);
+                            cell.setCellStyle(headStyle);
                         }else {
                             cell.setCellStyle(cellStyle);
                         }
@@ -457,7 +497,7 @@ public class ExcelUtil {
                 font.setFontName("宋体");
 
                 //设置单元格样式
-                CellStyle headStyle = workBook.createCellStyle();
+                XSSFCellStyle  headStyle = (XSSFCellStyle) workBook.createCellStyle();
                 headStyle.setFont(headFont);
                 headStyle.setAlignment(HorizontalAlignment.CENTER);
                 headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
@@ -474,9 +514,17 @@ public class ExcelUtil {
                 int b = Integer.parseInt((color.substring(4,6)),16);
 
                 //自定义cell颜色
-//                HSSFPalette palette = workBook.getCustomPalette();
+//            HSSFPalette palette = workBook.getCustomPalette();
                 //这里的9是索引
-//                palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
+//            palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
+
+                //设置自定义颜色
+                XSSFColor xssfColor = new XSSFColor();
+                byte[] colorRgb = { (byte)118, (byte)147, (byte)60 };
+                xssfColor.setRGB(colorRgb);
+                headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
+                headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 填充模式(和背景颜色成对使用)
+                //cs.setFillForegroundColor(IndexedColors.AQUA.getIndex()); //设置自带的颜色
 
                 CellStyle titleStyle = workBook.createCellStyle();
                 titleStyle.setFont(titleFont);
@@ -508,7 +556,7 @@ public class ExcelUtil {
                         for(int i = 0; i < rowList.size(); i++) {
                             Cell cell = row.createCell(i);
                             if(start == 0) {
-                                cell.setCellStyle(titleStyle);
+                                cell.setCellStyle(headStyle);
                             }else {
                                 cell.setCellStyle(cellStyle);
                             }