当前位置:编程学习 > JAVA >>

用jxl将数据库中的数据导出到excel,代码都走完了,但是页面没有任何反应,无法下载,代码如下,求高手指点

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;
}
--------------------编程问答-------------------- 用jxls吧,简单多了
http://blog.csdn.net/cictech/article/details/2620527 --------------------编程问答--------------------
引用 1 楼 a454344407 的回复:
用jxls吧,简单多了
http://blog.csdn.net/cictech/article/details/2620527

jxls是页面美观一些,我现在用jxl实现下载,功能都不可用,请问这是为什么呢? --------------------编程问答-------------------- response.getOutputStream();这个是你生成的excelStream吧。
但是我没发现有向客户端浏览器推送这个流的语句啊。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,