1 //创建单元格,并设置值表头 设置表头居中 2 XSSFFont font = wb.createFont(); 3 font.setFontHeightInPoints((short) 6); // 表头字体大小 4 font.setColor(XSSFFont.COLOR_NORMAL); //字体颜色 5 font.setFontHeight((short) 200); 6 font.setFontName("宋体"); // 表头字体名称 7 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); // 表头字体加粗 8 9 XSSFCellStyle cellStyle = wb.createCellStyle(); // 表头格式 10 cellStyle.setFont(font); // 表头字体 11 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 表头位置 创建一个居中格式 12 cellStyle.setBorderBottom(BORDER_THIN);// 下边框 13 cellStyle.setBorderLeft(BORDER_THIN);// 左边框 14 cellStyle.setBorderRight(BORDER_THIN);// 右边框 15 cellStyle.setBorderTop(BORDER_THIN);// 上边框 16 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 17 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//水平布局:居中 18 cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); 19 cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); 20 cellStyle.setWrapText(true);//设置自动换行 21 cellStyle.setLocked(true); 22
CellRangeAddress region31 = new CellRangeAddress(1, 2, 43, 43);
// 合并单元
sheet1.addMergedRegion(region31);
XSSFCell cell143 = row1.createCell(43);
cell143.setCellStyle(cellStyle);
cell143.setCellValue(new XSSFRichTextString("建筑高度"));
XSSFCell cell243 = row2.createCell(43);
cell243.setCellStyle(cellStyle);
添加公式的方法
public static void makeSumCellByGS(XSSFWorkbook wb,XSSFSheet sheet,XSSFCellStyle cellStyle,int rowNum,int colNum,String gs){
XSSFRow row = sheet.getRow(rowNum);
XSSFCell cell = row.createCell(colNum);
cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
//添加公式
cell.setCellFormula(gs);
cell.setCellStyle(cellStyle);
}