Java导出Excel(项目实战Demo)
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)的更多相关文章
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- [转载]Java导出Excel
一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
- java导出excel表格
java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...
- java导出excel报表
1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...
- java设计模式综合项目实战视频教程
java设计模式综合项目实战视频教程 视频课程目录如下: 第01节课:本课程整体内容介绍:X-gen系统概况,包括:引入.X-gen项目背景.X-gen的HelloWorld第02节课:X-gen整体 ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- java导出excel模板数据
Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...
- java导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
随机推荐
- Card Game for Three
Alice, Bob and Charlie are playing Card Game for Three, as below: At first, each of the three player ...
- DC-DC芯片
KIS-3R33S:同步整流7V-24V转5V/3A DC-DC降压模块 稳压电源模块 效率超过LM2576
- Pipenv & 虚拟环境
本教程将引导您完成安装和使用 Python 包. 它将向您展示如何安装和使用必要的工具,并就最佳做法做出强烈推荐.请记住, Python 用于许多不同的目的.准确地说,您希望如何管理依赖项可能会根据 ...
- 多项式输出 (0)<P2009_1>
多项式输出 (poly.pas/c/cpp) [问题描述] 一元n次多项式可用如下的表达式表示: 其中,称为i次项,ai称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输 ...
- laravel 左联报错问题
- Java通过反射实现实例化
public static void main(String[] args) throws Exception { User user= (User) test(User.class); System ...
- BugkuCTF解题Web基础(一)
Web2 打开链接看见一张动图,猜测flag应该就在网页前端源码里面 没有问题 计算器 典型的修改前端代码题目,题目让你计算结果,但只能填写一位数字. f12打开控制台,改代码maxlength we ...
- 十 Spring的AOP的底层实现:JDK动态代理和Cglib动态代理
SpringAOP底层的实现原理: JDK动态代理:只能对实现了接口的类产生代理.(实现接口默认JDK动态代理,底层自动切换) Cglib动态代理(类似Javassist第三方的代理技术):对没有实现 ...
- LeetCode 445. Add Two Numbers II(链表求和)
题意:两个非空链表求和,这两个链表所表示的数字没有前导零,要求不能修改原链表,如反转链表. 分析:用stack分别存两个链表的数字,然后从低位开始边求和边重新构造链表. Input: (7 -> ...
- sklearn中调用集成学习算法
1.集成学习是指对于同一个基础数据集使用不同的机器学习算法进行训练,最后结合不同的算法给出的意见进行决策,这个方法兼顾了许多算法的"意见",比较全面,因此在机器学习领域也使用地非常 ...