展开全部
给个我之前的写的例子给你action 层public ActionForward dataExport(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
String filenames = "数据报表32313133353236313431303231363533e78988e69d8331333337383934_"+new Date().getTime()+".xls";
HttpUtil.setFileNameDownloadHeader(request, response, filenames);
OutputStream os = response.getOutputStream();
service.expdataReport(request, os);
response.flushBuffer();
os.close();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
service层public void expdataReport(HttpServletRequest request, OutputStream os)
throws IOException, RowsExceededException, WriteException {
String itemid = request.getParameter("itemInfo");
if (StringUtils.isEmpty(itemid)) {
return;
}
String hql = getZhiJianHql(request);
Map map = new HashMap();
map.put("itemid", itemid);
List list = (List) basedao
.getObjectsByCondtions(ItemReportTemplate.class, map, null);
WritableWorkbook wbook = Workbook.createWorkbook(os); // 工作表
WritableSheet sheet = wbook.createSheet("数据报表", 0);// 生成名为“sheet1”的工作表,参数0表示这是第一页
WritableFont times16font = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.GRAY_50);
WritableCellFormat times16format = new WritableCellFormat(times16font); // 字体样式
ReportUtils.parseExcelHeader(sheet, times16format, list);
List list_c = (List) basedao
.ExcuteHQL(hql.toString());
if (list_c != null && list_c.size() > 0) {
for (int j = 0; j
for (int i = 0; i
ReportUtils.parseExcelCell(list.get(i), list_c.get(j),
sheet, i, j + 1);
}
}
}
wbook.write();
wbook.close();
}