若依导出主表和子表数据

    @PreAuthorize("@ss.hasPermi('manage:material:export')")
    @Log(title = "下料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Material material) {
        List<Material> list = materialService.selectMaterialList(material);
        List<Material> resultList = new ArrayList<>(); // 创建用于存储查询结果的集合
        for (Material item : list) {
            Material result = materialService.selectMaterialByMaterialId(item.getMaterialId());
            if (result != null) {
                // 根据id查询子表数据
                List<IotMaterialColors> iotMaterialColors = materialService.selectIotMaterialColorsByMaterialId(item.getMaterialId());
                result.setIotMaterialColorsList(iotMaterialColors);
                resultList.add(result); // 将处理后的对象添加到结果集
            }
        }

        ExcelUtil<Material> util = new ExcelUtil<>(Material.class);
        util.exportExcel(response, resultList, "下料管理数据");
    }

实体类设置主表 : 

width = 20, needMerge = true
    /** 客户名称 */
    @Excel(name = "客户名称",width = 20, needMerge = true)
    private String name;

    /** 下料人 */
    @Excel(name = "下料人",width = 20, needMerge = true)
    private String xialiaoren;

    /** 下料时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "下料时间", dateFormat = "yyyy-MM-dd",width = 20, needMerge = true)
    private Date cuttingTime;

      /** 子表信息 */
    @Excel
    private List<IotMaterialColors> iotMaterialColorsList;

实体类关联子表 : 

cellType = Excel.ColumnType.STRING
    /** 板材颜色 */
    @Excel(name = "板材颜色", cellType = Excel.ColumnType.STRING)
    private String color;

    /** 该颜色板材的数量 */
    @Excel(name = "板材数量(/张)")
    private BigDecimal quantity;

    /** 该颜色背板数量的数量 */
    @Excel(name = "背板数量(/张)")
    private Long quantitybb;

若依框架提供了方便的主子表数据导入导出功能。具体步骤如下: 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(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值