// 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<String, Object> 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<String, Object> 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<String, Object> params = Maps.newHashMap();
params.put("orgs", this.getOrganization());
params.put("orgUuid", paramMap.get("orgUuid"));
Map<String, Object> 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<String, Object> 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<String, Object> 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<String> 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<PeriodicExaminePageResult> result = new ArrayList<>();
if (isExistListInRedis(key)) {
List<Object> list = redisTemplate.boundListOps(key).range(0,-1);
for (int i = 0; i < list.size(); i++) {
result.add((PeriodicExaminePageResult) list.get(i));
}
}else {
result = operateService.queryPeriodicPageList(paramMap);
}
int index = 0;
int docTotal = 0, participateTotal = 0, finishTotal = 0, noFinishTotal = 0;
int commonlyTotal = 0, comFirstQualifiedTotal = 0, comWorkQualifiedTotal = 0, comMoralQualifiedTotal = 0, comBusinessResultTotal = 0,
comMurQualifiedTotal = 0, comMurNoQualifiedTotal = 0;
int simpleTotal = 0, simFirstQualifiedTotal = 0, simWorkQualifiedTotal = 0, simMoralQualifiedTotal = 0, simBusinessResultTotal = 0,
simMurQualifiedTotal = 0, simMurNoQualifiedTotal = 0;
for(PeriodicExaminePageResult p: result) {
HSSFRow row = sheet.createRow(index + 4);
cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(index+1); //序号
cell = row.createCell(1);
cell.setCellStyle(style);
cell.setCellValue(p.getName()); //所属机构
cell = row.createCell(2);
cell.setCellStyle(style);
CellRangeAddress cra = new CellRangeAddress(index + 4, index + 4, 1, 2); // 合并单元格,起始行、终止行、起始列、终止列
sheet.addMergedRegion(cra);
cell = row.createCell(3);
cell.setCellStyle(style);
cell.setCellValue(p.getTotal() == null? "0": p.getTotal()+"");//医师总数
docTotal += p.getTotal() == null? 0: p.getTotal();
cell = row.createCell(4);
cell.setCellStyle(style);
cell.setCellValue(p.getParticipateCount() == null? "0": p.getParticipateCount()+"");//应参加定考人数
participateTotal += p.getParticipateCount() == null? 0: p.getParticipateCount();
cell = row.createCell(5);
cell.setCellStyle(style);
cell.setCellValue(p.getFinishCount() == null? "0": p.getFinishCount()+"");//完成定考人数
finishTotal += p.getFinishCount() == null? 0: p.getFinishCount();
cell = row.createCell(6);
cell.setCellStyle(style);
cell.setCellValue(p.getNoFinishCount() == null? "0": p.getNoFinishCount()+"");//未完成定考人数
noFinishTotal += p.getNoFinishCount() == null? 0: p.getNoFinishCount();
cell = row.createCell(7);
cell.setCellStyle(style);
cell.setCellValue(p.getCommonlyCount() == null? "0": p.getCommonlyCount()+"");//一般程序总人数
commonlyTotal += p.getCommonlyCount() == null? 0: p.getCommonlyCount();
cell = row.createCell(8);
cell.setCellStyle(style);
cell.setCellValue(p.getComFirstQualified() == null? "0": p.getComFirstQualified()+"");//一般程序首次合格
comFirstQualifiedTotal += p.getComFirstQualified() == null? 0: p.getComFirstQualified();
cell = row.createCell(9);
cell.setCellStyle(style);
cell.setCellValue(p.getComWorkQualified() == null? "0": p.getComWorkQualified()+"");//一般程序工作成绩不合格
comWorkQualifiedTotal += p.getComWorkQualified() == null? 0: p.getComWorkQualified();
cell = row.createCell(10);
cell.setCellStyle(style);
cell.setCellValue(p.getComMoralQualified() == null? "0": p.getComMoralQualified()+"");//一般程序职业道德不合格
comMoralQualifiedTotal += p.getComMoralQualified() == null? 0: p.getComMoralQualified();
cell = row.createCell(11);
cell.setCellStyle(style);
cell.setCellValue(p.getComBusinessResult() == null? "0": p.getComBusinessResult()+"");//一般程序业务水平测试不合格
comBusinessResultTotal += p.getComBusinessResult() == null? 0: p.getComBusinessResult();
cell = row.createCell(12);
cell.setCellStyle(style);
cell.setCellValue(p.getComMurQualified() == null? "0": p.getComMurQualified()+"");//一般程序业再次考核合格
comMurQualifiedTotal += p.getComMurQualified() == null? 0: p.getComMurQualified();
cell = row.createCell(13);
cell.setCellStyle(style);
cell.setCellValue(p.getComMurNoQualified() == null? "0": p.getComMurNoQualified()+"");//一般程序业再次考核不合格
comMurNoQualifiedTotal += p.getComMurNoQualified() == null? 0: p.getComMurNoQualified();
cell = row.createCell(14);
cell.setCellStyle(style);
cell.setCellValue(p.getSimpleCount() == null? "0": p.getSimpleCount()+"");//简易程序总人数
simpleTotal += p.getSimpleCount() == null? 0: p.getSimpleCount();
cell = row.createCell(15);
cell.setCellStyle(style);
cell.setCellValue(p.getSimFirstQualified() == null? "0": p.getSimFirstQualified()+"");//简易程序首次合格
simFirstQualifiedTotal += p.getSimFirstQualified() == null? 0: p.getSimFirstQualified();
cell = row.createCell(16);
cell.setCellStyle(style);
cell.setCellValue(p.getSimWorkQualified() == null? "0": p.getSimWorkQualified()+"");//简易程序工作成绩不合格
simWorkQualifiedTotal += p.getSimWorkQualified() == null? 0: p.getSimWorkQualified();
cell = row.createCell(17);
cell.setCellStyle(style);
cell.setCellValue(p.getSimMoralQualified() == null? "0": p.getSimMoralQualified()+"");//简易程序职业道德不合格
simMoralQualifiedTotal += p.getSimMoralQualified() == null? 0: p.getSimMoralQualified();
cell = row.createCell(18);
cell.setCellStyle(style);
cell.setCellValue(p.getSimBusinessResult() == null? "0": p.getSimBusinessResult()+"");//简易程序业务水平测试不合格
simBusinessResultTotal += p.getSimBusinessResult() == null? 0: p.getSimBusinessResult();
cell = row.createCell(19);
cell.setCellStyle(style);
cell.setCellValue(p.getSimMurQualified() == null? "0": p.getSimMurQualified()+"");//简易程序业再次考核合格
simMurQualifiedTotal += p.getSimMurQualified() == null? 0: p.getSimMurQualified();
cell = row.createCell(20);
cell.setCellStyle(style);
cell.setCellValue(p.getSimMurNoQualified() == null? "0": p.getSimMurNoQualified()+"");//简易程序业再次考核不合格
simMurNoQualifiedTotal += p.getSimMurNoQualified() == null? 0: p.getSimMurNoQualified();
cell = row.createCell(21);
cell.setCellStyle(style);
cell.setCellValue(StringUtils.isEmpty(p.getQualificationRate())? "0.00%": p.getQualificationRate()+"%");//合格率
index++;
}
HSSFRow rowTotal = sheet.createRow(index + 4);
cell = rowTotal.createCell(0);
cell.setCellStyle(style);
cell.setCellValue("合计");
cell = rowTotal.createCell(1);
cell.setCellStyle(style);
cell.setCellValue("-");
cell = rowTotal.createCell(2);
cell.setCellStyle(style);
CellRangeAddress cra = new CellRangeAddress(index + 4, index + 4, 1, 2); // 合并单元格,起始行、终止行、起始列、终止列
sheet.addMergedRegion(cra);
cell = rowTotal.createCell(3);
cell.setCellStyle(style);
cell.setCellValue(docTotal);
cell = rowTotal.createCell(4);
cell.setCellStyle(style);
cell.setCellValue(participateTotal);
cell = rowTotal.createCell(5);
cell.setCellStyle(style);
cell.setCellValue(finishTotal);
cell = rowTotal.createCell(6);
cell.setCellStyle(style);
cell.setCellValue(noFinishTotal);
cell = rowTotal.createCell(7);
cell.setCellStyle(style);
cell.setCellValue(commonlyTotal);
cell = rowTotal.createCell(8);
cell.setCellStyle(style);
cell.setCellValue(comFirstQualifiedTotal);
cell = rowTotal.createCell(9);
cell.setCellStyle(style);
cell.setCellValue(comWorkQualifiedTotal);
cell = rowTotal.createCell(10);
cell.setCellStyle(style);
cell.setCellValue(comMoralQualifiedTotal);
cell = rowTotal.createCell(11);
cell.setCellStyle(style);
cell.setCellValue(comBusinessResultTotal);
cell = rowTotal.createCell(12);
cell.setCellStyle(style);
cell.setCellValue(comMurQualifiedTotal);
cell = rowTotal.createCell(13);
cell.setCellStyle(style);
cell.setCellValue(comMurNoQualifiedTotal);
cell = rowTotal.createCell(14);
cell.setCellStyle(style);
cell.setCellValue(simpleTotal);
cell = rowTotal.createCell(15);
cell.setCellStyle(style);
cell.setCellValue(simFirstQualifiedTotal);
cell = rowTotal.createCell(16);
cell.setCellStyle(style);
cell.setCellValue(simWorkQualifiedTotal);
cell = rowTotal.createCell(17);
cell.setCellStyle(style);
cell.setCellValue(simMoralQualifiedTotal);
cell = rowTotal.createCell(18);
cell.setCellStyle(style);
cell.setCellValue(comBusinessResultTotal);
cell = rowTotal.createCell(19);
cell.setCellStyle(style);
cell.setCellValue(simMurQualifiedTotal);
cell = rowTotal.createCell(20);
cell.setCellStyle(style);
cell.setCellValue(simMurNoQualifiedTotal);
cell = rowTotal.createCell(21);
cell.setCellStyle(style);
cell.setCellValue("-");
}
//发送响应流方法
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 setExamResultSheet2Title(HSSFSheet sheet, List<HSSFRow> rows, HSSFCellStyle style, int colStart, int colEnd, int rowStart, int rowEnd, String title) {
HSSFCell cell = null;
for(int i = 0; i < rows.size(); ++i) {
for(int j = colStart; j <= colEnd; ++j) {
cell = rows.get(i).createCell(j);
cell.setCellStyle(style);
if(i == 0 && j == colStart) {
cell.setCellValue(title);
}
}
}
CellRangeAddress cra = new CellRangeAddress(rowStart, rowEnd, colStart, colEnd); // 合并单元格,起始行、终止行、起始列、终止列
sheet.addMergedRegion(cra);
}
// 标题样式
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);
}
最后导出这样的一个excel。
注意这个案例是导出一个excel不是三个
声明:未经博主允许任何人不得随意发表为原创.