【以视图的方式返回导出excel】

文章展示了如何在JavaSpringMVC应用中使用注解和特定库来导出Excel文件,包括多个sheet。`Student`类定义了导出的字段,`studentExcel`方法处理学生表的导出,而`exportXls`方法则创建了包含学生和班级信息的两个不同sheet。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以视图的方式返回导出excel

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;

@Data
@ApiModel(value = "Student对象", description = "学生表")
public class Student{

 
    @Excel(name = "姓名", width = 35 )
    @ApiModelProperty(value = "姓名")private String name;

    @Excel(name = "年龄", width = 22)
    @ApiModelProperty(value = "年龄")
    private String age;

    @Excel(name = "班级", width = 22)
    @ApiModelProperty(value = "班级")
    private String class;
  /**
    @Excel(name = "状态", groupName = "学习状态")//groupName 合并单元格
    @ApiModelProperty(value = "状态")
    private String status;
    @Excel(name = "学习表现", groupName = "学习状态")//groupName 合并单元格
    @ApiModelProperty(value = "学习表现")
    private String expression;
    **/

}
import org.springframework.web.servlet.ModelAndView;
 @AutoLog(value = "学生表-导出")//日志
    @ApiOperation(value = "学生表-导出")//
    @RequiresRoles(value ={"",""}, logical = Logical.OR)//角色限制
    @GetMapping(value = "/studentExcel")
    public ModelAndView studentExcel() {
        List<Student> bfcEnterVos = studentService.scrapSummary();
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        ExportParams exportParams = new ExportParams();
     
        exportParams.setStyle(SignStyle.class);

        mv.addObject(NormalExcelConstants.FILE_NAME, "学生表数据");//导出扩展名为.xls
        mv.addObject( NormalExcelConstants.CLASS,Student.class);
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, bfcEnterVos);

        return mv;
    }

在这里插入图片描述

excel导出两个sheet

public ModelAndView exportXls() {
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());

    List<Student> bfcEnterVos = studentService.scrapSummary();
    List<Map<String, Object>> list = new ArrayList<>();
    //创建sheet1使用得map
     Map<String, Object> peopleExportMap = new HashMap<>();
     ExportParams exportParams = new ExportParams();
    exportParams.setSheetName("学生表");

    peopleExportMap.put(NormalExcelConstants.CLASS, Student.class);
    peopleExportMap.put(NormalExcelConstants.PARAMS, exportParams);
    peopleExportMap.put(NormalExcelConstants.DATA_LIST, bfcEnterVos);
    list.add(peopleExportMap);
    List<Class> subsEnters = classService.getClass();
    // 创建sheet2使用得map
    Map<String, Object> selectdatassExportMap = new HashMap<>();
     ExportParams exportParamsVo = new ExportParams();
    exportParamsVo.setSheetName("班级表");

    selectdatassExportMap.put(NormalExcelConstants.CLASS, Class.class);
    selectdatassExportMap.put(NormalExcelConstants.PARAMS, exportParamsVo);
    selectdatassExportMap.put(NormalExcelConstants.DATA_LIST, subsEnters );
    list.add(selectdatassExportMap);
    
    mv.addObject(NormalExcelConstants.FILE_NAME, "汇总表");
    mv.addObject(NormalExcelConstants.MAP_LIST, list);
    return mv;
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值