您的位置:首页 > 其它

POI颜色索引

2013-09-02 10:04 309 查看
POI颜色大全

POI颜色大全.pdf

网络上面找到的资料都是 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();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: