Java实现Excel导出 --- POI

1.引入依赖 org.apache.poi

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

2.底层实现

    /**
     * 导出excel
     * @param os
     * @param sheetName  文件名
     * @param header     excel表头,使用逗号分隔,如:姓名,性别,年龄
     * @param data       导出数据
     * @throws Exception
     */
    public static void export_excel(OutputStream os,String sheetName, String header, List<HashMap<String,Object>> data) throws Exception{

        //工作簿
        Workbook wk = new HSSFWorkbook();
        Sheet sheet = wk.createSheet(sheetName);
        // 设置列宽
        sheet.setColumnWidth(0, 5000);
        //创建一行,参数指的是: 行的索引=行号-1
        Row row = sheet.createRow(0);
        if(header == null || header==""){
            return;
        }
        String[] headers = header.split(",");
        for(int i = 0; i < headers.length; i++){
            //设置表头
            row.createCell(i).setCellValue(headers[i]);
        }

        //创建单元格, 参数指的是:列的索引,从0开始
        //输出每一条记录
        if(null != data && data.size() > 0) {
            for (int i = 1; i <= data.size(); i++) {
                row = sheet.createRow(i);
                HashMap<String,Object> excel_data = data.get(i-1);
                row.createCell(0).setCellValue( excel_data.get("last_push_time").toString());
                row.createCell(1).setCellValue( (String)excel_data.get("order_id") );
                row.createCell(2).setCellValue( (long)excel_data.get("player_id") );
                //...
            }
        }

        //可直接在本地生成 .xls 文件
        //String pathname = "D://"+sheetName+".xls";
        //wk.write(new FileOutputStream(new File(pathname)));

        //输出到输出流中
        try {
            wk.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                wk.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

通过使用枚举实现excel信息的动态存储:
这里写图片描述

3.controller层

    @ResponseBody
    @RequestMapping(value="html/export_excel.do", method=RequestMethod.GET)
    public HashMap<String,Object> exportExcel(HttpServletResponse response,HttpServletRequest request) throws Exception{
        HashMap<String,Object> result = new HashMap<>();

        try {
            HttpSession session = request.getSession();
            List<HashMap<String,Object>> data = (List<HashMap<String,Object>>)session.getAttribute("game_daily_data");
            String filename = "每日数据统计.xls";
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    new String(filename.getBytes(),"ISO-8859-1"));
            ExportExcel.export_excel(response.getOutputStream(),"game_daily",ExcelSheet.GAME_DAILY.getSheetName(), ExcelSheet.GAME_DAILY.getHeaders(),data);
        } catch (Exception e) {
             e.printStackTrace();
        }
        return result;
    }

4.前端实现
前端可直接通过a标签的download属性实现下载

<a href="/demo/html/export_excel.do" download="每日数据统计.xls">导出Excel</a>

猜你喜欢

转载自blog.csdn.net/qq_36526703/article/details/81635405