Java--最简单的Excel表格导出代码

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;
		}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值