Controller获取传入条件query,为套用之前的分页查询,将pageIndex当前页置为第一页,每页记录pageSize数置为最大值10000,之后获取List数据存入表格中,然后设置response头信息,通过IO写出,OK!
用到一个设置response头信息的工具类。
=================================
运行时,遇到一个问题
报错信息:Cannot call sendError() after the response has been committed
和
Could not find acceptable representation
因为outputStream.close(); 关闭之后不能再return 其他内容 只能return null
@RequestMapping(value = "/exportOrderListByAgent", produces = "application/json", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> exportOrderListByShop(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map<String, Object> query) {
// 获取登入代理商
AgentInfo agent = (AgentInfo) getSession().getAttribute("login_agent");
// 判断是否登入
if (agent == null) {
return returnResultMap(ResultMapInfo.RELOGIN);
}
query.put("page_index", 1);
query.put("page_size", 10000);
Page<OrderInfo> result = agentDataService.orderListByAgent(agent, query);
if (result != null && result.getDatalist() != null) {
List<OrderInfo> list = result.getDatalist();
// 生成Excel文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("商户订单表");
// 表头
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("商户名称");
headRow.createCell(1).setCellValue("订单号");
headRow.createCell(2).setCellValue("消费金额");
headRow.createCell(3).setCellValue("实收");
headRow.createCell(4).setCellValue("手续费");
headRow.createCell(5).setCellValue("交易状态");
headRow.createCell(6).setCellValue("支付方式");
headRow.createCell(7).setCellValue("创建时间");
for (OrderInfo orderInfo : list) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(orderInfo.getShop_name());
dataRow.createCell(1).setCellValue(orderInfo.getOrder_number());
dataRow.createCell(2).setCellValue(orderInfo.getMoney() + "");
dataRow.createCell(3).setCellValue(orderInfo.getShop_income() + "");
dataRow.createCell(4).setCellValue(orderInfo.getPlatform_income() + "");
dataRow.createCell(5).setCellValue(orderInfo.getOrder_state());
dataRow.createCell(6).setCellValue(orderInfo.getPay_type());
dataRow.createCell(7).setCellValue(orderInfo.getCreate_time());
}
// 下载导出
// 设置头信息
try {
response.setContentType("application/vnd.ms-excel");
String filename = "商户订单表.xls";
String exportAgent = request.getHeader("user-agent");
filename = ExportFileUtils.encodeDownloadFilename(filename, exportAgent);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
// 关闭
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} else {
return returnResultMap(ResultMapInfo.GETFAIL);
}
}
工具类
public class ExportFileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
if (agent.contains("Firefox")) { // 火狐浏览器
filename = "=?UTF-8?B?"
+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
} else { // IE及其他浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}