java导出excel 自定义表头

//声明一个工作薄
HSSFWorkbook workbook =new HSSFWorkbook();
//创建sheet
HSSFSheet sheet =workbook.createSheet(“统计表”);
//设置根据列名,列宽自适应 ,columnIndex 列索引
sheet.setColumnWidth(columnIndex,“列名”.getBytes().length2256);
//创建style
HSSFCellStyle style = workbook.createCellStyle();
//设置单元格边框
style.setBorderTop(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN);
//设置单元格水平、垂直居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//创建行
HSSFRow row =sheet.createRow(0);
//设置单元格合并,四个参数分别为:起始行、结束行、起始列、结束列
CellRangeAddress rangeAddress = new CellRangeAddress(0,1,0,0);
//添加到sheet中
sheet.addMergedRegion(rangeAddress);
//创建列,赋值、并添加样式
HSSFCell cell1 = row.createCell(0);
cell1.setCellStyle(style);
cell1.setCellValue(“序号”);

你可能感兴趣的:(代码,java)