diff --git a/service/pom.xml b/service/pom.xml index f0f72fcc97c6dc96d7e59b89c4f5258eca9a46bf..edd706fb58db595ad61b87ac734bc4d3b1bdc5fd 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -45,6 +45,11 @@ org.mybatis.spring.boot mybatis-spring-boot-autoconfigure + + com.alibaba + easyexcel + 3.0.5 + diff --git a/service/src/main/java/com/alawliet/dev/service/TestService.java b/service/src/main/java/com/alawliet/dev/service/TestService.java index 2ce06c84f24071801b2061752088bbd53fec6d94..7478a13cc7e601a3e5fddd3611cfceef138af31d 100644 --- a/service/src/main/java/com/alawliet/dev/service/TestService.java +++ b/service/src/main/java/com/alawliet/dev/service/TestService.java @@ -6,9 +6,19 @@ import com.alawliet.dev.client.exception.ServerException; import com.alawliet.dev.client.http.HttpResult; import com.alawliet.dev.client.http.RequestHead; import com.alawliet.dev.client.http.ResponseHead; +import com.alawliet.dev.service.entity.UserAccount; import com.alawliet.dev.service.mapper.AccountMapper; +import com.alawliet.dev.service.po.UserAccountPO; +import com.alawliet.dev.service.service.UserAccountService; +import com.alawliet.dev.service.utils.ExcelUtils; import jakarta.annotation.Resource; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; @Component public class TestService implements IServerApi { @@ -22,6 +32,9 @@ public class TestService implements IServerApi { @Resource AccountMapper accountMapper; + @Resource + UserAccountService userAccountService; + @Api(code = "A10002", title = "用户名和密码登录") public int login(HttpResult request) { System.out.println("调用了A10002"); @@ -47,4 +60,35 @@ public class TestService implements IServerApi { public void login6(HttpResult request) { var value = this.accountMapper.isFollow("123"); } + + @Api(code = "A10007", title = "导入账户数据") + public void importAccounts(MultipartFile file) { + try { + // 读取Excel文件并转换为PO列表 + List userAccountPOList = ExcelUtils.synchronousReadExcel(file.getInputStream(), UserAccountPO.class); + + // 将PO列表转换为BO列表 + List userAccountList = userAccountPOList.stream().map(po -> { + UserAccount bo = new UserAccount(); + BeanUtils.copyProperties(po, bo); + return bo; + }).collect(Collectors.toList()); + + // 批量保存到数据库 + userAccountService.saveBatch(userAccountList); + + // 打印Excel头部信息 + List fixedVoList = ExcelUtils.getFixedVoList(UserAccountPO.class, null); + for (ExcelUtils.FixedVo fixedVo : fixedVoList) { + System.out.println("字段名: " + fixedVo.getFiledName()); + System.out.println("注解值: " + fixedVo.getValue()); + System.out.println("字段值: " + fixedVo.getFiledValue()); + System.out.println("排序索引: " + fixedVo.getIndex()); + System.out.println(); + } + } catch (IOException e) { + e.printStackTrace(); + throw new ServerException("-100", e.getMessage()); + } + } } \ No newline at end of file diff --git a/service/src/main/java/com/alawliet/dev/service/po/UserAccountPO.java b/service/src/main/java/com/alawliet/dev/service/po/UserAccountPO.java new file mode 100644 index 0000000000000000000000000000000000000000..b9dcdc9d7e3ac53f150ac167c40c96b5af4aea30 --- /dev/null +++ b/service/src/main/java/com/alawliet/dev/service/po/UserAccountPO.java @@ -0,0 +1,40 @@ +package com.alawliet.dev.service.po; + + +import com.alibaba.excel.annotation.ExcelProperty; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +@Data +public class UserAccountPO { + @ExcelProperty(value = "用户ID", index = 0) + private String userUId; + + @ExcelProperty(value = "账户", index = 1) + private String account; + + @ExcelProperty(value = "用户名", index = 2) + private String userName; + + @ExcelProperty(value = "手机号", index = 3) + private String mobile; + + @ExcelProperty(value = "密码", index = 4) + private String passWord; + + @ExcelProperty(value = "用户编号", index = 5) + private String UserNo; + + @ExcelProperty(value = "组织ID", index = 6) + private String OrgId; + + @ExcelProperty(value = "组织代码", index = 7) + private String OrgCode; + + @ExcelProperty(value = "邮箱", index = 8) + private String Mail; + + @ExcelProperty(value = "工号", index = 9) + private String workNo; + +} \ No newline at end of file diff --git a/service/src/main/java/com/alawliet/dev/service/service/UserAccountService.java b/service/src/main/java/com/alawliet/dev/service/service/UserAccountService.java new file mode 100644 index 0000000000000000000000000000000000000000..dbf245929b1eb9e60bb7ba07a302407ebc675f2c --- /dev/null +++ b/service/src/main/java/com/alawliet/dev/service/service/UserAccountService.java @@ -0,0 +1,7 @@ +package com.alawliet.dev.service.service; + +import com.alawliet.dev.service.entity.UserAccount; +import com.baomidou.mybatisplus.extension.service.IService; + +public interface UserAccountService extends IService { +} diff --git a/service/src/main/java/com/alawliet/dev/service/service/impl/UserAccountServiceImpl.java b/service/src/main/java/com/alawliet/dev/service/service/impl/UserAccountServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..4af63fd1c30c87257acad3af227a3f291217a5ae --- /dev/null +++ b/service/src/main/java/com/alawliet/dev/service/service/impl/UserAccountServiceImpl.java @@ -0,0 +1,11 @@ +package com.alawliet.dev.service.service.impl; + +import com.alawliet.dev.service.entity.UserAccount; +import com.alawliet.dev.service.mapper.AccountMapper; +import com.alawliet.dev.service.service.UserAccountService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Service; + +@Service +public class UserAccountServiceImpl extends ServiceImpl implements UserAccountService { +} diff --git a/service/src/main/java/com/alawliet/dev/service/utils/ExcelUtils.java b/service/src/main/java/com/alawliet/dev/service/utils/ExcelUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..4a62f0cebf673efa2bff23c481afaa282dd0753a --- /dev/null +++ b/service/src/main/java/com/alawliet/dev/service/utils/ExcelUtils.java @@ -0,0 +1,167 @@ +package com.alawliet.dev.service.utils; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.annotation.format.NumberFormat; +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; + +import jakarta.servlet.http.HttpServletResponse; +import lombok.Data; + +import java.io.IOException; + +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; +import java.net.URLEncoder; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.stream.Collectors; + +/** + * excel工具类 + */ +public class ExcelUtils { + + public static void exportModule(String filename, HttpServletResponse response, Class classModule, List data) throws IOException { + + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("utf-8"); + response.setHeader("Content-disposition", "attachment;filename*=utf-8'zh_cn'" + getFilename(filename)); + EasyExcel.write(response.getOutputStream(), classModule).sheet("sheet1").doWrite(data); + } + + public static void exportDynamicHeadWrite(String filename, HttpServletResponse response, List> headList, List data) throws IOException { + + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("utf-8"); + response.setHeader("Content-disposition", "attachment;filename*=utf-8'zh_cn'" + getFilename(filename)); + EasyExcel.write(response.getOutputStream()) + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) + // 这里放入动态头 + .head(headList).sheet("sheet1") + // 当然这里数据也可以用 List> 去传入 + .doWrite(data); + } + + public static void exportDynamicHeadWriteStream(String filename, HttpServletResponse response, List> headList, List data) throws IOException { + + response.setContentType("application/octet-stream"); + response.setHeader("Content-disposition", "attachment;filename=" + getFilename(filename)); + EasyExcel.write(response.getOutputStream()) + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) + // 这里放入动态头 + .head(headList).sheet("sheet1") + // 当然这里数据也可以用 List> 去传入 + .doWrite(data); + } + + private static String getFilename(String filename) throws UnsupportedEncodingException { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String formattedDate = dateFormat.format(new Date()); + String fileName = filename + formattedDate; + return URLEncoder.encode(fileName, "UTF-8") + ".xlsx"; + } + + /** + * 同步的返回,不推荐使用,如果数据量大会把数据放到内存里面 + */ + public static List synchronousReadExcel(InputStream inputStream, Class clazz) throws IOException { + return EasyExcel.read(inputStream).head(clazz).sheet().doReadSync(); + } + + public static String[] getHead(Class c) { + List vo = getFixedVoList(c, null); + return vo.stream().sorted(Comparator.comparing(FixedVo::getIndex)).map(FixedVo::getValue).toArray(String[]::new); + } + + public static Map getData(Class c, T excelVo, String[] heads) { + Map excel = new LinkedHashMap<>(heads.length); + List vo = getFixedVoList(c, excelVo); + Map voMap = vo.stream().collect(Collectors.toMap(FixedVo::getValue, y -> y)); + for (String head : heads) { + Object filedValue = voMap.get(head).getFiledValue(); + excel.put(head, filedValue == null ? " " : filedValue); + } + return excel; + } + + + /** + * 获取字典属性列表 + */ + public static List getFixedVoList(Class c, T excelVo) { + if (c == null) { + return List.of(); + } + try { + Field[] fields = c.getDeclaredFields(); + List fixedVoList = new ArrayList<>(); + for (Field field : fields) { + ExcelIgnore ignore = field.getAnnotation(ExcelIgnore.class); + if (ignore != null) { + continue; + } + field.setAccessible(true); + // 获取ExcelProperty注解 + ExcelProperty property = field.getAnnotation(ExcelProperty.class); + NumberFormat numberFormat = field.getAnnotation(NumberFormat.class); + DateTimeFormat dateFormat = field.getAnnotation(DateTimeFormat.class); + if (property != null) { + try { + String head = property.value()[0]; + int index = property.index(); + FixedVo vo = new FixedVo(); + vo.setFiledName(field.getName()); + if (excelVo != null) { + Object filedValue = field.get(excelVo); + if (dateFormat != null) { + SimpleDateFormat dateFormatModel = new SimpleDateFormat(dateFormat.value()); + filedValue = dateFormatModel.format((Date) filedValue); + } else if (numberFormat != null) { + DecimalFormat d = new DecimalFormat(numberFormat.value()); + filedValue = d.format(filedValue); + } + vo.setFiledValue(filedValue); + } + vo.setValue(head); + vo.setIndex(index); + fixedVoList.add(vo); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return fixedVoList; + } catch (Exception e) { + e.printStackTrace(); + } + return List.of(); + } + + @Data + public static class FixedVo { + /** + * 排序 + */ + private Integer index; + + /** + * 注解中的名称,即表头 + */ + private String value; + + /** + * 字段名称 + */ + private String filedName; + + /** + * 字段值 + */ + private Object filedValue; + } +} \ No newline at end of file