生成excel整理(设置边框/字体/颜色/加粗/居中/)
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFCellStyle setBorder = wb.createCellStyle();
一、設(shè)置背景色:
setBorder.setFillForegroundColor((short) 13);// 設(shè)置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
二、設(shè)置邊框:
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
三、設(shè)置居中:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
setBorder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
四、設(shè)置字體:
HSSFFont font = wb.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設(shè)置字體大小
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);
setBorder.setFont(font);//選擇需要用到的字體格式
五、設(shè)置列寬:
sheet.setColumnWidth(0, 3766); //第一個(gè)參數(shù)代表列id(從0開始),第2個(gè)參數(shù)代表寬度值
六、設(shè)置自動(dòng)換行:
setBorder.setWrapText(true);//設(shè)置自動(dòng)換行
七、合并單元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//參數(shù)1:行號(hào) 參數(shù)2:起始列號(hào) 參數(shù)3:行號(hào) 參數(shù)4:終止列號(hào)
sheet.addMergedRegion(region1);
或者用
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
但應(yīng)注意兩個(gè)構(gòu)造方法的參數(shù)不是一樣的,具體使用哪個(gè)取決于POI的不同版本。
sheet.addMergedRegion(region1);
目前用過的就這么多,后續(xù)有新的會(huì)繼續(xù)添加。
單獨(dú)設(shè)置合并單元格邊框 RegionUtil.setBorderTop(1, region1, sheet, workbook); RegionUtil.setBorderBottom(1, region1, sheet, workbook); RegionUtil.setBorderLeft(1, region1, sheet, workbook); RegionUtil.setBorderRight(1, region1, sheet, workbook);
八、加邊框
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);
九、使用
獲取到某個(gè)單元格后在后面set
row1.createCell(5).setCellStyle(setBorder);
十、寬度自適應(yīng)
sheetAt.setColumnWidth(i, res.getBytes().length*2*256);
更多細(xì)節(jié)https://blog.csdn.net/mchehe/article/details/60128517
總結(jié)
以上是生活随笔為你收集整理的生成excel整理(设置边框/字体/颜色/加粗/居中/)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何关掉Eclipse里对于Maven下
- 下一篇: Fiori Hash url的生成原理