Sheet sheet = workbook.createSheet();
Font font = workbook.createFont(); //设置字体格式
font.setBold(true); //加粗
CellStyle cellStyle = createCellStyle(workbook); //创建样式对象 cellStyle
cellStyle.setFont(font); //把字体应用到当前的样式
CellStyle cellStyle1 = createCellStyle(workbook); //创建样式对象 cellStyle1
//设置每列的宽度
int[] arr = {20,15,10,25,20,40,10,10};
for(int i=0;i<=arr.length -1;i++){
sheet.setColumnWidth(i, arr[i] * 256);
}
createRow(sheet.createRow(0),cellStyle, "业主信息","房屋代码", "姓名", "手机", "身份证", "地址", "传真", "车型号", "车牌号");
// 合并单元格 a、c代表行与列,b d代表要合并到少行与列
CellRangeAddress callRangeAddress = new CellRangeAddress(a, b, c, d);
sheet.addMergedRegion(callRangeAddress);
private CellStyle createCellStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
return cellStyle;
}
private void createRow(Row row,CellStyle style, String... cellValues) {
for (int i = 0; i < cellValues.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(cellValues[i]);
cell.setCellStyle(style);
}
}