导出excel

该篇文章详细描述了如何使用Java和ApachePOI库在SpringMVC框架中,通过HTTP请求处理导出咨询数据到Excel文件的过程,包括数据查询、表格创建、单元格样式设置以及文件响应头的设置。
摘要由CSDN通过智能技术生成
    @GetMapping("/export")
    public void export(Consult consult, HttpServletResponse response) {
        consultService.export(consult,response);
    }


    //导出excel
    @Override
    public void export(Consult consult, HttpServletResponse response) {
        //目标数据
        Page<Consult> consultPage = selectPage(1, 1000, consult);
        List<Consult> list = consultPage.getRecords();
        //创建一个工作铺
        XSSFWorkbook workbook=new XSSFWorkbook();
        //创建一个sheet
        XSSFSheet sheet = workbook.createSheet("sheet名称");
        String[] heads={"序号","标题","内容","删除","创建日期","修改日期"};
        //背景颜色
        XSSFColor color = new XSSFColor(new java.awt.Color(0, 0, 255));
        //单元格样式
        XSSFCellStyle headStyle = getHeadStyleWithBorder(workbook, color, "宋体", (short) 12, true);
        //表头行
        XSSFRow headRow = sheet.createRow(0);
        //首行添加内容
        for (int i = 0; i < heads.length; i++) {
            //创建headRow首行单元格
            XSSFCell cell = headRow.createCell(i);
            cell.setCellValue(heads[i]);
            cell.setCellStyle(headStyle);
        }

        //contentStyle内容单元格样式
        XSSFCellStyle contentStyle = getContentStyleWithBorder(workbook);

        for (int i = 0; i < list.size(); i++) {
            //获取对象
            Consult record = list.get(i);
            //创建行,从第二行开始
            XSSFRow row = sheet.createRow(i + 1);

            //创建单元格设置+样式
            //序号
            XSSFCell cell0 = row.createCell(0);
            cell0.setCellValue(i+1);
            cell0.setCellStyle(contentStyle);
            //标题
            XSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(record.getTitle());
            cell1.setCellStyle(contentStyle);
            //内容
            XSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(record.getContent());
            cell2.setCellStyle(contentStyle);
            //删除
            XSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(record.getDeleted());
            cell3.setCellStyle(contentStyle);
            //创建日期
            XSSFCell cell4 = row.createCell(4);
            cell4.setCellValue(getDateStr(record.getCreateTime()));
            cell4.setCellStyle(contentStyle);
            //修改日期
            XSSFCell cell5 = row.createCell(5);
            cell5.setCellValue(getDateStr(record.getUpdateTime()));
            cell5.setCellStyle(contentStyle);
        }
        //设置1、3列列宽
        sheet.setColumnWidth(1,3000);
        sheet.setColumnWidth(2,7000);
        exportToWeb(response,workbook,"信息表");
    }


    //表头单元格类型
    public  static XSSFCellStyle getHeadStyleWithBorder(XSSFWorkbook workbook, XSSFColor color,String word,Short wordSize,Boolean isBold) {
        XSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setAlignment(HorizontalAlignment.CENTER);
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headStyle.setWrapText(true);
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headStyle.setBorderBottom(BorderStyle.THIN);
        headStyle.setBorderLeft(BorderStyle.THIN);
        headStyle.setBorderRight(BorderStyle.THIN);
        headStyle.setBorderTop(BorderStyle.THIN);
        headStyle.setFillForegroundColor(color);
        XSSFFont font = workbook.createFont();
        font.setFontName(word);
        font.setFontHeightInPoints(wordSize);
        font.setBold(isBold);
        headStyle.setFont(font);
        return headStyle;
    }

    //内容单格类型
    public static XSSFCellStyle getContentStyleWithBorder(XSSFWorkbook workbook) {
        XSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setAlignment(HorizontalAlignment.CENTER);
        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentStyle.setBorderBottom(BorderStyle.THIN);
        contentStyle.setBorderLeft(BorderStyle.THIN);
        contentStyle.setBorderRight(BorderStyle.THIN);
        contentStyle.setBorderTop(BorderStyle.THIN);
        contentStyle.setWrapText(true);
        return contentStyle;
    }

    //导出设置
    public void exportToWeb(HttpServletResponse response, XSSFWorkbook workbook, String name){
        OutputStream out = null;
        try {
            response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
//            log.error("导出"+name+"失败", e);
        }finally {
            IOUtils.closeQuietly(out);
        }
    }

依赖

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值