easypoi一对多子集导入导出,@ExcelCollection注解的使用。
作者:互联网
easypoi一对多子集导入导出。使用@ExcelCollection注解。
TestController:
import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; import com.xingyi.hos.model.response.ResponseObjectResult; import com.xingyi.hos.model.response.ResponseStatus; import com.xingyi.hos.model.response.ResultCode; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * @description: 设备台账接口 * @author liuyachao * @date 2021/9/23 12:07 */ @Api(value = "设备台账接口",description = "设备台账接口") @RestController @RequestMapping("/test") @Slf4j public class TestController { @PostMapping("/import") @ApiOperation(value = "设备台账导入", notes = "liuyachao") @ApiImplicitParams({ @ApiImplicitParam(name = "Token", value = "token", required = true, paramType = "header", dataType = "string"), @ApiImplicitParam(name = "file", value = "需要导入的excel文件", required = true, paramType = "form", dataType = "file") }) public ResponseObjectResult importEquipmentAccounts(@RequestPart(value = "file") MultipartFile file) { try { ImportParams params = new ImportParams(); params.setTitleRows(0); params.setHeadRows(2); List<Question> questionList = ExcelImportUtil.importExcel(file.getInputStream(),Question.class,params); log.info("questionList:{}",questionList); return new ResponseObjectResult(new ResponseStatus(ResultCode.SUCCESS)); } catch (Exception e) { log.error("importEquipmentAccounts 设备台账导入,异常:",e); return new ResponseObjectResult(new ResponseStatus(ResultCode.FAIL)); } } }
Question:
import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelCollection; import lombok.Data; import java.io.Serializable; import java.util.List; /** * Created by Administrator on 2022/7/19. */ @Data public class Question implements Serializable{ private static final long serialVersionUID = 4349471525156143284L; @Excel(name = "所属学科",needMerge = true) private String subject; @Excel(name = "知识点",needMerge = true) private String konwledge; @Excel(name = "类型",needMerge = true) private String tmType; @Excel(name = "案例",needMerge = true) private String content; @ExcelCollection(name = "选项") private List<QuestionChild> childList; }
QuestionChild:
import cn.afterturn.easypoi.excel.annotation.Excel; import lombok.Data; import java.io.Serializable; /** * Created by Administrator on 2022/7/19. */ @Data public class QuestionChild implements Serializable{ private static final long serialVersionUID = -6141999253465493661L; @Excel(name = "题目") private String title; @Excel(name = "子题型") private String type; }
结果如图:
可以获取到子列表:
导入的Excel表格:
导出同理。实体相同调用导出。
参考文档:http://easypoi.mydoc.io/#text_197841
标签:name,Excel,private,子集,ExcelCollection,io,import,easypoi 来源: https://www.cnblogs.com/super-chao/p/16493556.html