Controller 层

/**
* 考勤机刷卡明细导出Excel
*
* @throws Exception
*/
// @RequiresPermissions("report:ExportToExcel")
@RequestMapping(value = "/AttendMachineCardDetailsExportToExcel", method = RequestMethod.POST)
public void attendMachineCardDetailsExportToExcel(@RequestParam(value = "workTimeDateStart", required = false) String workTimeDateStart,
@RequestParam(value = "workTimeDateEnd", required = false) String workTimeDateEnd,
@RequestParam(value = "userName", required = false) String userName,
@RequestParam(value = "userType", required = false) String userType,
HttpServletRequest request,
HttpServletResponse response) throws Exception { AttendMachineCard attendMachineCard = new AttendMachineCard();
attendMachineCard.setWorkTimeDateStart(workTimeDateStart);
attendMachineCard.setWorkTimeDateEnd(workTimeDateEnd);
attendMachineCard.setUserName(userName);
attendMachineCard.setUserType(userType);
List<AttendMachineCard> list = attendMachineCardServiceImpl.getAttendMachineCardList(attendMachineCard);//用于查询,返回结果
attendMachineCardServiceImpl.exportAttendMachineCardDetails(list, request, response);//导出Excle
}

ServiceImpl

@Service
public class AttendMachineCardServiceImpl implements AttendMachineCardService{
@Autowired
private AttendMachineCardMapper attendMachineCardMapper;
@Autowired
private SysLogMapper sysLogMapper;
@Override
public List<AttendMachineCard> getAttendMachineCardList(AttendMachineCard attendMachineCard) {
List<AttendMachineCard> attendMachineCardList = null;
try {
attendMachineCardList = attendMachineCardMapper.getAttendMachineCardList(attendMachineCard);
} catch (Exception e) {
throw new OutsideSystemServiceException(ResultEnum.SEARCH_ATTEND_MACHINE_CARD_ERROR,e);
}
return attendMachineCardList;
} @Override
public void exportAttendMachineCardDetails(List<AttendMachineCard> attendMachineCard, HttpServletRequest request, HttpServletResponse response) throws IOException {
// set content attributes for the response
try {
this.logexportLogDetails(request);
} catch (Exception e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=人员考勤信息统计表.xlsx");
// 创建工作薄
try (XSSFWorkbook workbook = new XSSFWorkbook();) {
// 创建工作表
XSSFSheet sheet = workbook.createSheet("人员考勤信息统计表");
createExcelContentDetails(attendMachineCard, sheet);
workbook.write(response.getOutputStream());
}
response.flushBuffer();
} private void createExcelContentDetails(List<AttendMachineCard> attendMachineCards,XSSFSheet sheet){
XSSFRow titleRow = sheet.createRow(0);
for (int col = 0; col < 7; col++) {
// 向工作表中添加数据
titleRow.createCell(col);
}
titleRow.getCell(0).setCellValue("姓名");
titleRow.getCell(1).setCellValue("人员类别");
titleRow.getCell(2).setCellValue("刷卡日期");
titleRow.getCell(3).setCellValue("刷卡时间");
titleRow.getCell(4).setCellValue("签到类型");
titleRow.getCell(5).setCellValue("签到方式");
titleRow.getCell(6).setCellValue("设备编号"); sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 30 * 256);
sheet.setColumnWidth(3, 30 * 256);
sheet.setColumnWidth(4, 30 * 256);
sheet.setColumnWidth(5, 30 * 256);
sheet.setColumnWidth(6, 30 * 256); // 数据行
for (int row = 1; row < attendMachineCards.size() + 1; row++) {
XSSFRow rows = sheet.createRow(row);
for (int col = 0; col < 7; col++) {
rows.createCell(col);
}
AttendMachineCard attendMachineCard = attendMachineCards.get(row - 1);
rows.getCell(0).setCellValue(attendMachineCard.getUserName());
rows.getCell(1).setCellValue(attendMachineCard.getUserType());
rows.getCell(2).setCellValue(attendMachineCard.getPunckDate());
rows.getCell(3).setCellValue(attendMachineCard.getPunckTime());
rows.getCell(4).setCellValue(attendMachineCard.getSignType());
rows.getCell(5).setCellValue(attendMachineCard.getWayOfSigningIn());
rows.getCell(6).setCellValue(attendMachineCard.getNumber());
}
}
}

Java导出Excel(项目实战Demo)的更多相关文章

  1. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  2. [转载]Java导出Excel

    一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...

  3. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  4. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  5. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

  6. java设计模式综合项目实战视频教程

    java设计模式综合项目实战视频教程 视频课程目录如下: 第01节课:本课程整体内容介绍:X-gen系统概况,包括:引入.X-gen项目背景.X-gen的HelloWorld第02节课:X-gen整体 ...

  7. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  8. java导出excel模板数据

    Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...

  9. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

随机推荐

  1. ubuntu 允许root用户登录到ssh

    ubuntu的系统太太太麻烦了,我喜欢centos,但是还是要用ubuntu做东西,讨厌,装完系统以后,因为他不让你用root,我新建了一个wqz的用户名. 1.首先更新root的密码 sudo pa ...

  2. web.xml中的welcome-file-list标签作用

    welcome-file-list是一个配置在web.xml中的一个欢迎页,用于当用户在url中输入项目名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面. ...

  3. P2774 方格取数(网络流)

    https://www.luogu.com.cn/problem/P2774 在一个有 m×n 个方格的棋盘中,每个方格中有一个正整数. 现要从方格中取数,使任意2个数所在方格没有公共边,且取出的数的 ...

  4. leetcode 0218

    目录 ✅ 1200. 最小绝对差 描述 解答 cpp py ✅ 897. 递增顺序查找树 描述 解答 cpp 指针问题? fuck ptr py ✅ 183. 从不订购的客户 描述 解答 sql to ...

  5. Verilog状态机

    以1011为例 代码如下: //1011(Meay型) module state1(clk,in,rst_n,out); input clk; input rst_n; input in; outpu ...

  6. 设计模式课程 设计模式精讲 3-3 开闭原则 coding

    1 课程讲解 1.1 开闭原则定义 1.2 不重要内容 2 代码coding 2.1 基类 2.2 需求:打印出原价和折扣后的价格.(接口不应该随意变化,面向接口编程) 1 课程讲解 1.1 开闭原则 ...

  7. OA:办公自动化———笔记一

    oa:办公自动化 1.对公司结构的管理 基础数据管理         部门进行管理     角色进行管理     权限进行管理  员工进行管理   2.流程管理          利用工作流技术对比较 ...

  8. C++ 知识零碎搭建

    全局变量 局部变量 函数不能嵌套定义 C/C++ 变量在将要被使用时定义即可, 不必一开始就声明所有变量 函数的定义与声明的区别 C++常规类型自动类型转换规则 C语言中十六进制和八进制的格式: 二进 ...

  9. tcp连接建立和断开

    TCP协议作为传输层主要协议之一,具有面向连接,端到端,可靠的全双工通信,面向字节流的数据传输协议. 1.TCP报文段 虽然TCP面试字节流,但TCP传输的数据单元却是报文段.TCP报文段分为TCP首 ...

  10. SystemVerilog for design 笔记(二)

    转载请标明出处 1. System Verilog文本值和数据类型 1.1. 增强的文本值赋值 相对于verilog,SV在文本值赋值时可以1.无需指定进制    2.赋值可以是逻辑1 用法: reg ...