//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("sheet1");
//设置表头样式
HSSFCellStyle styleTitle = wb.createCellStyle();
//水平居中
styleTitle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
styleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置背景色
styleTitle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
styleTitle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
styleTitle.setFillPattern(FillPatternType.LEAST_DOTS);
//设置边框
styleTitle.setBorderBottom(BorderStyle.THIN);
styleTitle.setBottomBorderColor(IndexedColors.BLUE.index);
styleTitle.setBorderLeft(BorderStyle.THIN);
styleTitle.setLeftBorderColor(IndexedColors.BLUE.index);
styleTitle.setBorderRight(BorderStyle.THIN);
styleTitle.setRightBorderColor(IndexedColors.BLUE.index);
styleTitle.setBorderTop(BorderStyle.THIN);
styleTitle.setTopBorderColor(IndexedColors.BLUE.index);
//设置字体
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);//设置字体大小
font.setBold(true);//粗体显示;
styleTitle.setFont(font);
//创建第一行
HSSFRow row=sheet.createRow(0);
row.setHeight((short)500);//设置行高
HSSFCell cell=null;
//插入第一行数据的表头
for(int i=0;i<title.length;i++){
sheet.setColumnWidth(i, 5000); //第一个参数代表列id(从0开始),第2个参数代表宽度值
if(i==3) {
sheet.setColumnWidth(i, 10000); //第一个参数代表列id(从0开始),第2个参数代表宽度值
}
cell=row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(styleTitle);
}