JAVA导出查询结果到Excel

页面数据

 后端代码

 @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request,  HttpServletResponse response) throws UnsupportedEncodingException {
        //一、从后台拿数据
        List<Stuinfo> list = stuinfoService.findStuinfo();
        //二、 数据转成excel
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = "学生信息.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="  + fileName);
        // 第一步:定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步:创建一个Sheet页
        XSSFSheet sheet = wb.createSheet("学生信息");
        sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
        sheet.setColumnWidth(0,3000);//设置列宽
        sheet.setColumnWidth(1,3000);
        sheet.setColumnWidth(2,3000);
        sheet.setColumnWidth(3,8000);
        sheet.setColumnWidth(4,7000);
        sheet.setColumnWidth(5,7000);
        sheet.setColumnWidth(6,7000);
      //表头样式
        XSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBold(true);
        headFont.setFontHeightInPoints((short) 11);
        XSSFCellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);// 设置字体css
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 竖向居中
        headStyle.setAlignment(HorizontalAlignment.CENTER);      // 横向居中
         headStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //数据样式
        XSSFFont font = wb.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 11);
        XSSFCellStyle style = wb.createCellStyle();
        style.setFont(font); // 设置字体css
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 竖向居中
        style.setAlignment(HorizontalAlignment.CENTER); // 横向居中
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        String[] heads={"姓名 ","性别","年龄","身份证","手机号 ","邮箱 ","地址"};
        for(int i=0;i<heads.length;i++){
              cell = row.createCell(i);
              cell.setCellValue(heads[i]);
               cell.setCellStyle(headStyle);
        }
        XSSFRow rows;
        XSSFCell cells;
        for (int i = 0; i < list.size(); i++) {
            // 第三步:在这个sheet页里创建一行
            rows = sheet.createRow(i+1);
            // 第四步:在该行创建一个单元格
            cells = rows.createCell(0);
            // 第五步:在该单元格里设置值
            cells.setCellValue(list.get(i).getName());
            cells.setCellStyle(style);
            cells = rows.createCell(1);
            cells.setCellValue(list.get(i).getSex());
            cells.setCellStyle(style);
            cells = rows.createCell(2);
            cells.setCellValue(list.get(i).getAge());
            cells.setCellStyle(style);
            cells = rows.createCell(3);
            cells.setCellValue(list.get(i).getCard());
            cells.setCellStyle(style);
            cells = rows.createCell(4);
            cells.setCellValue(list.get(i).getTel());
            cells.setCellStyle(style);
            cells = rows.createCell(5);
            cells.setCellValue(list.get(i).getEmail());
            cells.setCellStyle(style);
            cells = rows.createCell(6);
            cells.setCellValue(list.get(i).getAddress());
            cells.setCellStyle(style);
        }
        try {
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
            wb.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

POM依赖

<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>

Excel表格

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值