Java实现前端把数据导出到本地excel

excel导入的相关代码请移步:
请戳这里!!!!

————————————————————————————
maven导一下包:

		<dependency>
		    <groupId>net.sourceforge.jexcelapi</groupId>
		    <artifactId>jxl</artifactId>
		    <version>2.6.12</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>

前端页面:
在这里插入图片描述
导出:
在这里插入图片描述

前端代码:

//导出按钮绑定点击事件,调用后台方法
$("#xiazai").click(function() {
		window.location.href = "download";

	})

后台download方法:

@RequestMapping("/download")
	@ResponseBody
	public void download(HttpServletResponse response,PlatformRecords pr,HttpSession session) {
		try {

			response.setContentType("application/vnd.ms-excel");
			response.addHeader("Content-Disposition", "attachment;fileName=_platformrecords.xls");
			OutputStream os = response.getOutputStream();

			WritableWorkbook wwb = null;
			wwb = Workbook.createWorkbook(os);
			WritableSheet ws = wwb.createSheet("sheet1", 0);// 创建一个可写入的工作表
			
			ws.setColumnView(0, 25);
			ws.setColumnView(1, 25);
			ws.setColumnView(2, 25);
			ws.setColumnView(3, 25);
			ws.setColumnView(4, 25);
			ws.setColumnView(5, 25);
			ws.setColumnView(6, 25);// 设置列的宽度

			ws.addCell(new Label(0, 0, "访问时间"));
			ws.addCell(new Label(1, 0, "渠道ID"));
			ws.addCell(new Label(2, 0, "渠道IP"));
			ws.addCell(new Label(3, 0, "接口交易码"));
			ws.addCell(new Label(4, 0, "状态"));
			ws.addCell(new Label(5, 0, "响应时间"));
			ws.addCell(new Label(6, 0, "备注"));


			//调用查询时的方法,保存到session里
			/*
			List<PlatformRecords> list = service.downloadPlatformRecords(pr);
			session.setAttribute("list", list);
			*/
			List<PlatformRecords> list = (List<PlatformRecords>) session.getAttribute("list");
			
	    
			for (int i = 0,length = list.size(); i < length; i++) {
				pr = list.get(i);
				ws.addCell(new Label(0, i + 1, pr.getInterviewtime()));
				ws.addCell(new Label(1, i + 1, pr.getPlatformId()));
				ws.addCell(new Label(2, i + 1, pr.getPlatformIp()));
				ws.addCell(new Label(3, i + 1, pr.getTransCode()));
				ws.addCell(new Label(4, i + 1, "正常"));
				ws.addCell(new Label(5, i + 1, pr.getProcTime()+""));
				ws.addCell(new Label(6, i + 1, "正常"));
			}
			wwb.write();// 从内存中写入文件
			wwb.close(); // 关闭资源,释放内存
			os.close();
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}

	}

欢迎各位大佬交流交流!

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值