Java导出Excel(项目实战Demo)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java导出Excel(项目实战Demo),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3860字,纯文字阅读大概需要6分钟。
内容图文
![Java导出Excel(项目实战Demo)](/upload/InfoBanner/zyjiaocheng/648/79fcd9ce194f4d51ab200d64b7e128c7.jpg)
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(项目实战Demo)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。