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 ...
随机推荐
- Euler Sums系列(三)
\[\Large\sum_{n=1}^{\infty}\frac{\left(H_{n}^{(2)}\right)^{2}}{n^{2}}=\frac{19}{24}\zeta(6)+\zeta^{2 ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 高级索引
import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print (y) import n ...
- Catalyst 3850 升级-1
Cisco Catalyst 3850交换机使用Cisco IOS XE软件. Cisco IOS XE软件是一个包含一组包文件的一个集合. 我们可以使用以下两种模式之一在Cisco Catalyst ...
- mcast_join函数
#include <errno.h> #include <string.h> #include <net/if.h> #include <netinet/in ...
- python列表操作方法详解
列表 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表是一个数据的集合,集合内可以放任何数据类型,可对集合方便的增删改查操作.Python已经内置确定序列的长度以及确 ...
- header头中 content-type的作用
- Ajax案例
展示页面jsp: <%@ page language="java" contentType="text/html; charset=utf-8" pa ...
- maven设置jdk版本
方法一:在maven文件夹下的settings.xml中添加 <profile> <id>jdk-1.8</id> <activation> <a ...
- 小白学 Python 爬虫:Selenium 获取某大型电商网站商品信息
目标 先介绍下我们本篇文章的目标,如图: 本篇文章计划获取商品的一些基本信息,如名称.商店.价格.是否自营.图片路径等等. 准备 首先要确认自己本地已经安装好了 Selenium 包括 Chrome ...
- 输入url到展示页面过程发生了什么?
输入网址,首先在书签或者历史记录里面去搜索相关的网址推荐给你 浏览器查找域名的IP的地址(在hosts文件有没有对应IP -> 浏览器发出一个DNS请求到本地DNS服务器,本地服务器一般是网 ...