From 312b236072ae083875d7a2d81adafdfd45ca4082 Mon Sep 17 00:00:00 2001
From: adru <2960599346@qq.com>
Date: Mon, 29 Jul 2024 00:43:20 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AAexcel?=
=?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
service/pom.xml | 5 +
.../com/alawliet/dev/service/TestService.java | 44 +++++
.../dev/service/po/UserAccountPO.java | 40 +++++
.../service/service/UserAccountService.java | 7 +
.../service/impl/UserAccountServiceImpl.java | 11 ++
.../dev/service/utils/ExcelUtils.java | 167 ++++++++++++++++++
6 files changed, 274 insertions(+)
create mode 100644 service/src/main/java/com/alawliet/dev/service/po/UserAccountPO.java
create mode 100644 service/src/main/java/com/alawliet/dev/service/service/UserAccountService.java
create mode 100644 service/src/main/java/com/alawliet/dev/service/service/impl/UserAccountServiceImpl.java
create mode 100644 service/src/main/java/com/alawliet/dev/service/utils/ExcelUtils.java
diff --git a/service/pom.xml b/service/pom.xml
index f0f72fc..edd706f 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 2ce06c8..7478a13 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 0000000..b9dcdc9
--- /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 0000000..dbf2459
--- /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 0000000..4af63fd
--- /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 0000000..4a62f0c
--- /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
--
Gitee