专业添加导入模板下载和数据上传

This commit is contained in:
2026-09-15 17:43:34 +08:00
parent c7397ad457
commit 6e767947f7
8 changed files with 691 additions and 10 deletions
@@ -6,14 +6,19 @@ import com.roomroot.jwgl.service.DisciplineService;
import com.roomroot.jwgl.unit.PageQuery;
import com.roomroot.jwgl.unit.PageResult;
import com.roomroot.jwgl.unit.Result;
import com.roomroot.jwgl.vo.discipline.MajorImportResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* 学科专业管理控制器
@@ -212,4 +217,36 @@ public class DisciplineManagementController {
cond.setTy(ty);
return Result.success(disciplineService.pageMajor(new PageQuery(pageNum, pageSize), cond));
}
/**
* 从 Excel 导入专业(.xls/.xlsx)。
* <p>
* 表头按专业导入模板的列名匹配(列顺序不限)。已存在的专业跳过、不覆盖库中已有数据;
* 校验不通过的行跳过并在结果明细中给出原因,合格行正常导入。
* </p>
*
* @param file 上传的 Excel 文件(form-data 字段名 file)
* @return 导入结果(新增/跳过/失败数量与明细)
*/
@PostMapping("/major/import")
public Result<MajorImportResultVO> importMajor(@RequestParam("file") MultipartFile file) throws Exception {
MajorImportResultVO result = disciplineService.importMajorExcel(file);
return Result.success(result.getMessage(), result);
}
/**
* 下载专业导入模板(xlsx),列头与导入解析保持一致。
*
* @param response 响应对象
*/
@GetMapping("/major/template")
public void downloadMajorTemplate(HttpServletResponse response) throws Exception {
byte[] bytes = disciplineService.buildMajorImportTemplate();
String fileName = "专业导入模板.xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, StandardCharsets.UTF_8));
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
}
}