导入jar包

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

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

代码


    @Override
    public void export(HttpSession session, HttpServletRequest req, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        response.setContentType("octets/stream");

        try {
            String fileName = "生成的文件名";
            fileName = new String(fileName.getBytes(), "ISO-8859-1");
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + fileName + ".xls");
            OutputStream out = response.getOutputStream();
            startTime = startTime.replace("-","");

            String headers [] = {"第一列","第二列"};
            String values [] = {"字段1","字段2"};
	    List<UserDto> data = new ArralList<>();
            HSSFWorkbook wb = generateNewInDetailsExcelFile(data,headers,values, UserDto.class);

            wb.write(out);
            out.flush();
            out.close();
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(out!=null) {
                    out.close();
                }
                if(wb!=null) {
                    wb.close();
                }
            } catch (Exception e2) {

            }
        }

    }
    /**
     * 生成文件
     * @param details
     * @param headers
     * @param values
     * @param user
     * @param entity
     * @param <T>
     * @return
     */
    private static <T> HSSFWorkbook generateNewInDetailsExcelFile(List<T> details,String[] headers,String[] values,Class entity) {
        /**log4j日志*/
        Logger logger = LoggerFactory.getLogger(User);

        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = workbook.createSheet("sheet1");
        // 第三步,在sheet中添加表头第0行
        HSSFRow row = sheet.createRow(0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = workbook.createCellStyle();

        boolean headFlag = creatXlsFileHead(row,style,headers,logger);


        try {
            // 第五步,写入实体数据
            for (int i = 0; i < details.size(); i++) {
                row = sheet.createRow(i + 1);
                // 第六步,创建单元格,并设置值
                T t = details.get(i);
                for(int j = 0;j < values.length; j++) {
                    Field tValue = entity.getDeclaredField(values[j].trim());
                    tValue.setAccessible(true);
                    row.createCell(j).setCellValue((String)tValue.get(t));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("生成导出xls文件--生成xls文件内容失败,失败原因:" + e.getMessage());
        }

        return workbook;
    }

    /**
     * 生成xls文件表头
     * @param row
     * @param style
     * @param headers
     * @param logger
     * @return
     */
    private static boolean creatXlsFileHead(HSSFRow row,HSSFCellStyle style,String[] headers,Logger logger) {

        logger.info("生成导出xls文件--生成xls文件表头,接收到参数:headers" + headers.toString());
        try {
            for(int i=0;i<headers.length;i++) {
                row.createCell(i).setCellValue(headers[i].trim());
                //设置表头居中
                row.getCell(i).setCellStyle(style);
            }
        } catch (Exception e) {
            logger.info("生成导出xls文件--生成xls文件表头失败,失败原因:" + e.getMessage());
        }

        logger.info("创建表头完成");

        return true;
    }