JSP + SSM 实现对表格数据数据的Excel 导出

一、页面效果

二、jsp页面

2.1 定义导出按钮

<button onclick="exportAll()" type="button" class="btn btn-info 报表">导出到Excel</button>

 2.2 实现按钮请求

//导出
		function exportAll() {
			window.location.href="${pageContext.request.contextPath}/dudaopingjiao/excel";
		}

三、SSM操作

3.1 Controller 层

/**
	 * 导出excel
	 */
	@RequestMapping("/excel")
	@IgnoreAuth
	public void exportToExcel(HttpServletResponse response, DudaopingjiaoEntity dudaopingjiaoEntity) throws IOException {
		// 获取需要导出的数据
		List<DudaopingjiaoEntity> dataList = dudaopingjiaoService.Excel();

		// 创建Excel文件
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("督导评教数据导出表");

		// 创建表头
		HSSFRow headerRow = sheet.createRow(0);
		String[] headers = {"教师工号", "教师姓名", "学院","职称","任教课程","评分","评价内容","评价日期","督导账号","督导姓名"}; // 根据你的数据调整列名
		for (int i = 0; i < headers.length; i++) {
			HSSFCell headerCell = headerRow.createCell(i);
			headerCell.setCellValue(headers[i]);
		}

		// 填充数据
		for (int i = 0; i < dataList.size(); i++) {
			HSSFRow dataRow = sheet.createRow(i + 1);
			DudaopingjiaoEntity data = dataList.get(i);
			dataRow.createCell(0).setCellValue(data.getJiaoshigonghao()); // 假设YourData有getField1()方法
			dataRow.createCell(1).setCellValue(data.getJiaoshixingming()); // 假设YourData有getField2()方法
			dataRow.createCell(2).setCellValue(data.getXueyuan()); // 假设YourData有getField3()方法
			dataRow.createCell(3).setCellValue(data.getZhicheng()); // 假设YourData有getField3()方法
			dataRow.createCell(4).setCellValue(data.getRenjiaokemu()); // 假设YourData有getField3()方法
			dataRow.createCell(5).setCellValue(data.getPingfen()); // 假设YourData有getField3()方法
			dataRow.createCell(6).setCellValue(data.getPingjianeirong()); // 假设YourData有getField3()方法
			dataRow.createCell(7).setCellValue(data.getPingjiariqi()); // 假设YourData有getField3()方法
			dataRow.createCell(8).setCellValue(data.getDudaozhanghao()); // 假设YourData有getField3()方法
			dataRow.createCell(9).setCellValue(data.getDudaoxingming()); // 假设YourData有getField3()方法
			// 根据实际情况填充其他字段
		}

		// 设置响应类型和内容处置
		response.setContentType("application/vnd.ms-excel");
//		response.setHeader("Content-Disposition", "attachment; filename=学生评教数据导出表.xls");
		response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("学生评教数据导出表.xls", "UTF-8"));


		// 将Excel写入响应输出流
		try (ServletOutputStream outputStream = response.getOutputStream()) {
			workbook.write(outputStream);
		}

		// 关闭工作簿
		workbook.close();
	}

3.2 service 层

List<DudaopingjiaoEntity> Excel();

3.3 serviceImpl层

	@Override
	public List<DudaopingjiaoEntity> Excel() {
		return baseMapper.Excel();
	}

3.4 dao层

 List<DudaopingjiaoEntity> Excel();

3.5 mapper.xml

  <!--    查询所有数据-->
    <select id="Excel" resultMap="dudaopingjiaoMap">
        select * from dudaopingjiao
    </select>

四、搞定

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天的接口写完了吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值