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 ...
随机推荐
- Emprie 使用基础笔记
0x01 简介 empire 是一个后渗透攻击框架,具有加密通信和灵活框架的功能.Empire可以在不需要Powershell.exe的情况下执行PowerShell代理,后期利用的模块很强大,如sc ...
- 13. 为什么我们会需要 Pod?
13. 为什么我们会需要 Pod? 13.1 docker容器的本质 """ docker容器的本质 是进程. 主要通过 Namespace 做隔离,Cgroups 做限 ...
- P1157 组合的输出
P1157 组合的输出 #include <bits/stdc++.h> using namespace std; int n,r; int a[25]; vector<int> ...
- 一个支持国密SM2/SM3/SM4/SM9/ZUC/SSL的密码工具箱
转:https://blog.csdn.net/xuq09/article/details/91815366 The GmSSL Project网址:http://gmssl.org/docs/qui ...
- Oracle的TO_CHAR()格式化数字为百分数的字符串
TO_CHAR(-0.926903249,'FM999999990.00') || '%' 说明: 1,(点号) :'99.99' : 点号,不要念为"句号",句号是个圆圈,点号只 ...
- liux 防火墙以及开关
一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙. 开启防火墙: [root@centos6 ...
- php 基础 获取远程连接
1 file_get_contents get $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=> ...
- php 算法知识 冒泡排序
function bubble_order($arr){ //得到长度 $count_num=count($arr); for($k=1;$k<$count_num;$k++){ //对长度越来 ...
- golang数据库操作初体验
在golang中,提供了标准的数据库接口database/sql包,做过数据库开发的应该知道,不同的数据库有不同的数据库驱动.比如mysql等,我们可以去找 https://golang.org/s/ ...
- Python - unittest打印成功信息
参考 https://stackoverflow.com/questions/36834677/print-success-messages-for-asserts-in-python 总结 clas ...