poi实现导出报表到excel功能

本文详细介绍了一种使用Java POI库导出Excel文件的方法,包括控制层、业务层、数据访问层的设计与实现,以及具体的SQL语句。通过实例展示了如何创建Excel工作簿、设置样式、填充数据并输出到客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.控制层

controller(控制层)

/**
     * 写一个导出excel的功能
     */
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletResponse response) {
        response.setContentType("application/binary;charset=UTF-8");            
        String[] titles = { "角色", "描述", "审核状态", "审核时间","最后更新人","最后更新时间" };
        System.out.println(titles);
        system_RoleService.export(titles);

    }

2.业务层及业务实现层

service(接口层)

/**
     * @return 
     * 
    * @Title: export 
    * @Description: 导出excel(这里用一句话描述这个方法的作用) 
    * @param @param titles    设定文件 
    * @return void    返回类型 
    * @throws
     */
    void export(String[] titles);

serviceimpl(业务实现层)

/**
     * 导出excel
     */
    public void export(String[] titles) { 
           
            try {
                // 创建excel文件
                HSSFWorkbook workbook = new HSSFWorkbook();
                // 建立新的sheet对象
                HSSFSheet hssfSheet = workbook.createSheet("sheet1");
                // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
                HSSFRow hssfRow = hssfSheet.createRow(0);
                // 第四步,创建单元格,并设置值表头 设置表头居中
                HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
                // 居中样式
                hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                HSSFCell hssfCell = null;
                for (int i = 0; i < titles.length; i++) {
                    hssfCell = hssfRow.createCell(i);// 列索引从0开始
                    hssfCell.setCellValue(titles[i]);// 列名1
                    hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
                }
                // 第五步,写入实体数据
                List<System_Role> role = system_RoleDao.export();
                if (role != null && !role.isEmpty()) {
                    for (int i = 0; i < role.size(); i++) {
                        hssfRow = hssfSheet.createRow(i + 1);
                        System_Role System_Role = role.get(i);
                        // 第六步,创建单元格,并设置值
                        String name = "";
                        if (System_Role.getName() != null) {
                            name = System_Role.getName();
                        }
                        hssfRow.createCell(0).setCellValue(name);
                        
                        String description = "";
                        if (System_Role.getDescription() != null) {
                            description = System_Role.getDescription();
                        }
                        hssfRow.createCell(1).setCellValue(description);


                        int audit_state = 0;
                        if (System_Role.getAudit_state() != 0) {
                            audit_state = System_Role.getAudit_state();
                        }
                        hssfRow.createCell(2).setCellValue(audit_state);


                        String audit_date_time = null;
                        if (System_Role.getAudit_date_time() != null) {
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                            audit_date_time =formatter.format(System_Role.getAudit_date_time());
                        }
                        hssfRow.createCell(3).setCellValue(audit_date_time);
                        
                        String last_updated_user_name = "";
                        if(System_Role.getLast_updated_user_name() != null){
                            last_updated_user_name =System_Role.getLast_updated_user_name();
                        }
                        hssfRow.createCell(4).setCellValue(last_updated_user_name);
                        
                        String last_updated_date_time = "";
                        if(System_Role.getLast_updated_date_time() !=null){
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                            last_updated_date_time =formatter.format(System_Role.getLast_updated_date_time());
                        }
                        hssfRow.createCell(5).setCellValue(last_updated_date_time);
                    }
                }


                // 第七步,将文件输出到客户端浏览器 || 使用下载
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.xls");// 指定路径与名字和格式
                    workbook.write(fileOutputStream);// 将数据写出去
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

3.dao(接口层)

/**
     * 
    * @Title: export 
    * @Description: 导出excel(这里用一句话描述这个方法的作用) 
    * @param @param titles    设定文件 
    * @return void    返回类型 
    * @throws
     */
     List<System_Role>  export();

4.mapper(映射)

<!-- 导出的SQL语句 -->
<select id="export" resultType="com.znkj.entity.System_Role">
 select name,description,
 audit_state,audit_date_time,last_updated_user_name, 
 last_updated_date_time from system_role
</select>
5.总结
 
poi导出功能相对简单,需要注意的是导出路径一定要选对

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值