parent
1c4f91635b
commit
01fb76f5d7
@ -0,0 +1,81 @@
|
|||||||
|
package com.gmh.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.gmh.entity.GmhUser;
|
||||||
|
import com.gmh.entity.R;
|
||||||
|
import com.gmh.utils.POIExcelUtil;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/excel")
|
||||||
|
public class ExcelController {
|
||||||
|
|
||||||
|
@RequestMapping("/t01")
|
||||||
|
public R test01() {
|
||||||
|
return R.ok("ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/readData")
|
||||||
|
public R readSourceData(MultipartFile file) throws IOException {
|
||||||
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
||||||
|
Integer nameCellIndex = null;
|
||||||
|
Map<String, List<GmhUser>> sheetNameList = new LinkedHashMap<>();
|
||||||
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
||||||
|
List<GmhUser> nameList = new ArrayList<>();
|
||||||
|
Sheet sheet = workbook.getSheetAt(i);
|
||||||
|
for (int r = 0; r <= sheet.getLastRowNum(); r++) {
|
||||||
|
Row row = sheet.getRow(r);
|
||||||
|
if (row == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int lastCellNum = row.getLastCellNum();
|
||||||
|
for (int c = 0; c < lastCellNum; c++) {
|
||||||
|
Cell cell = row.getCell(c);
|
||||||
|
String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
||||||
|
if (r == 0) {
|
||||||
|
// 第一行有姓名列
|
||||||
|
if ("姓名".equals(stringCellValue)) {
|
||||||
|
nameCellIndex = c;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// 第一列没有姓名的情况
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nameCellIndex != null) {
|
||||||
|
Cell cell = row.getCell(nameCellIndex);
|
||||||
|
String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
||||||
|
if (!"姓名".equals(stringCellValue) && StrUtil.isNotBlank(stringCellValue)) {
|
||||||
|
GmhUser user = new GmhUser();
|
||||||
|
user.setName(stringCellValue);
|
||||||
|
nameList.add(user);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("未找到姓名列");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!nameList.isEmpty()) {
|
||||||
|
sheetNameList.put(sheet.getSheetName(), nameList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return R.ok().setData(sheetNameList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/template")
|
||||||
|
public R templateUpload(MultipartFile file) throws IOException {
|
||||||
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
||||||
|
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.gmh.controller;
|
||||||
|
|
||||||
|
import com.gmh.entity.GmhUser;
|
||||||
|
import com.gmh.entity.R;
|
||||||
|
import com.gmh.entity.SysBaijiaxing;
|
||||||
|
import com.gmh.service.GmhUserService;
|
||||||
|
import com.gmh.service.SysBaijiaxingService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GmhUserService gmhUserService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysBaijiaxingService baijiaxingService;
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public R test() {
|
||||||
|
List<GmhUser> list = gmhUserService.list();
|
||||||
|
return R.ok("success").setData(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.gmh.dao;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface GmhUserDao extends BaseMapper<com.gmh.entity.GmhUser> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.gmh.dao;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.gmh.entity.SysBaijiaxing;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface SysBaijiaxingDao extends BaseMapper<SysBaijiaxing> {
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.gmh.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import static com.baomidou.mybatisplus.annotation.IdType.AUTO;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("gmh_user")
|
||||||
|
public class GmhUser {
|
||||||
|
|
||||||
|
@TableId(type = AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
package com.gmh.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class R implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private boolean success;
|
||||||
|
private int code;
|
||||||
|
private String msg;
|
||||||
|
private Object data;
|
||||||
|
private long timestamp;
|
||||||
|
|
||||||
|
private R() {}
|
||||||
|
|
||||||
|
public static R error() {
|
||||||
|
return error(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R error(String message) {
|
||||||
|
return error(null, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R error(Integer code, String message) {
|
||||||
|
if(code == null) {
|
||||||
|
code = 500;
|
||||||
|
}
|
||||||
|
if(message == null) {
|
||||||
|
message = "服务器内部错误";
|
||||||
|
}
|
||||||
|
return build(code, false, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R ok() {
|
||||||
|
return ok(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R ok(String message) {
|
||||||
|
return ok(null, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R ok(Integer code, String message) {
|
||||||
|
if(code == null) {
|
||||||
|
code = 200;
|
||||||
|
}
|
||||||
|
if(message == null) {
|
||||||
|
message = "操作成功";
|
||||||
|
}
|
||||||
|
return build(code, true, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static R build(int code, boolean success, String message) {
|
||||||
|
return new R()
|
||||||
|
.setCode(code)
|
||||||
|
.setSuccess(success)
|
||||||
|
.setMessage(message)
|
||||||
|
.setTimestamp(System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
public R setCode(int code) {
|
||||||
|
this.code = code;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public R setSuccess(boolean success) {
|
||||||
|
this.success = success;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public R setMessage(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public R setData(Object data) {
|
||||||
|
this.data = data;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public R setTimestamp(long timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.gmh.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import static com.baomidou.mybatisplus.annotation.IdType.AUTO;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("sys_firstname")
|
||||||
|
public class SysBaijiaxing {
|
||||||
|
|
||||||
|
@TableId(type = AUTO)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String xingshi;
|
||||||
|
|
||||||
|
private String pinyin;
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.gmh.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.gmh.entity.GmhUser;
|
||||||
|
|
||||||
|
public interface GmhUserService extends IService<GmhUser> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.gmh.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.gmh.entity.SysBaijiaxing;
|
||||||
|
|
||||||
|
public interface SysBaijiaxingService extends IService<SysBaijiaxing> {
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.gmh.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.gmh.dao.GmhUserDao;
|
||||||
|
import com.gmh.entity.GmhUser;
|
||||||
|
import com.gmh.service.GmhUserService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GmhUserServiceImpl extends ServiceImpl<GmhUserDao, GmhUser> implements GmhUserService {
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.gmh.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.gmh.dao.SysBaijiaxingDao;
|
||||||
|
import com.gmh.entity.SysBaijiaxing;
|
||||||
|
import com.gmh.service.SysBaijiaxingService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SysBaijiaxingServiceImpl extends ServiceImpl<SysBaijiaxingDao, SysBaijiaxing> implements SysBaijiaxingService {
|
||||||
|
}
|
@ -1 +1,5 @@
|
|||||||
|
server.port=18021
|
||||||
|
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/gmh?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=root
|
||||||
|
Loading…
Reference in new issue