|
@@ -0,0 +1,78 @@
|
|
|
+package com.iohao.mmo.excel.controller;
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.iohao.mmo.common.kit.Result;
|
|
|
+import com.iohao.mmo.excel.dto.DemoExcelDTO;
|
|
|
+import com.iohao.mmo.excel.entity.DemoExcel;
|
|
|
+import com.iohao.mmo.excel.kit.ExcelKit;
|
|
|
+import com.iohao.mmo.excel.listener.ExcelDataCustomListener;
|
|
|
+import com.iohao.mmo.excel.listener.ExcelDataListener;
|
|
|
+import com.iohao.mmo.excel.service.DemoExcelService;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.Parameter;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author 渔民小镇
|
|
|
+ * @date 2023-08-29
|
|
|
+ */
|
|
|
+@AllArgsConstructor
|
|
|
+@RestController
|
|
|
+@RequestMapping("/excel")
|
|
|
+@Tag(name = "ExcelController", description = "excel示例")
|
|
|
+public class ExcelController {
|
|
|
+ private final DemoExcelService demoExcelService;
|
|
|
+ @GetMapping("/index")
|
|
|
+ public String index(@RequestParam("name") @Parameter(description = "名字")String name) {
|
|
|
+ return name + ", say - " + LocalTime.now().getSecond();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用导出示例
|
|
|
+ * @param params
|
|
|
+ * @param response
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Operation(summary = "通用导出示例",description = "通用导出示例")
|
|
|
+ @GetMapping("exportdemo")
|
|
|
+ public void exportDemo(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception{
|
|
|
+ List<DemoExcel> list = demoExcelService.list(params,DemoExcel.class);
|
|
|
+ ExcelKit.exportExcelToTarget(response, null, "Excel示例", list, DemoExcelDTO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用导入示例
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @PostMapping(value = "importdemo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
|
+ @Operation(summary = "通用导入示例",description = "通用导入示例")
|
|
|
+ public Result importDemo(@RequestPart("file") @Parameter(description = "文件")MultipartFile file) throws Exception {
|
|
|
+ //通用读取
|
|
|
+ EasyExcel.read(file.getInputStream(), DemoExcelDTO.class, new ExcelDataListener<>(demoExcelService)).sheet(0).doRead();
|
|
|
+ return new Result().ok("导入成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入数据自定义处理示例
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @PostMapping(value = "importcustom", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
|
+ @Operation(summary = "导入数据自定义处理示例",description = "导入数据自定义处理示例")
|
|
|
+ public Result importCustom(@RequestPart("file") @Parameter(description = "文件")MultipartFile file) throws Exception {
|
|
|
+ //自定义读取
|
|
|
+ EasyExcel.read(file.getInputStream(), DemoExcelDTO.class, new ExcelDataCustomListener<>(demoExcelService)).sheet(0).doRead();
|
|
|
+ return new Result().ok("导入成功!");
|
|
|
+ }
|
|
|
+}
|