首先需要导出的页面是这样,涉及到合并单元格的操作
调用合并单元格需要的参数说明
String fileName = "调休统计表";
String[] head0=new String[] {"姓名","值班类型","工作日夜班","工作日夜班","周末公休日和法定节假日","周末公休日和法定节假日","合计补休(天)"};
String[] headnum0 = new String[] { "1,2,0,0", "1,2,1,1", "1,1,2,3",
"1,1,4,5", "1,2,6,6"};//对应excel中的行和列,下表从0开始{"开始行,结束行,开始列,结束列"}
String[] head1 = new String[]{"值班(个)","补休(天)","占用(个)","补休(天)"};
String[] headnum1 = new String[] { "2,2,2,2", "2,2,3,3", "2,2,4,4",
"2,2,5,5" };
String[] colName = new String[] { "name", "ondutyName", "nums",
"compRest", "numsHoliday", "compRestHoliday", "sum" };//需要显示在excel中的参数对应的值,因为是用map存的,放的都是对应的key
//list是需要导出的列表的数据
ExcelUtils.reportMergeXls(request,response,list,head0,headnum0,head1,headnum1,colName,fileName);
调用合并单元格的接口
/**
* 多行表头
* dataList:导出的数据;sheetName:表头名称; head0:表头第一行列名;headnum0:第一行合并单元格的参数
* head1:表头第二行列名;detail:导出的表体字段
*
*/
public static void reportMergeXls(HttpServletRequest request,
HttpServletResponse response, List<Map<String, Object>> dataList,
String[] head0, String[] headnum0,
String[] head1, String[] headnum1, String[] detail ,
String fileName) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();// 创建一个表
// 设置列宽 (第几列,宽度)
// 第一行表头标题
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, head0.length-1));
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 0x349);
HSSFCell cell = row.createCell(0);
cell.setCellValue(fileName);
// 第二行表头列名
row = sheet.createRow(1);
for (int i = 0; i < head0.length; i++) {
cell = row.createCell(i);
cell.setCellValue(head0[i]);
}
//动态合并单元格
for (int i = 0; i < headnum0.length; i++) {
String[] temp = headnum0[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}
//设置合并单元格的参数并初始化带边框的表头(这样做可以避免因为合并单元格后有的单元格的边框显示不出来)
row = sheet.createRow(2);//因为下标从0开始,所以这里表示的是excel中的第三行
for (int i = 0; i < head0.length; i++) {
for (int j = 0; j < head1.length; j++) {
cell = row.createCell(j + 2);
cell.setCellValue(head1[j]);
}
}
//动态合并单元格
/*for (int i = 0; i < headnum1.length; i++) {
String[] temp = headnum1[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
startcol, overcol));
}*/
// 设置列值-内容
for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow(i + 3);
for (int j = 0; j < detail.length; j++) {
Map tempmap = (HashMap) dataList.get(i);
Object data = tempmap.get(detail[j]);
cell = row.createCell(j);
cell.setCellValue(String.valueOf(data));
}
}
//3.将excel文件返回到前台
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateTime = sdf.format(new Date());
String filename = fileName + "(" + dateTime + ").xls";
writeResponse(workbook, filename, request, response);
}