POI顏色大全
網絡上面找到的資料都是 HSSFColor中顏色索引
現在大家基本都用XSSF了吧。即07之后的 xlsx 格式的excel
設置顏色的基本方法
| thStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); //設置前景填充樣式 thStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());//設置顏色 |
POI 顏色 是一個short值。你可以直接設置數字,可是這樣你或之后的人並不知道這個數字代表的顏色是什么。所以這里把常用的顏色放到IndexedColors中。
IndexColors提供了(64-8)56種顏色,真虧他起了56個名字。。。
這里用
for (IndexedColors c : IndexedColors.values())
即可遍歷全部顏色
將顏色寫入到excel。
第一個數值表示這個顏色的short值
第二個名字表示 顏色名 IndexedColors.顏色名.getIndex() ,可這樣調用獲取顏色代號
第三個顏色的顏色。
| 008 |
BLACK |
|
009 |
WHITE |
|
| 010 |
RED |
|
011 |
BRIGHT_GREEN |
|
| 012 |
BLUE |
|
013 |
YELLOW |
|
| 014 |
PINK |
|
015 |
TURQUOISE |
|
| 016 |
DARK_RED |
|
017 |
GREEN |
|
| 018 |
DARK_BLUE |
|
019 |
DARK_YELLOW |
|
| 020 |
VIOLET |
|
021 |
TEAL |
|
| 022 |
GREY_25_PERCENT |
|
023 |
GREY_50_PERCENT |
|
| 024 |
CORNFLOWER_BLUE |
|
025 |
MAROON |
|
| 026 |
LEMON_CHIFFON |
|
028 |
ORCHID |
|
| 029 |
CORAL |
|
030 |
ROYAL_BLUE |
|
| 031 |
LIGHT_CORNFLOWER_BLUE |
|
040 |
SKY_BLUE |
|
| 041 |
LIGHT_TURQUOISE |
|
042 |
LIGHT_GREEN |
|
| 043 |
LIGHT_YELLOW |
|
044 |
PALE_BLUE |
|
| 045 |
ROSE |
|
046 |
LAVENDER |
|
| 047 |
TAN |
|
048 |
LIGHT_BLUE |
|
| 049 |
AQUA |
|
050 |
LIME |
|
| 051 |
GOLD |
|
052 |
LIGHT_ORANGE |
|
| 053 |
ORANGE |
|
054 |
BLUE_GREY |
|
| 055 |
GREY_40_PERCENT |
|
056 |
DARK_TEAL |
|
| 057 |
SEA_GREEN |
|
058 |
DARK_GREEN |
|
| 059 |
OLIVE_GREEN |
|
060 |
BROWN |
|
| 061 |
PLUM |
|
062 |
INDIGO |
|
| 063 |
GREY_80_PERCENT |
|
064 |
AUTOMATIC |
|

附生成源代碼
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ColorTest {
public static void main(String[] args) {
short colorNum = 2;//每行顯示的顏色的個數
short width1 = 2000;//顏色序號寬度
short width = 6000;//顏色名欄位的寬度
XSSFWorkbook workbook = new XSSFWorkbook();
CellStyle thStyle = workbook.createCellStyle();
XSSFSheet sheet = workbook.createSheet("IndexedColors遍歷");
Row row =null;
Cell cell;
short i,j,index=0;
String text="";
for(i=0;i<colorNum;i++){
sheet.setColumnWidth((short) i*3 , width1 );
sheet.setColumnWidth((short) i*3 + 1 , width );
}
for (IndexedColors c : IndexedColors.values()){
i=(short) (index/colorNum);
j=(short) (index%colorNum);
thStyle = workbook.createCellStyle();
thStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); //設置前景填充樣式
thStyle.setFillForegroundColor(c.getIndex());
row = j==0?sheet.createRow(i):row;//如果到了換行的時候row new一下 否則就當什么都沒有發生
// text="";
// text += (index + 1000 + "").substring(1,4) +"||";
// text += (c.getIndex() + 1000 + "").substring(1,4) +"||";
// text += c;
row.createCell(3*j).setCellValue(" "+(c.getIndex() + 1000 + "").substring(1,4));
row.createCell(3*j+1).setCellValue(c+"");
row.createCell(3*j+2).setCellStyle(thStyle);
index++;//計數
}
sheet = workbook.createSheet("30X"+colorNum+"顏色遍歷");
for( i=0;i<30;i++ ){
row = sheet.createRow(i);
for(j=0;j<colorNum;j++){
thStyle = workbook.createCellStyle();
thStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); //設置前景填充樣式
thStyle.setFillForegroundColor((short)(colorNum*i + j));
row.createCell(2*j).setCellValue((short)(colorNum*i + j));
row.createCell(2*j+1).setCellStyle(thStyle);
}
}
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream("POI顏色大全.xlsx");
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
System.out.println("保存xlsx的時候發生的異常");
e.printStackTrace();
}
}
}
