public String toExport() throws IOException
{
// excel的xml模版,你需要了解xml的Attributes怎么用
StringBuilder sbody = new StringBuilder();
sbody.append("<?xml version=\"1.0\"?>\n");
sbody.append("<?mso-application progid=\"Excel.Sheet\"?>\n");
sbody.append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
sbody.append("xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n");
sbody.append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");
sbody.append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");
sbody.append("xmlns:html=\"http:\\www.w3.org\\TR\\REC-html40\">\n");
sbody.append("<Styles>\n");
sbody.append("<Style ss:ID=\"s50\">");
sbody.append("<Font ss:Bold=\"1\" ss:Size=\"10\" x:CharSet=\"134\" ss:FontName=\"宋体\"/>");
sbody.append("</Style>");
sbody.append("</Styles>\n");
sbody.append("<Worksheet ss:Name=\"报名列表\"><Table>");
List<SignListVo> list = signService.signListVos(null);
if (list != null && list.size() > 0)
{
sbody.append("<Row>");
sbody.append("<Cell ss:StyleID=\"s50\"><Data ss:Type=\"String\">赛事活动名称</Data></Cell>");
sbody.append("<Cell ss:StyleID=\"s50\"><Data ss:Type=\"String\">姓名</Data></Cell>");
sbody.append("<Cell ss:StyleID=\"s50\"><Data ss:Type=\"String\">手机号</Data></Cell>");
sbody.append("<Cell ss:StyleID=\"s50\"><Data ss:Type=\"String\">参赛类型</Data></Cell>");
sbody.append("<Cell ss:StyleID=\"s50\"><Data ss:Type=\"String\">报名时间</Data></Cell>");
sbody.append("</Row>");
for (SignListVo vo : list)
{
sbody.append("<Row>");
sbody.append("<Cell><Data ss:Type=\"String\">" + vo.getSignMatch() + "</Data></Cell>");
sbody.append("<Cell><Data ss:Type=\"String\">" + vo.getSignUser() + "</Data></Cell>");
sbody.append("<Cell><Data ss:Type=\"String\">" + vo.getSignMobile() + "</Data></Cell>");
if ("0".equals(vo.getSignMatchtype()))
sbody.append("<Cell><Data ss:Type=\"String\">全马</Data></Cell>");
else
sbody.append("<Cell><Data ss:Type=\"String\">" + vo.getSignMatchtype() + "</Data></Cell>");
sbody.append("<Cell><Data ss:Type=\"String\">" + vo.getSignAddtime() + "</Data></Cell>");
sbody.append("</Row>");
}
}
sbody.append("</Table></Worksheet>");
sbody.append("</Workbook>\n");
String fileName = "报名列表" + TimeUtils.getToday(false) + ".xls";
byte[] data = sbody.toString().getBytes("utf-8");
HttpServletResponse response = getResponse();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(data);
ouputStream.close();
return RESULT_NONE;
}
思路来自:http://blog.csdn.net/jinjazz/article/details/2766203