//直接上代码
public void exportPersons(HttpServletResponse response) {
//生成excel表格
String filePath = this.getClass().getResource("").getPath();//设置文件路径
int indexOf = filePath.indexOf(BackStageConstant.PROJECT_NAME);
//获取服务器的地址信息
String path1 = filePath + "体检团体个人名单录入模板" + ".xls";
HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook)
//创建sheet页
HSSFSheet sheet = workbook.createSheet("体检团体名单");//创建工作表(Sheet)
//创建header页
HSSFHeader header = sheet.getHeader();
//创建首行
Row row = sheet.createRow(0);
//创建一个单元格(第一列)
Cell cell = row.createCell(0);
//创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
// 创建一个居中格
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setAlignment(HorizontalAlignment.CENTER); // 居中
//设置第一列的值
row.createCell(0).setCellValue("医院名称");//excel中字段名
//设置第二列的值
row.createCell(1).setCellValue("单位名称");//excel中字段名
//设置第三列的值
row.createCell(2).setCellValue("套餐");//excel中字段名
//设置第四列的值
row.createCell(3).setCellValue("姓名");//excel中字段名
//设置第五列的值
row.createCell(4).setCellValue("性别(男/女)");//excel中字段名
//设置第六列的值
row.createCell(5).setCellValue("身份证");//excel中字段名
//设置第七列的值
row.createCell(6).setCellValue("手机");//excel中字段名
//设置第八列的值
row.createCell(7).setCellValue("上限金额");//excel中字段名
//设置第九列的值
row.createCell(8).setCellValue("卡号");//excel中字段名
//设置第十列的值
row.createCell(9).setCellValue("密码");//excel中字段名
//设置表头居中
row.getCell(0).setCellStyle(style);
row.getCell(1).setCellStyle(style);
row.getCell(2).setCellStyle(style);
row.getCell(3).setCellStyle(style);
row.getCell(4).setCellStyle(style);
row.getCell(5).setCellStyle(style);
row.getCell(6).setCellStyle(style);
row.getCell(7).setCellStyle(style);
row.getCell(8).setCellStyle(style);
row.getCell(9).setCellStyle(style);
//设置列宽
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(2, 6000);
sheet.setColumnWidth(3, 2000);
sheet.setColumnWidth(4, 2000);
sheet.setColumnWidth(5, 8000);
sheet.setColumnWidth(6, 6000);
sheet.setColumnWidth(7, 3000);
sheet.setColumnWidth(8, 6000);
sheet.setColumnWidth(9, 2000);
//保存文件
FileOutputStream out = null;
try {
out = new FileOutputStream(path1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
workbook.write(out);//保存Excel文件
out.close();//关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
//读取文件
PoiUtil.downloadFile(response, path1);
//文件读取完毕,删除生成的文件
File file = new File(path1);
file.delete();
}