若依实现主子表导出 (导入不会)

本文介绍了如何使用Java技术,通过API实现对自由裁量库(左)列表的查询,并利用ExcelUtil将结果导出为Excel文件,同时处理了子表(右)数据的功能。
摘要由CSDN通过智能技术生成
 /**
     * 导出自由裁量库(左)列表
     */
    @PreAuthorize("@ss.hasPermi('enforceLaw:basePunishType:export')")
    @Log(title = "自由裁量库(左)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LawBasePunishType lawBasePunishType)
    {
        List<LawBasePunishType> list = lawBasePunishTypeService.selectLawBasePunishTypeList(lawBasePunishType);
        List<LawBasePunishType> resultList = new ArrayList<>(); // 创建用于存储查询结果的集合
        for(LawBasePunishType item : list){
            LawBasePunishType result = lawBasePunishTypeService.selectLawBasePunishTypeById(item.getId());
            result.setLawBasePunishDataList(result.getLawBasePunishDataList()); // 设置查询到的子表数据到主表对象中
            resultList.add(result); // 将查询结果添加到结果集合中

        }
        ExcelUtil<LawBasePunishType> util = new ExcelUtil<LawBasePunishType>(LawBasePunishType.class);
        util.exportExcel(response, resultList, "自由裁量库(左)数据");
    }
 /** 自由裁量库种类(右)信息 */
    @Excel(name = "子表数据")
    private List<LawBasePunishData> lawBasePunishDataList;

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
若依框架提供了方便的主子数据导入导出功能。具体步骤如下: 1.在实体类中使用@Excel注解标注需要导入导出的字段,同时使用@ExcelCollection注解标注子表对应的集合字段。 2.在Controller中使用EasyExcel工具类进行导入导出操作。具体代码如下: ```java // 导出主子数据 @RequestMapping("/export") public void export(HttpServletResponse response) throws IOException { // 查询数据 List<MainTable> list = mainTableService.getList(); // 导出操作 EasyExcelUtil.exportExcel(response, list, MainTable.class, "主子数据"); } // 导入主子数据 @RequestMapping("/import") public void importData(MultipartFile file) throws IOException { // 导入操作 List<MainTable> list = EasyExcelUtil.importExcel(file, MainTable.class); // 保存数据 mainTableService.saveList(list); } ``` 3.在EasyExcelUtil工具类中封装了导入导出的具体实现。具体代码如下: ```java /** * 导出Excel * * @param response HttpServletResponse * @param list 数据列 * @param clazz 实体类 * @param fileName 文件名 * @throws IOException IO异常 */ public static void exportExcel(HttpServletResponse response, List<?> list, Class<?> clazz, String fileName) throws IOException { // 设置响应头 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); // 导出操作 EasyExcel.write(response.getOutputStream(), clazz).sheet("Sheet1").doWrite(list); } /** * 导入Excel * * @param file 文件 * @param clazz 实体类 * @return 数据列 * @throws IOException IO异常 */ public static List<?> importExcel(MultipartFile file, Class<?> clazz) throws IOException { // 导入操作 return EasyExcel.read(file.getInputStream(), clazz, new ExcelListener()).sheet().doReadSync(); } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值