HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFCellStyle setBorder = wb.createCellStyle();
一、設置背景色:
setBorder.setFillForegroundColor((short) 13);// 設置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
二、設置邊框:
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
三、設置居中:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
setBorder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
四、設置字體:
HSSFFont font = wb.createFont();
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設置字體大小
font.setFontName("黑體");
font.setFontHeightInPoints((short) 16);//設置字體大小
HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
font2.setFontHeightInPoints((short) 12);
setBorder.setFont(font);//選擇需要用到的字體格式
五、設置列寬:
sheet.setColumnWidth(0, 3766); //第一個參數代表列id(從0開始),第2個參數代表寬度值
六、設置自動換行:
setBorder.setWrapText(true);//設置自動換行
七、合並單元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//參數1:行號 參數2:起始列號 參數3:行號 參數4:終止列號
sheet.addMergedRegion(region1);
sheet.addMergedRegion(region1);
或者用
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
但應注意兩個構造方法的參數不是一樣的,具體使用哪個取決於POI的不同版本。
sheet.addMergedRegion(region1);
sheet.addMergedRegion(region1);
目前用過的就這么多,后續有新的會繼續添加。
單獨設置合並單元格邊框 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);
九、使用
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);
九、使用
獲取到某個單元格后在后面set
row1.createCell(5).setCellStyle(setBorder);
十、寬度自適應
sheetAt.setColumnWidth(i, res.getBytes().length*2*256);