java 实现导出excel列表

本文介绍了如何使用Java在controller和service层实现Excel数据的导出,包括具体的方法实现步骤。
摘要由CSDN通过智能技术生成

相关坐标

 <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
  </dependency> 
    
 <dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.9</version>
 </dependency>

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml-schemas</artifactId>
	<version>3.9</version>
</dependency>

1.controller层方法

   /**
     * 导出excel列表
     *
     * @param technologyDisplayRepair
     * @author xujj
     * @since 2020-06-10 09:40:07
     */
    @RequestMapping("exportToExcel")
    public void exportExcel(DisplayRepair technologyDisplayRepair, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            String title="展品报修列表";
            String downDate= DateUtil.ifType("yyyy-MM-dd", "", 0);
            response.setHeader(
                    "Content-disposition",
                    "attachment;" + "filename="
                            + GlobalFunc.toUtf8String(title+downDate+".xlsx"));
            response.setContentType("application/vnd.ms-excel;charset=utf8");
            ServletOutputStream output = response.getOutputStream();

            displayRepairService.exportListToExcel(technologyDisplayRepair, output, session);
            if(output != null){
                output.flush();
                output.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.service层方法

  /**
     * 导出列表
     *
     * @param technologyDisplayRepair
     * @param output
     * @param session
     * @throws Exception
     */
    public void exportListToExcel(DisplayRepair technologyDisplayRepair, ServletOutputStream output, HttpSession session) throws Exception {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet = wb.createSheet("展品报修列表"); // 工作表对象
        sheet = wb.getSheetAt(0);
        CellStyle style = wb.createCellStyle();
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直
        style.setAlignment(CellStyle.ALIGN_CENTER);// 水平
        Row nRow = null; // 行对象
        //Cell nCell = null; // 列对象

        //标题数组
        String[] titles = {"报修展品", "所属展厅", "报修人", "服务商","状态","更新时间"};
        //属性数组
        String[] props = {"displayName","roomName","createUserName","supplierName","repairStatus","updateTime"};
        nRow = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            sheet.setColumnWidth((short) i, (short) (30 * 200));
            Cell cell = nRow.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(style);
        }
        technologyDisplayRepair.setPageSize(-1);
        List<DisplayRepair> list = this.displayRepairMapper.queryAll(technologyDisplayRepair);
        if (list != null && list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                nRow = sheet.createRow(i + 1);
                nRow.setRowStyle(style);
                Cell cell = null;
                // 将对象转换成map
                Map<String,Object> map = JSON.parseObject(JSON.toJSONString(list.get(i)),Map.class);
                if (map.get("updateTime") != null){
                    String updateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(map.get("updateTime"));
                    map.put("updateTime",updateTime);
                }
                for (int j = 0; j < props.length; j++) {
                    cell = nRow.createCell(j);
                    if (map.get(props[j]) != null){
                        cell.setCellValue(map.get(props[j]).toString());
                    }

                }
            }
        }
        wb.write(output);
        output.close();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值