JAVA excel 常用方法

 1 //创建HSSFWorkbook对象(excel的文档对象)
 2 HSSFWorkbook wb = new HSSFWorkbook();
 3 
 4 Font font = wb.createFont();
 5 //字体大小
 6 font.setFontHeightInPoints((short) 11);
 7 //字体颜色
 8 font.setColor(HSSFColor.WHITE.index);
 9 //字体
10 font.setFontName("等线");
11 //粗体显示
12 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
13  
14 //HSSFCellStyle不能设置数字类型
15 HSSFCellStyle style = wb.createCellStyle();
16 //背景色两个一起才有效果
17 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
18 style.setFillPattern(CellStyle.SOLID_FOREGROUND);
19 //水平居中
20 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
21 //垂直居中
22 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
23 //上下左右框
24 style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
25 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
26 style.setBorderRight(HSSFCellStyle.BORDER_THIN);
27 style.setBorderTop(HSSFCellStyle.BORDER_THIN);
28 //字体样式
29 style.setFont(font);
30  
31 //CellStyle可以设置数字类型
32 CellStyle cellStyle = wb.createCellStyle();
33 HSSFDataFormat df = wb.createDataFormat();  //此处设置数据格式
34 //小数格式
35 cellStyle.setDataFormat(df.getFormat("#,#0.00"));
36 //百分号格式
37 cellStyle1.setDataFormat(df1.getFormat("0.00%"));
38 //字体样式
39 cellStyle.setFont(font);
40  
41 HSSFSheet sheet = wb.createSheet("新建sheet");
42  
43 //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
44 HSSFRow row1 = sheet.createRow(0);
45  
46 //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
47 //表头定义数组省事,修改方便,效率高
48 String[] orgNames = new String[]{"券商统计", "类型", "研究分", "固收", "服务", "合计", "调整后", "占比", "名次"};
49 for (int j = 0; j < orgNames.length; j++) {
50     Cell cell = row1.createCell(j);
51     //单元格赋值
52     cell.setCellValue(orgNames[j]);
53     //单元格样式
54     cell.setCellStyle(style);
55 }
56  
57 //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
58 sheet.addMergedRegion(new CellRangeAddress(0,0,0,10));
颜色与代码参考:

 

 

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

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

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

 

posted @ 2022-08-02 09:16  酷盖的小机灵  阅读(156)  评论(0编辑  收藏  举报