springboot实现根据查询条件导出excel

首先添加pom文件

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

controller层

@RestController
@RequestMapping("/adminApi/excel")
public class ExcelController {

    @Resource
    private ExcelService excelService;

    /*
     *导出用户信息
     * 2018/10/30+
     * dayue
     */
    @RequestMapping(value = "/export")
    public void downloadAllClassmate(@RequestParam(value = "status") String status,
                                     @RequestParam(value = "sex") String sex, @RequestParam(value = "type") String type,
                                     @RequestParam(value = "firstTime") String firstTime, @RequestParam(value = "lastTime") String lastTime,
                                     HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("用户信息表");
        
        //查询出的数据
        List<ExcelUser> classmateList = excelService.selectUser(status,sex,type,firstTime,lastTime);
         
        //文件名称  必须用英文
        String fileName = "User"  + ".xls";//设置要导出的文件的名字
        //新增数据行,并且设置单元格数据

        int rowNum = 1;
        
        //根据实际情况编写列表名(顺序必须一一对应)
        String[] headers = {"姓氏", "名字"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
         
        //查询出来的数据可能为null这种看着很不舒服  将null变为空
         String lastName = null;
         String firstName = null;

        //在表中存放查询到的数据放入对应的列
        for (ExcelUser excelUser : classmateList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            //判断lastName是否为null  若为null 给空  若不为null 传相应的值
            if (excelUser.getLastName() == null){
                lastName = "";
            }else {
                lastName = excelUser.getLastName();
            }
            row1.createCell(0).setCellValue(lastName);
            //同上
            if (excelUser.getFirstName() == null){
                firstName = "";
            }else {
                firstName = excelUser.getFirstName();
            }
            row1.createCell(1).setCellValue(firstName);
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

Service层

List<ExcelUser> selectUser(String status,String sex, String type, String firstTime, String lastTime);

ServiceImp层

@Override
    public List<ExcelUser> selectUser(String status,String sex, String type, String firstTime, String lastTime){
        return  bpUserMapper.findUser(status,sex,type,firstTime,lastTime);
    }

sql就是根据你查询条件写一个sql语句  就不写了

最后重点  不能使post请求  用get或者链接请求  否则会返回乱码

 

可以使用 Apache POI 库实现将 MySQL 查询结果导出Excel 文件中。下面是一个简单的示例代码: 1. 添加 Maven 依赖 ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建 Excel 文件并写入数据 ```java // 创建工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 执行查询并获取结果集 List<Object[]> resultList = jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<>(clazz)); // 写入表头 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } // 写入数据 int rowIndex = 1; for (Object[] row : resultList) { Row dataRow = sheet.createRow(rowIndex++); for (int i = 0; i < row.length; i++) { Cell cell = dataRow.createCell(i); cell.setCellValue(row[i].toString()); } } // 输出 Excel 文件 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); ``` 其中,`jdbcTemplate` 是 Spring 提供的 JDBC 操作工具类,`sql` 是要执行的 SQL 语句,`args` 是 SQL 语句中的参数,`clazz` 是查询结果对应的实体类,`headers` 是表头数组,`fileName` 是导出Excel 文件名,`response` 是 HttpServletResponse 对象。 以上代码将查询结果写入 Excel 文件并直接输出到浏览器。你可以根据实际需求自行修改。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值