|
|
|
@ -10,11 +10,22 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.example.springboot.service.IUserService;
|
|
|
|
|
import com.example.springboot.entity.User;
|
|
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import jakarta.servlet.ServletOutputStream;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelWriter;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
@ -32,6 +43,9 @@ public class UserController {
|
|
|
|
|
@Resource
|
|
|
|
|
private IUserService userService;
|
|
|
|
|
|
|
|
|
|
@Value("${files.upload.path}") // 从配置文件中读取上传路径
|
|
|
|
|
private String fileUploadPath;
|
|
|
|
|
|
|
|
|
|
// 新增或者更新
|
|
|
|
|
@PostMapping
|
|
|
|
|
public boolean save(@RequestBody User user) {
|
|
|
|
@ -79,6 +93,98 @@ public class UserController {
|
|
|
|
|
queryWrapper.orderByDesc("id");
|
|
|
|
|
return userService.page(new Page<>(pageNum, pageSize), queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//导入接口
|
|
|
|
|
@CrossOrigin // 添加跨域注解
|
|
|
|
|
@PostMapping("/import")
|
|
|
|
|
public boolean imp(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
|
|
if (file.isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
InputStream inputStream = file.getInputStream();
|
|
|
|
|
ExcelReader reader = ExcelUtil.getReader(inputStream);
|
|
|
|
|
|
|
|
|
|
// 设置列名映射
|
|
|
|
|
reader.addHeaderAlias("用户名", "username");
|
|
|
|
|
reader.addHeaderAlias("密码", "password");
|
|
|
|
|
reader.addHeaderAlias("昵称", "nickname");
|
|
|
|
|
reader.addHeaderAlias("邮箱", "email");
|
|
|
|
|
reader.addHeaderAlias("电话", "phone");
|
|
|
|
|
reader.addHeaderAlias("地址", "address");
|
|
|
|
|
|
|
|
|
|
// 读取数据
|
|
|
|
|
List<User> list = reader.readAll(User.class);
|
|
|
|
|
|
|
|
|
|
// 设置创建时间
|
|
|
|
|
for (User user : list) {
|
|
|
|
|
user.setCreateTime(LocalDateTime.now());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存到数据库
|
|
|
|
|
return userService.saveBatch(list);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//导出接口
|
|
|
|
|
@GetMapping("/export")
|
|
|
|
|
public void export(HttpServletResponse response) throws IOException {
|
|
|
|
|
try {
|
|
|
|
|
// 查询所有数据
|
|
|
|
|
List<User> list = userService.list();
|
|
|
|
|
|
|
|
|
|
// 创建一个工作簿
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter(true);
|
|
|
|
|
|
|
|
|
|
// 清除默认标题别名
|
|
|
|
|
writer.clearHeaderAlias();
|
|
|
|
|
|
|
|
|
|
// 按照指定顺序设置标题别名
|
|
|
|
|
LinkedHashMap<String, String> headerAliasMap = new LinkedHashMap<>();
|
|
|
|
|
headerAliasMap.put("username", "用户名");
|
|
|
|
|
headerAliasMap.put("password", "密码");
|
|
|
|
|
headerAliasMap.put("nickname", "昵称");
|
|
|
|
|
headerAliasMap.put("email", "邮箱");
|
|
|
|
|
headerAliasMap.put("phone", "电话");
|
|
|
|
|
headerAliasMap.put("address", "地址");
|
|
|
|
|
|
|
|
|
|
// 设置标题别名
|
|
|
|
|
writer.setHeaderAlias(headerAliasMap);
|
|
|
|
|
|
|
|
|
|
// 只导出设置了别名的字段
|
|
|
|
|
writer.setOnlyAlias(true);
|
|
|
|
|
|
|
|
|
|
// 写入数据
|
|
|
|
|
writer.write(list, true);
|
|
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|
|
|
|
String fileName = URLEncoder.encode("用户信息.xlsx", "UTF-8");
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
|
|
|
|
|
|
|
|
|
// 输出
|
|
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
|
|
writer.flush(out, true);
|
|
|
|
|
|
|
|
|
|
// 关闭流
|
|
|
|
|
writer.close();
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
response.reset();
|
|
|
|
|
response.setContentType("application/json");
|
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
|
response.getWriter().println("导出失败:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|