|
@@ -1,39 +1,78 @@
|
|
-/*
|
|
|
|
- * ioGame
|
|
|
|
- * Copyright (C) 2021 - 2023 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
|
|
|
|
- * # iohao.com . 渔民小镇
|
|
|
|
- *
|
|
|
|
- * This program is free software: you can redistribute it and/or modify
|
|
|
|
- * it under the terms of the GNU Affero General Public License as
|
|
|
|
- * published by the Free Software Foundation, either version 3 of the
|
|
|
|
- * License, or (at your option) any later version.
|
|
|
|
- *
|
|
|
|
- * This program is distributed in the hope that it will be useful,
|
|
|
|
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
- * GNU Affero General Public License for more details.
|
|
|
|
- *
|
|
|
|
- * You should have received a copy of the GNU Affero General Public License
|
|
|
|
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
- */
|
|
|
|
package com.iohao.mmo.excel.controller;
|
|
package com.iohao.mmo.excel.controller;
|
|
|
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
-
|
|
|
|
|
|
+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.time.LocalTime;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author 渔民小镇
|
|
* @author 渔民小镇
|
|
* @date 2023-08-29
|
|
* @date 2023-08-29
|
|
*/
|
|
*/
|
|
|
|
+@AllArgsConstructor
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/excel")
|
|
@RequestMapping("/excel")
|
|
|
|
+@Tag(name = "ExcelController", description = "excel示例")
|
|
public class ExcelController {
|
|
public class ExcelController {
|
|
-
|
|
|
|
|
|
+ private final DemoExcelService demoExcelService;
|
|
@GetMapping("/index")
|
|
@GetMapping("/index")
|
|
- public String index(String name) {
|
|
|
|
|
|
+ public String index(@RequestParam("name") @Parameter(description = "名字")String name) {
|
|
return name + ", say - " + LocalTime.now().getSecond();
|
|
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("导入成功!");
|
|
|
|
+ }
|
|
}
|
|
}
|