js代码如下
$.ajax({
url: '/req/ajax/req/zjInfoAjaxAction.do?method=emportReqDate&boInsId=<%=boInsId%>',
type: 'POST',
     timeout: 5000,
    async: false,
error: function(){
alert('Error Loading Data, 请重新刷新页面');
},
success: function(text){
alert(text);
}
});
url请求的是另外一个工程下的action
action代码如下:
public ActionForward emportReqDate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.info("execute emportReqDate method.");
try {
String boinsId = request.getParameter("boInsId");// 下发单ID
//String orgId = request.getParameter("orgId");
IReqCollect iCollectInfoService = (IReqCollect) this.getBean("iReqCollect");
List results = iCollectInfoService.quaryReportByCollId(boinsId);
response.setBufferSize(100 * 1024);
response.setHeader("Cache-Control", "public");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode("需求汇总.xls", "UTF-8"));
OutputStream output = response.getOutputStream();
ZjInfoAjaxAction.outDate(results, output);// 调用具体导出excel方法
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
return null;
}public static void outDate(List resultlList, OutputStream out) {
WritableWorkbook workBook = null;
// 声明两个类型的单元格 一个是:字符串类型的;一个是数字类型的.
Label cell = null;
try {
// 创建工作薄,创建工作页
workBook = Workbook.createWorkbook(out);
WritableSheet workSheet = workBook.createSheet("需求列表", 0); // 设置列标题样式
WritableFont font = new WritableFont(WritableFont.COURIER, 13,
WritableFont.BOLD);
font.setColour(Colour.BLUE_GREY);
WritableCellFormat cellFormat = new WritableCellFormat(font);
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat.setBackground(Colour.PALE_BLUE);
cellFormat
.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
// 设置标题
String[] title = new String[] { "需求单位", "需求部门", "需求联系人", "需求名称",
"期望完成时间", "需求负责人" }; for (int i = 0; i < title.length; i++) {
workSheet.setColumnView(i, 20);
cell = new Label(i, 0, title[i], cellFormat);
workSheet.mergeCells(i, 0, i, 2);
workSheet.addCell(cell);
}
Iterator it = resultlList.iterator();
int flag = title.length;
int j = 0;
// Map map = new HashMap(); // 输入数据
cellFormat = new WritableCellFormat(new WritableFont(
WritableFont.COURIER, 9));
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat
.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK); ReqReportEntity reportBean = null;
flag = 0;
while (it.hasNext()) {
reportBean = (ReqReportEntity) it.next();
int n = 0;
cell = new Label(n++, flag + 3, reportBean.getReport_org_name());
workSheet.addCell(cell);
cell = new Label(n++, flag + 3, reportBean
.getReport_dept_name());
workSheet.addCell(cell);
cell = new Label(n++, flag + 3, reportBean
.getReport_user_name());
workSheet.addCell(cell);
cell = new Label(n++, flag + 3, reportBean.getReport_title());
workSheet.addCell(cell);
cell = new Label(n++, flag + 3, reportBean.getReport_end_time());
workSheet.addCell(cell);
cell = new Label(n++, flag + 3, reportBean
.getReport_conn_name());
workSheet.addCell(cell);
flag++;
}
} catch (Exception e) {
log.error(e);
e.printStackTrace();
} finally {
try {
// 将数据输出
workBook.write();
} catch (IOException e1) {
log.error(e1);
e1.printStackTrace();
}
// 关闭流
if (workBook != null) {
try {
workBook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}