public void exportDutyRecord(Dto pDto, HttpServletResponse response) {
Dto outDto = new BaseDto();
try {
// 获取要导出的数据
List resultList;
if ("allPage".equals(pDto.getAsString("allPage"))) {
// pDto.remove("RN");
resultList = atomDao.queryForList("Duty.queryEadutyRecordList",
pDto);
} else {
resultList = atomDao.queryForPage("Duty.queryEadutyRecordList",
pDto);
}
if (null == resultList || 0 == resultList.size()) {
outDto.put("msg", "没有 可导出的数据!");
outDto.put("success", new Boolean(false));
} response.setContentType("application/vnd.ms-excel");
// Excel
WritableWorkbook workbook = null;
// Excel中的sheet
WritableSheet sheet = null; SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String nowDate = sf.format(new Date());
// 创建Exl
String sheetTitle = URLEncoder.encode("排班记录" + nowDate + ".xls",
"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ sheetTitle); WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("GBK");
workbook = jxl.Workbook.createWorkbook(response.getOutputStream(),
ws);
// 设置sheet
sheet = workbook.createSheet(pDto.getAsString("dutydate")
+ " 月份排班记录", 0); jxl.write.WritableFont wf1 = new jxl.write.WritableFont(
WritableFont.createFont("宋体"), 11, WritableFont.BOLD);
jxl.write.WritableCellFormat wcf1 = new jxl.write.WritableCellFormat(
wf1);
// 纵向对齐
wcf1.setVerticalAlignment(jxl.format.VerticalAlignment.BOTTOM);
// 实线
wcf1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle
.getStyle(1)); // 对齐方式
wcf1.setAlignment(jxl.format.Alignment.CENTRE); jxl.write.WritableCellFormat wcf2 = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 11));
// 纵向对齐
wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.BOTTOM);
// 实线
wcf2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle
.getStyle(1));
wcf2.setWrap(true);
// 填写标题
sheet.addCell(new jxl.write.Label(0, 0, "日期", wcf1));
sheet.addCell(new jxl.write.Label(1, 0, "白班/夜班", wcf1));
sheet.addCell(new jxl.write.Label(1, 0, "值班人", wcf1));
// 列宽
sheet.setColumnView(0, 11);
sheet.setColumnView(1, 11);
sheet.setColumnView(2, 11); Dto itemDto = new BaseDto();
for (int i = 0; i < resultList.size(); i++) {
itemDto = (Dto) resultList.get(i); // 填写数据
// 日期
sheet.addCell(new jxl.write.Label(0, i + 1, itemDto
.getAsString("DUTYDATE"), wcf2)); // 白班/夜班
sheet.addCell(new jxl.write.Label(1, i + 1, itemDto
.getAsString("DUTYKIND"), wcf2));
// 白班/夜班
sheet.addCell(new jxl.write.Label(2, i + 1, itemDto
.getAsString("USERNAME"), wcf2)); }
workbook.write();
workbook.close(); } catch (Exception e) {
e.printStackTrace();
outDto.put("msg", "导出排班记录出错!");
outDto.put("success", new Boolean(false));
} finally {
}
// outDto.put("msg", "导出排班记录成功!");
// outDto.put("success", new Boolean(true));
// return outDto;
}