// excel的文档对象
HSSFWorkbook wb = new HSSFWorkbook();
// excel的表单
HSSFSheet sheet = wb.createSheet("性别"); // sheet的名称为性别
// excel字体
HSSFFont font = wb.createFont();
// cell样式
HSSFCellStyle style = wb.createCellStyle();
// excel的行 从0行开始
HSSFRow row = sheet.createRow(1);
// excel的格子单元
HSSFCell cell = row.createCell(1);
// 合并单元格,起始行、终止行、起始列、终止列
CellRangeAddress cra = new CellRangeAddress(rowStart, rowEnd, colStart, colEnd);
// 合并内容
sheet.addMergedRegion(cra);
上面由于无法使用树形图所以在这里我只能文字说明一下,
(1) 首先创建一个文档对象
(2) 在文档对象的下面我们可以创建sheet、创建font字体、创建CellStyle
(3) 在sheet下面创建Row、对行进行合并、设置行的宽度
(4) 在row下面创建cell
二、对于上面的解释我们再往近一步操作
(1) 在Excel对象下创建的内容
// excel的文档对象
HSSFWorkbook wb = new HSSFWorkbook();
// excel的表单
HSSFSheet sheet = wb.createSheet("性别"); // sheet的名称为性别
// excel字体
HSSFFont font = wb.createFont();
// cell样式
HSSFCellStyle style = wb.createCellStyle();
(2)在sheet下创建的内容
(1) 合并单元格操作
// 合并单元格,起始行、终止行、起始列、终止列
CellRangeAddress cra = new CellRangeAddress(rowStart, rowEnd, colStart, colEnd);
// 使用sheet将行内容进行合并
sheet.addMergedRegion(cra);
(2) 设置行的宽度
sheet.setColumnWidth(i, 256*18); // i 表示i的
(3) 利用sheet设置行
HSSFRow rowTotal = sheet.createRow(index + 4);
(3)在行下面创建的内容
(1)// 利用行设置cell
HSSFCell cell = row.createCell(1);
(4)在cell下面创建内容
(1)// 设置单元格行的样式
cell.setCellStyle(style);
(2)// 设置单元格的值
cell.setCellValue("男");
(5)在font下创建内容
(1)设置字体的大小
font.setFontHeightInPoints((short)14);
(2)设置字体的颜色等
font.setColor
(6)在style下面创建内容
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中对齐
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边
style.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边
style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 顶边
style.setFont(font); // 样式当中的字体
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 设置填充的方式
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); // 设置前景颜色
三、下面我们以一个实例进行分析:
public void exportExcel(@RequestParam Map paramMap) {
// 创建excel
HSSFWorkbook wb = new HSSFWorkbook();
//年龄
this.fillSexRato(wb, paramMap);
this.setResponseHeader(response, "运营数据.xls");
OutputStream os = null;
try {
os = response.getOutputStream();
wb.write(os);
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void fillSexRato(HSSFWorkbook wb, Map paramMap) {
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)14);
HSSFSheet sheet = wb.createSheet("性别");
//标题
this.setTitle(wb, sheet, "医师性别比例统计表", font, 1, 2);
//内容样式
HSSFCellStyle style = this.contentStyle1(wb);
HSSFRow row = sheet.createRow(1);
HSSFCell cell = row.createCell(1);
cell.setCellStyle(style);
cell.setCellValue("男");
cell = row.createCell(2);
cell.setCellStyle(style);
cell.setCellValue("女");
// ==============从redis获取数据 start===================
Map params = Maps.newHashMap();
params.put("orgs", this.getOrganization());
params.put("orgUuid", paramMap.get("orgUuid"));
Map sexRato = Maps.newHashMap();
String key = this.generateKey(SEX_RATO_KEY, params);
if(this.isExistInRedis(key)) {
sexRato = this.getMapFromRedis(key);
}else {
sexRato = doctorInfoService.selectSexRato(params);
}
// ==============从redis获取数据 end====================
row = sheet.createRow(2);
cell = row.createCell(1);
cell.setCellStyle(style);
cell.setCellValue(sexRato.get("male") == null? "0": sexRato.get("male").toString());
cell = row.createCell(2);
cell.setCellStyle(style);
cell.setCellValue(sexRato.get("female") == null? "0": sexRato.get("female").toString());
}
//发送响应流方法
private void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void setTitle(HSSFWorkbook wb, HSSFSheet sheet, String title, HSSFFont font, Integer startCol, Integer endCol) {
//标题样式
HSSFCellStyle styleTitle = wb.createCellStyle();
styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中对齐
styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
styleTitle.setFont(font);
for(int i = startCol; i <= endCol; ++i) {
sheet.setColumnWidth(i, 256*18);
}
//title
HSSFRow rowTitle = sheet.createRow(0);
HSSFCell cellTitle = rowTitle.createCell(startCol);
cellTitle.setCellStyle(styleTitle);
cellTitle.setCellValue(title);
CellRangeAddress cra = new CellRangeAddress(0, 0, startCol, endCol); // 合并单元格,起始行、终止行、起始列、终止列
sheet.addMergedRegion(cra);
}
示例图:
四、实例二多个表格合并
// 主方法
public void exportExcel(@RequestParam Map paramMap) {
// 创建excel
HSSFWorkbook wb = new HSSFWorkbook();
//第九个sheet:定期考核结果分布
this.fillExamResultSheet2(wb, paramMap);
this.setResponseHeader(response, "运营数据.xls");
OutputStream os = null;
try {
os = response.getOutputStream();
wb.write(os);
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 设置数据和标题
private void fillExamResultSheet2(HSSFWorkbook wb, Map paramMap) {
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)16);
HSSFSheet sheet = wb.createSheet("考核结果分布(新)");
//标题
this.setTitle(wb, sheet, "医师定期考核结果分布表", font, 0, 21);
//表头
HSSFCellStyle style = this.contentStyle1(wb);
HSSFCellStyle styleTitleGray = this.contentStyleGray(wb);
HSSFCellStyle styleTitleBlue = this.contentStyleBlueGrey(wb);
HSSFCellStyle styleTitlePink = this.contentStylePink(wb);
HSSFCellStyle styleTitleGreen = this.contentStyleGreen(wb);
HSSFRow row1 = sheet.createRow(1);
HSSFRow row2 = sheet.createRow(2);
HSSFRow row3 = sheet.createRow(3);
HSSFCell cell = null;
//序号
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), style, 0, 0, 1, 3, "序号");
//所属行政
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), style, 1, 2, 1, 3, "所属行政");
//医师总数
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), styleTitleGray, 3, 3, 1, 3, "医师总数");
//应参加定考人数
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), styleTitleGray, 4, 4, 1, 3, "应参加定考人数");
//完成定考人数
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), styleTitleGray, 5, 5, 1, 3, "完成定考人数");
//未完成定考人数
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), styleTitleGray, 6, 6, 1, 3, "未完成定考人数");
//一般程序
this.setExamResultSheet2Title(sheet, Arrays.asList(row1), styleTitleBlue, 7, 13, 1, 1, "一般程序");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2, row3), styleTitleBlue, 7, 7, 2, 3, "总人数");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2), styleTitleBlue, 8, 11, 2, 2, "首次考核");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2), styleTitleBlue, 12, 13, 2, 2, "再次考核");
List headers = Arrays.asList("首次合格", "工作成绩不合格", "职业道德不合格", "业务水平测试不合格", "合格", "不合格");
for(int i = 0; i < headers.size(); ++i) {
cell = row3.createCell(i+8);
cell.setCellStyle(styleTitleBlue);
cell.setCellValue(headers.get(i));
}
//简易程序
this.setExamResultSheet2Title(sheet, Arrays.asList(row1), styleTitlePink, 14, 20, 1, 1, "简易程序");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2, row3), styleTitlePink, 14, 14, 2, 3, "总人数");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2), styleTitlePink, 15, 18, 2, 2, "首次考核");
this.setExamResultSheet2Title(sheet, Arrays.asList(row2), styleTitlePink, 19, 20, 2, 2, "再次考核");
headers = Arrays.asList("首次合格", "工作成绩不合格", "职业道德不合格", "业务水平测试不合格", "合格", "不合格");
for(int i = 0; i < headers.size(); ++i) {
cell = row3.createCell(i+15);
cell.setCellStyle(styleTitlePink);
cell.setCellValue(headers.get(i));
}
//合格率
this.setExamResultSheet2Title(sheet, Arrays.asList(row1, row2, row3), styleTitleGreen, 21, 21, 1, 3, "合格率");
//填充数据
String key = this.generateKey(PERIDIC_EXAMINE_TAB4_KEY, paramMap);
List result = new ArrayList<>();
if (isExistListInRedis(key)) {
List
最后导出这样的一个excel。
注意这个案例是导出一个excel不是三个
声明:未经博主允许任何人不得随意发表为原创.