一、页面效果
二、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>
四、搞定