当前位置: 首页 > article >正文

poi-生成excel文档并返还给浏览器

处理请求代码

public void exportExcel(HttpServletRequest req, HttpServletResponse resp) throws IOException {//1.设置响应类型resp.setContentType("application/vnd.ms-excel");resp.setHeader("Content-Disposition","attachment;filename=\"duty.xls\"");//2.获取请求参数//3.得到响应数据DutyService service = new DutyServiceImpl();List<Duty> duties = service.selectAll();//4.做出响应ArrayList<String> cols = new ArrayList<>();cols.add("用户名");cols.add("真实姓名");cols.add("所属部门");cols.add("出勤日期");cols.add("签到时间");cols.add("签退时间");ExcelUtil.exportExcel(duties,cols,resp.getOutputStream());}

导出excel代码

public static void  exportExcel(List<Duty> data, List<String> colNames, OutputStream outputStream ){// 创建一个Excel文件HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个工作表HSSFSheet sheet = workbook.createSheet("考勤信息表");// 添加表头行HSSFRow hssfRow = sheet.createRow(0);// 设置单元格格式居中HSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setAlignment(HorizontalAlignment.CENTER);// 添加表头内容for(int i=0; i<colNames.size();i++){HSSFCell headCell = hssfRow.createCell(i);headCell.setCellValue(colNames.get(i));headCell.setCellStyle(cellStyle);}// 添加数据内容for (int i = 0; i < data.size(); i++) {hssfRow = sheet.createRow((int) i + 1);Duty duty = data.get(i);// 创建单元格,并设置值HSSFCell cell = hssfRow.createCell(0);cell.setCellValue(duty.getEmprid());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(1);cell.setCellValue(duty.getEmployee().getRealname());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(2);cell.setCellValue(duty.getDept().getDeptname());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(3);cell.setCellValue(duty.getDtdate());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(4);cell.setCellValue(duty.getSignintime());cell.setCellStyle(cellStyle);cell = hssfRow.createCell(5);cell.setCellValue(duty.getSignouttime());cell.setCellStyle(cellStyle);}// 保存Excel文件try {workbook.write(outputStream);outputStream.close();} catch (Exception e) {e.printStackTrace();}}
http://www.lryc.cn/news/2417133.html

相关文章:

  • python排序算法 ——冒泡排序(附代码)
  • 微机原理小题知识点总结
  • 计算机网络概述(入门篇)
  • Windows API大全
  • printStackTrace()方法在exception中的理解
  • Python:打包生成.pyc、.pyd文件
  • Secure CRT8.1.3 64位安装及注册激活
  • 什么是XHTML,XHTML学习笔记
  • linux基础IO——用户缓冲区——概念深度探索、IO模拟实现
  • kotlin基础 变量,函数 1
  • 虚拟内存、物理内存与OOM Killer
  • SurfaceView和View区别总结
  • html table th分层,html中 table的结构 彻底搞清 caption th thead等
  • tsearch, tfind, tdelete, twalk, tdestr函数—标准树
  • 『TopCoder 组件开发指南』
  • 创建共享网盘、访问共享网盘
  • ubuntu切换软件源为国内源
  • windows 2000 系统安装和配置
  • 修改Win10右键菜单
  • 机器学习——RBF神经网络
  • 信息增益与信息增益率详解
  • 11个超高清图片素材网站,可直接访问
  • oVirt4.4单台主机Allinone部署(非HostedEngine方式)
  • 用户登录模块
  • 为什么要进行透明计算和透明计算是什么
  • UDP协议的两个主要方法sendto和recvfrom详解
  • 猫头虎分享已解决Bug || 代理服务器错误(Proxy Server Error):ProxyFailure, ProxyConnectionRefused
  • 【虚幻引擎】UE4 同步和异步资源加载(软引用)
  • Android设备唯一标识(终极方案!)
  • weak reference的介绍