https://www.cnblogs.com/yanjie-java/p/8329631.html
1 package com.java.connect.poi; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 import org.apache.poi.ss.usermodel.Cell; 7 import org.apache.poi.ss.usermodel.CellStyle; 8 import org.apache.poi.ss.usermodel.IndexedColors; 9 import org.apache.poi.ss.usermodel.Row; 10 import org.apache.poi.ss.usermodel.Sheet; 11 import org.apache.poi.ss.usermodel.Workbook; 12 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 13 14 public class POIFillAndColorExample { 15 public static void main(String[] args) throws IOException { 16 // Create a workbook object 17 Workbook workbook = new XSSFWorkbook(); 18 // Create sheet 19 Sheet sheet = workbook.createSheet(); 20 21 // Create a row and put some cells in it. 22 Row row = sheet.createRow((short) 1); 23 24 // Aqua background 25 CellStyle style = workbook.createCellStyle(); 26 style.setFillForegroundColor(IndexedColors.AQUA.getIndex()); 27 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 28 Cell cell = row.createCell((short) 1); 29 cell.setCellValue("X1"); 30 cell.setCellStyle(style); 31 32 // Orange "foreground", foreground being the fill foreground not the 33 // font color. 34 style = workbook.createCellStyle(); 35 style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex()); 36 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 37 cell = row.createCell((short) 2); 38 cell.setCellValue("X2"); 39 cell.setCellStyle(style); 40 41 style = workbook.createCellStyle(); 42 style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); 43 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 44 cell = row.createCell((short) 3); 45 cell.setCellValue("X3"); 46 cell.setCellStyle(style); 47 48 style = workbook.createCellStyle(); 49 style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex()); 50 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 51 cell = row.createCell((short) 4); 52 cell.setCellValue("X4"); 53 cell.setCellStyle(style); 54 55 style = workbook.createCellStyle(); 56 style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex()); 57 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 58 cell = row.createCell((short) 5); 59 cell.setCellValue("X5"); 60 cell.setCellStyle(style); 61 62 // Create a row and put some cells in it. 63 Row row2 = sheet.createRow((short) 2); 64 65 style = workbook.createCellStyle(); 66 style.setFillForegroundColor(IndexedColors.BROWN.getIndex()); 67 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 68 cell = row2.createCell((short) 1); 69 cell.setCellValue("X6"); 70 cell.setCellStyle(style); 71 72 style = workbook.createCellStyle(); 73 style.setFillForegroundColor(IndexedColors.CORAL.getIndex()); 74 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 75 cell = row2.createCell((short) 2); 76 cell.setCellValue("X7"); 77 cell.setCellStyle(style); 78 79 style = workbook.createCellStyle(); 80 style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex()); 81 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 82 cell = row2.createCell((short) 3); 83 cell.setCellValue("X8"); 84 cell.setCellStyle(style); 85 86 style = workbook.createCellStyle(); 87 style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); 88 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 89 cell = row2.createCell((short) 4); 90 cell.setCellValue("X9"); 91 cell.setCellStyle(style); 92 style = workbook.createCellStyle(); 93 style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex()); 94 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 95 cell = row2.createCell((short) 5); 96 cell.setCellValue("X10"); 97 cell.setCellStyle(style); 98 99 // Create a row and put some cells in it. 100 Row row3 = sheet.createRow((short) 3); 101 102 style = workbook.createCellStyle(); 103 style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex()); 104 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 105 cell = row3.createCell((short) 1); 106 cell.setCellValue("X11"); 107 cell.setCellStyle(style); 108 style = workbook.createCellStyle(); 109 style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex()); 110 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 111 cell = row3.createCell((short) 2); 112 cell.setCellValue("X12"); 113 cell.setCellStyle(style); 114 115 style = workbook.createCellStyle(); 116 style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex()); 117 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 118 cell = row3.createCell((short) 3); 119 cell.setCellValue("X13"); 120 cell.setCellStyle(style); 121 style = workbook.createCellStyle(); 122 style.setFillForegroundColor(IndexedColors.GOLD.getIndex()); 123 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 124 cell = row3.createCell((short) 4); 125 cell.setCellValue("X14"); 126 cell.setCellStyle(style); 127 128 style = workbook.createCellStyle(); 129 style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); 130 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 131 cell = row3.createCell((short) 5); 132 cell.setCellValue("X15"); 133 cell.setCellStyle(style); 134 135 // Create a row and put some cells in it. 136 Row row4 = sheet.createRow((short) 4); 137 style = workbook.createCellStyle(); 138 style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); 139 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 140 cell = row4.createCell((short) 1); 141 cell.setCellValue("X16"); 142 cell.setCellStyle(style); 143 144 style = workbook.createCellStyle(); 145 style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); 146 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 147 cell = row4.createCell((short) 2); 148 cell.setCellValue("X17"); 149 cell.setCellStyle(style); 150 style = workbook.createCellStyle(); 151 style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); 152 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 153 cell = row4.createCell((short) 3); 154 cell.setCellValue("X18"); 155 cell.setCellStyle(style); 156 157 style = workbook.createCellStyle(); 158 style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex()); 159 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 160 cell = row4.createCell((short) 4); 161 cell.setCellValue("X19"); 162 cell.setCellStyle(style); 163 style = workbook.createCellStyle(); 164 style.setFillForegroundColor(IndexedColors.INDIGO.getIndex()); 165 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 166 cell = row4.createCell((short) 5); 167 cell.setCellValue("X20"); 168 cell.setCellStyle(style); 169 170 // Create a row and put some cells in it. 171 Row row5 = sheet.createRow((short) 5); 172 173 style = workbook.createCellStyle(); 174 style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex()); 175 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 176 cell = row5.createCell((short) 1); 177 cell.setCellValue("X21"); 178 cell.setCellStyle(style); 179 180 style = workbook.createCellStyle(); 181 style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex()); 182 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 183 cell = row5.createCell((short) 2); 184 cell.setCellValue("X22"); 185 cell.setCellStyle(style); 186 187 style = workbook.createCellStyle(); 188 style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); 189 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 190 cell = row5.createCell((short) 3); 191 cell.setCellValue("X23"); 192 cell.setCellStyle(style); 193 style = workbook.createCellStyle(); 194 style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex()); 195 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 196 cell = row5.createCell((short) 4); 197 cell.setCellValue("X24"); 198 cell.setCellStyle(style); 199 200 style = workbook.createCellStyle(); 201 style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); 202 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 203 cell = row5.createCell((short) 5); 204 cell.setCellValue("X25"); 205 cell.setCellStyle(style); 206 207 // Create a row and put some cells in it. 208 Row row6 = sheet.createRow((short) 6); 209 style = workbook.createCellStyle(); 210 style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE 211 .getIndex()); 212 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 213 cell = row6.createCell((short) 1); 214 cell.setCellValue("X26"); 215 cell.setCellStyle(style); 216 217 style = workbook.createCellStyle(); 218 style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); 219 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 220 cell = row6.createCell((short) 2); 221 cell.setCellValue("X27"); 222 cell.setCellStyle(style); 223 style = workbook.createCellStyle(); 224 style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); 225 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 226 cell = row6.createCell((short) 3); 227 cell.setCellValue("X28"); 228 cell.setCellStyle(style); 229 230 style = workbook.createCellStyle(); 231 style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); 232 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 233 cell = row6.createCell((short) 4); 234 cell.setCellValue("X29"); 235 cell.setCellStyle(style); 236 237 style = workbook.createCellStyle(); 238 style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); 239 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 240 cell = row6.createCell((short) 5); 241 cell.setCellValue("X30"); 242 cell.setCellStyle(style); 243 244 // Create a row and put some cells in it. 245 Row row7 = sheet.createRow((short) 7); 246 style = workbook.createCellStyle(); 247 style.setFillForegroundColor(IndexedColors.LIME.getIndex()); 248 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 249 cell = row7.createCell((short) 1); 250 cell.setCellValue("X31"); 251 cell.setCellStyle(style); 252 style = workbook.createCellStyle(); 253 style.setFillForegroundColor(IndexedColors.MAROON.getIndex()); 254 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 255 cell = row7.createCell((short) 2); 256 cell.setCellValue("X32"); 257 cell.setCellStyle(style); 258 259 style = workbook.createCellStyle(); 260 style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex()); 261 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 262 cell = row7.createCell((short) 3); 263 cell.setCellValue("X33"); 264 cell.setCellStyle(style); 265 style = workbook.createCellStyle(); 266 style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); 267 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 268 cell = row7.createCell((short) 4); 269 cell.setCellValue("X34"); 270 cell.setCellStyle(style); 271 272 style = workbook.createCellStyle(); 273 style.setFillForegroundColor(IndexedColors.ORCHID.getIndex()); 274 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 275 cell = row7.createCell((short) 5); 276 cell.setCellValue("X35"); 277 cell.setCellStyle(style); 278 279 // Create a row and put some cells in it. 280 Row row8 = sheet.createRow((short) 8); 281 282 style = workbook.createCellStyle(); 283 style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex()); 284 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 285 cell = row8.createCell((short) 1); 286 cell.setCellValue("X36"); 287 cell.setCellStyle(style); 288 289 style = workbook.createCellStyle(); 290 style.setFillForegroundColor(IndexedColors.PINK.getIndex()); 291 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 292 cell = row8.createCell((short) 2); 293 cell.setCellValue("X37"); 294 cell.setCellStyle(style); 295 style = workbook.createCellStyle(); 296 style.setFillForegroundColor(IndexedColors.PLUM.getIndex()); 297 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 298 cell = row8.createCell((short) 3); 299 cell.setCellValue("X38"); 300 cell.setCellStyle(style); 301 302 style = workbook.createCellStyle(); 303 style.setFillForegroundColor(IndexedColors.RED.getIndex()); 304 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 305 cell = row8.createCell((short) 4); 306 cell.setCellValue("X39"); 307 cell.setCellStyle(style); 308 style = workbook.createCellStyle(); 309 style.setFillForegroundColor(IndexedColors.ROSE.getIndex()); 310 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 311 cell = row8.createCell((short) 5); 312 cell.setCellValue("X40"); 313 cell.setCellStyle(style); 314 315 // Create a row and put some cells in it. 316 Row row9 = sheet.createRow((short) 9); 317 318 style = workbook.createCellStyle(); 319 style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex()); 320 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 321 cell = row9.createCell((short) 1); 322 cell.setCellValue("X41"); 323 cell.setCellStyle(style); 324 style = workbook.createCellStyle(); 325 style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex()); 326 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 327 cell = row9.createCell((short) 2); 328 cell.setCellValue("X42"); 329 cell.setCellStyle(style); 330 331 style = workbook.createCellStyle(); 332 style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex()); 333 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 334 cell = row9.createCell((short) 3); 335 cell.setCellValue("X43"); 336 cell.setCellStyle(style); 337 style = workbook.createCellStyle(); 338 style.setFillForegroundColor(IndexedColors.TAN.getIndex()); 339 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 340 cell = row9.createCell((short) 4); 341 cell.setCellValue("X44"); 342 cell.setCellStyle(style); 343 344 style = workbook.createCellStyle(); 345 style.setFillForegroundColor(IndexedColors.TEAL.getIndex()); 346 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 347 cell = row9.createCell((short) 5); 348 cell.setCellValue("X45"); 349 cell.setCellStyle(style); 350 351 // Create a row and put some cells in it. 352 Row row10 = sheet.createRow((short) 10); 353 354 style = workbook.createCellStyle(); 355 style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex()); 356 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 357 cell = row10.createCell((short) 1); 358 cell.setCellValue("X46"); 359 cell.setCellStyle(style); 360 361 style = workbook.createCellStyle(); 362 style.setFillForegroundColor(IndexedColors.VIOLET.getIndex()); 363 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 364 cell = row10.createCell((short) 2); 365 cell.setCellValue("X47"); 366 cell.setCellStyle(style); 367 style = workbook.createCellStyle(); 368 style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); 369 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 370 cell = row10.createCell((short) 3); 371 cell.setCellValue("X48"); 372 cell.setCellStyle(style); 373 374 style = workbook.createCellStyle(); 375 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); 376 style.setFillPattern(CellStyle.SOLID_FOREGROUND); 377 cell = row10.createCell((short) 3); 378 cell.setCellValue("X49"); 379 cell.setCellStyle(style); 380 381 // Write the output to a file 382 FileOutputStream fileOut = new FileOutputStream( 383 "POIFillAndColorExample.xlsx"); 384 workbook.write(fileOut); 385 fileOut.close(); 386 387 } 388 }
生成的excel文件看起来像下面的图片。
color | org.apache.poi.hssf.util.HSSFColor$GREY_80_PERCENT@1aadbf7 |
color | org.apache.poi.hssf.util.HSSFColor$INDIGO@4f4458 |
color | org.apache.poi.hssf.util.HSSFColor$PLUM@100c56 |
color | org.apache.poi.hssf.util.HSSFColor$BROWN@19a1fae |
color | org.apache.poi.hssf.util.HSSFColor$OLIVE_GREEN@1960c9e |
color | org.apache.poi.hssf.util.HSSFColor$DARK_GREEN@168f39 |
color | org.apache.poi.hssf.util.HSSFColor$SEA_GREEN@11525cd |
color | org.apache.poi.hssf.util.HSSFColor$DARK_TEAL@164e67f |
color | org.apache.poi.hssf.util.HSSFColor$GREY_40_PERCENT@158cfda |
color | org.apache.poi.hssf.util.HSSFColor$BLUE_GREY@1b613c0 |
color | org.apache.poi.hssf.util.HSSFColor$ORANGE@cae1b3 |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_ORANGE@1a7e798 |
color | org.apache.poi.hssf.util.HSSFColor$GOLD@55d1ef |
color | org.apache.poi.hssf.util.HSSFColor$LIME@49b9ad |
color | org.apache.poi.hssf.util.HSSFColor$AQUA@3d0f0e |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_BLUE@a7624e |
color | org.apache.poi.hssf.util.HSSFColor$TAN@1271218 |
color | org.apache.poi.hssf.util.HSSFColor$LAVENDER@150b2d |
color | org.apache.poi.hssf.util.HSSFColor$ROSE@190c9dc |
color | org.apache.poi.hssf.util.HSSFColor$PALE_BLUE@b4bc8e |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_YELLOW@1c74456 |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_GREEN@15783a2 |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_TURQUOISE@830993 |
color | org.apache.poi.hssf.util.HSSFColor$SKY_BLUE@e99642 |
color | org.apache.poi.hssf.util.HSSFColor$BLUE@187f194 |
color | org.apache.poi.hssf.util.HSSFColor$TEAL@55f9f |
color | org.apache.poi.hssf.util.HSSFColor$DARK_RED@c8e08f |
color | org.apache.poi.hssf.util.HSSFColor$VIOLET@edd19 |
color | org.apache.poi.hssf.util.HSSFColor$TURQUOISE@1d5c7a1 |
color | org.apache.poi.hssf.util.HSSFColor$YELLOW@252119 |
color | org.apache.poi.hssf.util.HSSFColor$PINK@19ff062 |
color | org.apache.poi.hssf.util.HSSFColor$DARK_BLUE@15eb4d0 |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_CORNFLOWER_BLUE@b0e84b |
color | org.apache.poi.hssf.util.HSSFColor$ROYAL_BLUE@62a19d |
color | org.apache.poi.hssf.util.HSSFColor$CORAL@16075b3 |
color | org.apache.poi.hssf.util.HSSFColor$ORCHID@1cf46c2 |
color | org.apache.poi.hssf.util.HSSFColor$LIGHT_TURQUOISE@12e8a41 |
color | org.apache.poi.hssf.util.HSSFColor$LEMON_CHIFFON@76ec7a |
color | org.apache.poi.hssf.util.HSSFColor$PLUM@19f7952 |
color | org.apache.poi.hssf.util.HSSFColor$CORNFLOWER_BLUE@d60cdd |
color | org.apache.poi.hssf.util.HSSFColor$GREY_50_PERCENT@e6bc11 |
color | org.apache.poi.hssf.util.HSSFColor$GREY_25_PERCENT@452267 |
color | org.apache.poi.hssf.util.HSSFColor$TEAL@d5b614 |
color |
org.apache.poi.hssf.util.HSSFColor$VIOLET@a4e3ae |
color | org.apache.poi.hssf.util.HSSFColor$DARK_YELLOW@15fd4a3 |
color | org.apache.poi.hssf.util.HSSFColor$DARK_BLUE@8137c9 |
color | org.apache.poi.hssf.util.HSSFColor$GREEN@1757596 |
color | org.apache.poi.hssf.util.HSSFColor$DARK_RED@7ad4d5 |
color | org.apache.poi.hssf.util.HSSFColor$TURQUOISE@2aee3f |
color | org.apache.poi.hssf.util.HSSFColor$PINK@7f788b |
color | org.apache.poi.hssf.util.HSSFColor$YELLOW@c311d5 |
color | org.apache.poi.hssf.util.HSSFColor$BLUE@c7e580 |
color | org.apache.poi.hssf.util.HSSFColor$BRIGHT_GREEN@1ac55ac |
color | org.apache.poi.hssf.util.HSSFColor$RED@12cc460 |
color | org.apache.poi.hssf.util.HSSFColor$WHITE@10ad7e |
color |
org.apache.poi.hssf.util.HSSFColor$BLACK@ee336f |