// public HSSFWorkbook createExcel(List list) throws SysException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Sheet1"); ExcelUtil util = new ExcelUtil(wb); HSSFRow row = sheet.createRow(0); /* group相关信息 */ util.setCellValue(row, (short) 0, "亲情组名称", util.getTableHeadStyle()); util.setCellValue(row, (short) 1, "亲情组类型", util.getTableHeadStyle()); util.setCellValue(row, (short) 2, "费率", util.getTableHeadStyle()); util.setCellValue(row, (short) 3, "亲情组最大成员数目", util.getTableHeadStyle()); util.setCellValue(row, (short) 4, "亲情主号码", util.getTableHeadStyle()); util.setCellValue(row, (short) 5, "本地网", util.getTableHeadStyle()); util.setCellValue(row, (short) 6, "服务区", util.getTableHeadStyle()); util.setCellValue(row, (short) 7, "操作员工", util.getTableHeadStyle()); util.setCellValue(row, (short) 8, "操作员工号", util.getTableHeadStyle()); /* 成员相关信息 */ util.setCellValue(row, (short) 9, "亲情成员号码", util.getTableHeadStyle()); util.setCellValue(row, (short) 10, "号码类型", util.getTableHeadStyle()); util.setCellValue(row, (short) 11, "创建时间", util.getTableHeadStyle()); util.setCellValue(row, (short) 12, "号码所属本地网", util.getTableHeadStyle()); util.setCellValue(row, (short) 13, "号码操作员工", util.getTableHeadStyle()); util.setCellValue(row, (short) 14, "号码操作员工号", util.getTableHeadStyle()); // *用于标识当前写入的行号 */ int currentRow = 0; // *需要合并的开始行号*/ int beginRow = 0; for (int i = 0; i < list.size(); i++) { TdGroupVO groupVO = (TdGroupVO) list.get(i); beginRow = ++currentRow; row = sheet.createRow(beginRow); util.setCellValue(row, (short) 0, groupVO.getGroupName(), util.getStringStyle()); util.setCellValue(row, (short) 1, groupVO.getGroupType(), util.getStringStyle()); util.setCellValue(row, (short) 2, groupVO.getPrefRateName(), util.getStringStyle()); util.setCellValue(row, (short) 3, groupVO.getGroupNumber(), util.getIntegerStyle()); util.setCellValue(row, (short) 4, groupVO.getAccNbr(), util.getStringStyle()); util.setCellValue(row, (short) 5, groupVO.getLocalNetName(), util.getStringStyle()); util.setCellValue(row, (short) 6, groupVO.getAreaName(), util.getStringStyle()); util.setCellValue(row, (short) 7, groupVO.getName(), util.getStringStyle()); util.setCellValue(row, (short) 8, groupVO.getSysUserName(), util.getStringStyle()); List memberList = GroupDelegate.getInstance().queryGroupMemberByGroupId( groupVO.getGroupId().toString(), Long.valueOf(groupVO.getLocalNetId()).intValue()); for (int j = 0; j < memberList.size(); j++) { //用于合并单元格的样式初始化 if (j != 0) { util.setCellValue(row, (short) 0, "", util.getStringStyle()); util.setCellValue(row, (short) 1, "", util.getStringStyle()); util.setCellValue(row, (short) 2, "", util.getStringStyle()); util.setCellValue(row, (short) 3, "", util.getStringStyle()); util.setCellValue(row, (short) 4, "", util.getStringStyle()); util.setCellValue(row, (short) 5, "", util.getStringStyle()); util.setCellValue(row, (short) 6, "", util.getStringStyle()); util.setCellValue(row, (short) 7, "", util.getStringStyle()); util.setCellValue(row, (short) 8, "", util.getStringStyle()); } TdGroupMemberVO memberVO = (TdGroupMemberVO) memberList.get(j); util.setCellValue(row, (short) 9, memberVO.getAccNbr(), util.getIntegerStyle()); if (j == 0) { util.setCellValue(row, (short) 10, "组号码", util.getStringStyle()); } else { util.setCellValue(row, (short) 10, "成员号码", util.getStringStyle()); } util.setCellValue(row, (short) 11, memberVO.getStsDate().toString(), util .getStringStyle()); util.setCellValue(row, (short) 12, memberVO.getLocalNetName(), util .getStringStyle()); util.setCellValue(row, (short) 13, memberVO.getName(), util.getStringStyle()); util .setCellValue(row, (short) 14, memberVO.getSysUserName(), util .getStringStyle()); row = sheet.createRow(++currentRow);// add a new row } //合并单元格 for (int j = 0; j < 9; j++) { sheet.addMergedRegion(new Region(beginRow, (short) j, currentRow, (short) j)); } } return wb; }
其中ExcelUtil 是一个工具类,用来处理excel的样式问题,这种方式是处理一对多关系同时导出到一个excel的方法。