【无标题】

//创建HSSFWorkbook对象(excel的文档对象)
 HSSFWorkbook wb = new HSSFWorkbook();
 
 Font font = wb.createFont();
 //字体大小
 font.setFontHeightInPoints((short) 11);
 //字体颜色
 font.setColor(HSSFColor.WHITE.index);
 //字体
font.setFontName("等线");
//粗体显示
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 
//HSSFCellStyle不能设置数字类型
HSSFCellStyle style = wb.createCellStyle();
//背景色两个一起才有效果
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//水平居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//上下左右框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//字体样式
style.setFont(font);
 
//CellStyle可以设置数字类型
CellStyle cellStyle = wb.createCellStyle();
HSSFDataFormat df = wb.createDataFormat();  //此处设置数据格式
//小数格式
cellStyle.setDataFormat(df.getFormat("#,#0.00"));
//百分号格式
cellStyle1.setDataFormat(df1.getFormat("0.00%"));
//字体样式
cellStyle.setFont(font);
 
HSSFSheet sheet = wb.createSheet("新建sheet");

//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);

//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
 //表头定义数组省事,修改方便,效率高
 String[] orgNames = new String[]{"券商统计", "类型", "研究分", "固收", "服务", "合计", "调整后", "占比", "名次"};
 for (int j = 0; j < orgNames.length; j++) {
     Cell cell = row1.createCell(j);
     //单元格赋值
    cell.setCellValue(orgNames[j]);
    //单元格样式
    cell.setCellStyle(style);
}
  
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,10));


颜色与代码参考:
 

java使excel字体变红 java中excel设置字体颜色_SOLID

 

上面的单元格颜色对应下面的英语颜色表示,从X1-X49 按顺序对应;

将下面对应的code填入上述代码加粗斜体位置即可。

登录后复制 
IndexedColors.AQUA.getIndex() //1
IndexedColors.AUTOMATIC.getIndex() //2
IndexedColors.BLUE.getIndex() //3
IndexedColors.BLUE_GREY.getIndex() //4
IndexedColors.BRIGHT_GREEN.getIndex() //5
IndexedColors.BROWN.getIndex() //6
IndexedColors.CORAL.getIndex() //7
IndexedColors.CORNFLOWER_BLUE.getIndex() //8
IndexedColors.DARK_BLUE.getIndex() //9
IndexedColors.DARK_GREEN.getIndex() //10
IndexedColors.DARK_RED.getIndex() //11
IndexedColors.DARK_TEAL.getIndex() //12
IndexedColors.DARK_YELLOW.getIndex() //13
IndexedColors.GOLD.getIndex() //14
IndexedColors.GREEN.getIndex() //15
IndexedColors.GREY_25_PERCENT.getIndex() //16
IndexedColors.GREY_40_PERCENT.getIndex() //17
IndexedColors.GREY_50_PERCENT.getIndex() //18
IndexedColors.GREY_80_PERCENT.getIndex() //19
IndexedColors.INDIGO.getIndex() //20
IndexedColors.LAVENDER.getIndex() //21
IndexedColors.LEMON_CHIFFON.getIndex() //22
IndexedColors.LIGHT_BLUE.getIndex() //23
IndexedColors.LEMON_CHIFFON.getIndex() //24
IndexedColors.LIGHT_BLUE.getIndex() //25
IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex() //26
IndexedColors.LIGHT_GREEN.getIndex() //27
IndexedColors.LIGHT_ORANGE.getIndex() //28
IndexedColors.LIGHT_TURQUOISE.getIndex() //29
IndexedColors.LIGHT_YELLOW.getIndex() //30
IndexedColors.LIME.getIndex() //31
IndexedColors.MAROON.getIndex() //32
IndexedColors.OLIVE_GREEN.getIndex() //33
IndexedColors.ORANGE.getIndex() //34
IndexedColors.ORCHID.getIndex() //35
IndexedColors.PALE_BLUE.getIndex() //36
IndexedColors.PINK.getIndex() //37
IndexedColors.PLUM.getIndex() //38
IndexedColors.RED.getIndex() //39
IndexedColors.ROSE.getIndex() //40
IndexedColors.ROYAL_BLUE.getIndex() //41
IndexedColors.SEA_GREEN.getIndex() //42
IndexedColors.SKY_BLUE.getIndex() //43
IndexedColors.TAN.getIndex() //44
IndexedColors.TEAL.getIndex() //45
IndexedColors.TURQUOISE.getIndex() //46
IndexedColors.VIOLET.getIndex() //47
IndexedColors.WHITE.getIndex() //48
IndexedColors.YELLOW.getIndex() //49
-----------------------------------
java使excel字体变红 java中excel设置字体颜色

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值