poi生成excel整理(设置边框/字体/颜色/加粗/居中/)

 

HSSFWorkbook wb = new HSSFWorkbook();html

HSSFSheet sheet = wb.createSheet();post

HSSFCellStyle setBorder = wb.createCellStyle();字体

1、设置背景色:spa

setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);htm

2、设置边框:blog

setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框it

3、设置居中:io

setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中class

4、设置字体:float

HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小

HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font2.setFontHeightInPoints((short) 12);

setBorder.setFont(font);//选择须要用到的字体格式

5、设置列宽:

sheet.setColumnWidth(0, 3766); //第一个参数表明列id(从0开始),第2个参数表明宽度值

6、设置自动换行:

setBorder.setWrapText(true);//设置自动换行

7、合并单元格:

Region region1 = new Region(0, (short) 0, 0, (short) 6);

//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
sheet.addMergedRegion(region1);

或者用

CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);

但应注意两个构造方法的参数不是同样的,具体使用哪一个取决于POI的不一样版本。
sheet.addMergedRegion(region1);

目前用过的就这么多,后续有新的会继续添加。

8、加边框

  HSSFCellStyle cellStyle= wookBook.createCellStyle();  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  cellStyle.setBorderBottom(HSSFCellStyle.BorderBORDER_MEDIUM);  cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);  cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);  cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);  cellStyle.setRightBorderColor(HSSFColor.BLACK.index);  cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);  cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

相关文章
相关标签/搜索