ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

ssm怎么导出数据库表为excel

2021-05-08 18:00:18  阅读:191  来源: 互联网

标签:表为 cell appoint excel ssm getAppoint createCell setCellValue row


非常直白,请君自行饮用

//下载excel
    @RequestMapping("/downloadAllAppoint")
    @ResponseBody
    public String reprotRecord(HttpServletResponse response) throws IOException {
        // 文件名称
        String fileName = URLEncoder.encode("刷卡记录.xlsx", "utf-8");
        // 通过response设置Content-Type、Content-Disposition
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename*=utf-8'zh_cn'" + fileName);

        //生成workBook
        //	HSSFWorkbook workbook = createWorkbook();
        OutputStream outputStream = null;
        HSSFWorkbook workBook = null;

        try {
            // 获取输出流
            outputStream = response.getOutputStream();
            // 生成workBook
            workBook = downloadAllAppoint();
            workBook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            if (outputStream != null) {
                outputStream.close();
            }
        }
        return null;
    }
    public HSSFWorkbook downloadAllAppoint() throws IOException {
        ///创建个空白的workbook[name:workbook]
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建个空白的sheet[name:appoint]
        HSSFSheet sheet = workbook.createSheet("appoint");
        //行
        HSSFRow row;
        //单元格
        HSSFCell cell;
        //创建行循环标签
        int i = 0;
        //创建行
        row = sheet.createRow(i);
        //列名标柱
        cell = row.createCell(0);
        cell.setCellValue("预约编号");
        cell = row.createCell(1);
        cell.setCellValue("预约用户账号");
        cell = row.createCell(2);
        cell.setCellValue("用户名");
        cell = row.createCell(3);
        cell.setCellValue("手机号");
        cell = row.createCell(4);
        cell.setCellValue("预约项目");
        cell = row.createCell(5);
        cell.setCellValue("预约时间");
        cell = row.createCell(6);
        cell.setCellValue("预约状态");

        //查询所有预约信息
        List<Appoint> appointList = appointService.selectAllAppoint();
        //开始循环

        for (Appoint appoint : appointList) {
            i++;
            row = sheet.createRow(i);
            cell = row.createCell(0);
            cell.setCellValue(appoint.getAppoint_id());
            cell = row.createCell(1);
            cell.setCellValue(appoint.getAppoint_userid());
            cell = row.createCell(2);
            cell.setCellValue(appoint.getAppoint_username());
            cell = row.createCell(3);
            cell.setCellValue(appoint.getAppoint_phonenum());
            cell = row.createCell(4);
            cell.setCellValue(appoint.getAppoint_project());
            cell = row.createCell(5);
            cell.setCellValue(appoint.getAppoint_time());
            cell = row.createCell(6);
            cell.setCellValue(appoint.getAppoint_status());
        }
        System.out.println("orderCondition.xls--[status:success]!");
        return workbook;
    }

标签:表为,cell,appoint,excel,ssm,getAppoint,createCell,setCellValue,row
来源: https://blog.csdn.net/weixin_45095712/article/details/116536189

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有