POI的一些配置

POI的一些配置

 1 引用:http://apps.hi.baidu.com/share/detail/17249059
 2 
 3 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结:
 4 
 5 先获取工作薄对象:
 6 
 7 HSSFWorkbook wb = new HSSFWorkbook();
 8 
 9 HSSFSheet sheet = wb.createSheet();
10 
11 HSSFCellStyle setBorder = wb.createCellStyle();
12 
13 一、设置背景色:
14 
15 setBorder.setFillForegroundColor((short) 13);// 设置背景色
16 setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
17 
18 二、设置边框:
19 
20 setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
21 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
22 setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
23 setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
24 
25 三、设置居中:
26 
27 setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
28 
29 四、设置字体:
30 
31 HSSFFont font = wb.createFont();
32 font.setFontName("黑体");
33 font.setFontHeightInPoints((short) 16);//设置字体大小
34 
35 HSSFFont font2 = wb.createFont();
36 font2.setFontName("仿宋_GB2312");
37 font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
38 font2.setFontHeightInPoints((short) 12);
39 
40 setBorder.setFont(font);//选择需要用到的字体格式
41 
42 五、设置列宽:
43 
44 sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值  参考 :"2012-08-10"的宽度为2500
45 
46 六、设置自动换行:
47 
48 setBorder.setWrapText(true);//设置自动换行
49 
50 七、合并单元格:
51 
52 Region region1 = new Region(0, (short) 0, 0, (short) 6);
53 
54 //参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
55 
56 或者用
57 
58 CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
59 
60 但应注意两个构造方法的参数不是一样的,具体使用哪个取决于POI的不同版本。
61 sheet.addMergedRegion(region1);
62 
63 目前用过的就这么多,后续有新的会继续添加。





  CellStyle cellStyle = wb.createCellStyle();
  cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
  cellStyle.setBorderRight(CellStyle.BORDER_THIN);
  cellStyle.setBorderTop(CellStyle.BORDER_THIN);
  cellStyle.setAlignment(CellStyle.ALIGN_CENTER);

原文地址:https://www.cnblogs.com/gym2017/p/7297534.html