技术:
前端:vue
后端:springboot
数据库:mysql
pom.xml:
org.apache.poi
poi
3.17
org.apache.poi
poi-ooxml
RELEASE
KHController:
package com.heeexy.example.controller;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.bean.KH;
import com.heeexy.example.bean.LianXiRen;
import com.heeexy.example.service.KHService;
import com.heeexy.example.util.CommonUtil;
@RestController
@RequestMapping("/kh")
@CrossOrigin
@ResponseBody
public class KHController {
@Autowired
private KHService khService;
// 列表
@RequiresPermissions("kh:lists")
@GetMapping("/listKHS")
public JSONObject listKHS(HttpServletRequest request) {
return khService.listKHS(CommonUtil.request2Json(request));
}
// 查询方法
@RequiresPermissions("kh:list")
@GetMapping("/listXZ")
public JSONObject listXZ(HttpServletRequest request) {
return khService.listXZ(CommonUtil.request2Json(request));
}
/**
* excel导入
*/
@RequiresPermissions("kh:upload")
@PostMapping("/upload")
public String addKHexcel(@RequestParam("file") MultipartFile file) throws Exception {
String fileName = file.getOriginalFilename();
String test = khService.batchImport(fileName, file);
System.out.println("test="+test);
return test;
}
}
KHService:
package com.heeexy.example.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.bean.KH;
import com.heeexy.example.bean.LianXiRen;
public interface KHService {
//列表
public JSONObject listKHS(JSONObject jsonObject);
//查询方法
public JSONObject listXZ(JSONObject jsonObject);
//excel导入
public String batchImport(String fileName, MultipartFile file) throws Exception;
}
KHServiceImpl:
package com.heeexy.example.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.*;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.bean.DD;
import com.heeexy.example.bean.KH;
import com.heeexy.example.bean.LianXiRen;
import com.heeexy.example.dao.KHDao;
import com.heeexy.example.service.KHService;
import com.heeexy.example.util.CommonUtil;
@Service
public class KHServiceImpl implements KHService {
@Autowired
private KHDao khDao;
//列表
@Override
public JSONObject listKHS(JSONObject jsonObject) {
CommonUtil.fillPageParam(jsonObject);
int count = khDao.countKH(jsonObject);
List list = khDao.listKHS(jsonObject);
return CommonUtil.successPage(jsonObject, list, count);
}
//查询方法
@Override
public JSONObject listXZ(JSONObject jsonObject) {
CommonUtil.fillPageParam(jsonObject);
int count = khDao.countXZ(jsonObject);
List list = khDao.listXZ(jsonObject);
return CommonUtil.successPage(jsonObject, list, count);
}
/**
* id放入list
*
* @param id
* id(多个已逗号分隔)
* @return List集合
* declareAd
*/
public List getList(String id) {
List list = new ArrayList();
String[] str = id.split(",");
for (int i = 0; i < str.length; i++) {
list.add(str[i]);
}
return list;
}
/**
* excel导入
*/
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public String batchImport(String fileName, MultipartFile file) throws Exception {
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
throw new Exception("上传文件格式不正确");
}
List khs = new ArrayList<>();
try{
InputStream is = file.getInputStream();
HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(is));
//有多少个sheet
int sheets = workbook.getNumberOfSheets();
for (int i = 0; i < sheets; i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
//获取多少行
int rows = sheet.getPhysicalNumberOfRows();
KH kh = null;
//遍历每一行,注意:第 0 行为标题
System.out.println("1234456576879");
System.out.println("进入遍历方法");
for (int j = 1; j < rows; j++) {
kh = new KH();
//获得第 j 行
HSSFRow row = sheet.getRow(j);
System.out.println("row="+row);
kh.setGongsimingcheng(row.getCell(0).getStringCellValue());
System.out.println("getGongsimingcheng="+ kh.getGongsimingcheng());
kh.setGongsidizhi(row.getCell(1).getStringCellValue());
System.out.println("getGongsidizhi="+ kh.getGongsidizhi());
kh.setKhmc(row.getCell(2).getStringCellValue());
System.out.println("getKhmc="+ kh.getKhmc());
kh.setKhlx(row.getCell(3).getStringCellValue());
System.out.println("getKhlx="+ kh.getKhlx());
kh.setDianhua(row.getCell(4).getNumericCellValue());
System.out.println("getDianhua="+ kh.getDianhua());
kh.setShouji(row.getCell(5).getNumericCellValue());
System.out.println("getShouji="+ kh.getShouji());
kh.setDizhi(row.getCell(6).getStringCellValue());
System.out.println("getDizhi="+ kh.getDizhi());
kh.setZuixingenjinjilu(row.getCell(7).getStringCellValue());
System.out.println("getZuixingenjinjilu="+ kh.getZuixingenjinjilu());
kh.setQianfzr(row.getCell(8).getStringCellValue());
System.out.println("getQianfzr="+ kh.getQianfzr());
kh.setKhzt(row.getCell(9).getStringCellValue());
System.out.println("getKhzt="+ kh.getKhzt());
kh.setGenjinriqi(row.getCell(10).getDateCellValue());
System.out.println("getGenjinriqi="+ kh.getGenjinriqi());
kh.setGenjinneirong(row.getCell(11).getStringCellValue());
System.out.println("getGenjinneirong="+ kh.getGenjinneirong());
kh.setFaqiriqi(row.getCell(12).getDateCellValue());
System.out.println("getFaqiriqi="+ kh.getFaqiriqi());
kh.setFuzeren(row.getCell(13).getStringCellValue());
System.out.println("getFuzeren="+ kh.getFuzeren());
khs.add(kh);
System.out.println("khs="+khs.size());
System.out.println("结束");
System.out.println("退出循环");
khDao.addKHexcel(kh);
System.out.println("----------------");
}
}
}catch(Exception e){
return "导入数据格式有误,请检查上传文件";
}
return "导入数据成功";
}
}
KHDao:
package com.heeexy.example.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.bean.KH;
import com.heeexy.example.bean.LianXiRen;
public interface KHDao {
//列表
public int countKH(JSONObject jsonObject);
public List listKHS(JSONObject jsonObject);
//查询
public int countXZ(JSONObject jsonObject);
public List listXZ(JSONObject jsonObject);
//excel导入
public void addKHexcel(KH sysKH);
}
KHMapper:
INSERT INTO kh (gongsimingcheng,gongsidizhi,khmc,khlx,dianhua,shouji,dizhi,zuixingenjinjilu,qianfzr,khzt,genjinriqi,genjinneirong,faqiriqi,fuzeren,dr) VALUES (#{gongsimingcheng},#{gongsidizhi},#{khmc},#{khlx},#{dianhua},#{shouji},#{dizhi},#{zuixingenjinjilu},#{qianfzr},#{khzt},#{genjinriqi},#{genjinneirong},#{faqiriqi},#{fuzeren},#{dr})
KH:
package com.heeexy.example.bean;
import java.util.Date;
//客户列表
public class KH {
private int id;
private int userId;
private Date update_time;
private Date create_time;
private int dr=0;
//客户类型
private String khlx;
//客户状态
private String khzt;
//所属部门
private String ssbm;
//客户名称
private String khmc;
//电话
private double dianhua;
//手机
private double shouji;
//地址
private String dizhi;
//最新跟进记录
private String zuixingenjinjilu;
//前负责人
private String qianfzr;
//计划状态
private int status;
//跟进日期
private Date genjinriqi;
//跟进内容
private String genjinneirong;
//发起日期
private Date faqiriqi;
//公司名称
private String gongsimingcheng;
//公司地址
private String gongsidizhi;
//负责人
private String fuzeren;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
public Date getCreate_time() {
return create_time;
}
public void setCreate_time(Date create_time) {
this.create_time = create_time;
}
public String getKhlx() {
return khlx;
}
public void setKhlx(String khlx) {
this.khlx = khlx;
}
public String getKhzt() {
return khzt;
}
public void setKhzt(String khzt) {
this.khzt = khzt;
}
public String getSsbm() {
return ssbm;
}
public void setSsbm(String ssbm) {
this.ssbm = ssbm;
}
public String getKhmc() {
return khmc;
}
public void setKhmc(String khmc) {
this.khmc = khmc;
}
public String getDizhi() {
return dizhi;
}
public void setDizhi(String dizhi) {
this.dizhi = dizhi;
}
public String getZuixingenjinjilu() {
return zuixingenjinjilu;
}
public void setZuixingenjinjilu(String zuixingenjinjilu) {
this.zuixingenjinjilu = zuixingenjinjilu;
}
public String getQianfzr() {
return qianfzr;
}
public void setQianfzr(String qianfzr) {
this.qianfzr = qianfzr;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public double getDianhua() {
return dianhua;
}
public void setDianhua(double dianhua) {
this.dianhua = dianhua;
}
public double getShouji() {
return shouji;
}
public void setShouji(double shouji) {
this.shouji = shouji;
}
public Date getGenjinriqi() {
return genjinriqi;
}
public void setGenjinriqi(Date genjinriqi) {
this.genjinriqi = genjinriqi;
}
public String getGenjinneirong() {
return genjinneirong;
}
public void setGenjinneirong(String genjinneirong) {
this.genjinneirong = genjinneirong;
}
public Date getFaqiriqi() {
return faqiriqi;
}
public void setFaqiriqi(Date faqiriqi) {
this.faqiriqi = faqiriqi;
}
public String getGongsimingcheng() {
return gongsimingcheng;
}
public void setGongsimingcheng(String gongsimingcheng) {
this.gongsimingcheng = gongsimingcheng;
}
public String getGongsidizhi() {
return gongsidizhi;
}
public void setGongsidizhi(String gongsidizhi) {
this.gongsidizhi = gongsidizhi;
}
public String getFuzeren() {
return fuzeren;
}
public void setFuzeren(String fuzeren) {
this.fuzeren = fuzeren;
}
public int getDr() {
return dr;
}
public void setDr(int dr) {
this.dr = dr;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((create_time == null) ? 0 : create_time.hashCode());
long temp;
temp = Double.doubleToLongBits(dianhua);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((dizhi == null) ? 0 : dizhi.hashCode());
result = prime * result + dr;
result = prime * result + ((faqiriqi == null) ? 0 : faqiriqi.hashCode());
result = prime * result + ((fuzeren == null) ? 0 : fuzeren.hashCode());
result = prime * result + ((genjinneirong == null) ? 0 : genjinneirong.hashCode());
result = prime * result + ((genjinriqi == null) ? 0 : genjinriqi.hashCode());
result = prime * result + ((gongsidizhi == null) ? 0 : gongsidizhi.hashCode());
result = prime * result + ((gongsimingcheng == null) ? 0 : gongsimingcheng.hashCode());
result = prime * result + id;
result = prime * result + ((khlx == null) ? 0 : khlx.hashCode());
result = prime * result + ((khmc == null) ? 0 : khmc.hashCode());
result = prime * result + ((khzt == null) ? 0 : khzt.hashCode());
result = prime * result + ((qianfzr == null) ? 0 : qianfzr.hashCode());
temp = Double.doubleToLongBits(shouji);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((ssbm == null) ? 0 : ssbm.hashCode());
result = prime * result + status;
result = prime * result + ((update_time == null) ? 0 : update_time.hashCode());
result = prime * result + userId;
result = prime * result + ((zuixingenjinjilu == null) ? 0 : zuixingenjinjilu.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
KH other = (KH) obj;
if (create_time == null) {
if (other.create_time != null)
return false;
} else if (!create_time.equals(other.create_time))
return false;
if (Double.doubleToLongBits(dianhua) != Double.doubleToLongBits(other.dianhua))
return false;
if (dizhi == null) {
if (other.dizhi != null)
return false;
} else if (!dizhi.equals(other.dizhi))
return false;
if (dr != other.dr)
return false;
if (faqiriqi == null) {
if (other.faqiriqi != null)
return false;
} else if (!faqiriqi.equals(other.faqiriqi))
return false;
if (fuzeren == null) {
if (other.fuzeren != null)
return false;
} else if (!fuzeren.equals(other.fuzeren))
return false;
if (genjinneirong == null) {
if (other.genjinneirong != null)
return false;
} else if (!genjinneirong.equals(other.genjinneirong))
return false;
if (genjinriqi == null) {
if (other.genjinriqi != null)
return false;
} else if (!genjinriqi.equals(other.genjinriqi))
return false;
if (gongsidizhi == null) {
if (other.gongsidizhi != null)
return false;
} else if (!gongsidizhi.equals(other.gongsidizhi))
return false;
if (gongsimingcheng == null) {
if (other.gongsimingcheng != null)
return false;
} else if (!gongsimingcheng.equals(other.gongsimingcheng))
return false;
if (id != other.id)
return false;
if (khlx == null) {
if (other.khlx != null)
return false;
} else if (!khlx.equals(other.khlx))
return false;
if (khmc == null) {
if (other.khmc != null)
return false;
} else if (!khmc.equals(other.khmc))
return false;
if (khzt == null) {
if (other.khzt != null)
return false;
} else if (!khzt.equals(other.khzt))
return false;
if (qianfzr == null) {
if (other.qianfzr != null)
return false;
} else if (!qianfzr.equals(other.qianfzr))
return false;
if (Double.doubleToLongBits(shouji) != Double.doubleToLongBits(other.shouji))
return false;
if (ssbm == null) {
if (other.ssbm != null)
return false;
} else if (!ssbm.equals(other.ssbm))
return false;
if (status != other.status)
return false;
if (update_time == null) {
if (other.update_time != null)
return false;
} else if (!update_time.equals(other.update_time))
return false;
if (userId != other.userId)
return false;
if (zuixingenjinjilu == null) {
if (other.zuixingenjinjilu != null)
return false;
} else if (!zuixingenjinjilu.equals(other.zuixingenjinjilu))
return false;
return true;
}
@Override
public String toString() {
return "KH [id=" + id + ", userId=" + userId + ", update_time=" + update_time + ", create_time=" + create_time
+ ", dr=" + dr + ", khlx=" + khlx + ", khzt=" + khzt + ", ssbm=" + ssbm + ", khmc=" + khmc
+ ", dianhua=" + dianhua + ", shouji=" + shouji + ", dizhi=" + dizhi + ", zuixingenjinjilu="
+ zuixingenjinjilu + ", qianfzr=" + qianfzr + ", status=" + status + ", genjinriqi=" + genjinriqi
+ ", genjinneirong=" + genjinneirong + ", faqiriqi=" + faqiriqi + ", gongsimingcheng=" + gongsimingcheng
+ ", gongsidizhi=" + gongsidizhi + ", fuzeren=" + fuzeren + "]";
}
public KH(int id, int userId, Date update_time, Date create_time, int dr, String khlx, String khzt, String ssbm,
String khmc, double dianhua, double shouji, String dizhi, String zuixingenjinjilu, String qianfzr,
int status, Date genjinriqi, String genjinneirong, Date faqiriqi, String gongsimingcheng,
String gongsidizhi, String fuzeren) {
super();
this.id = id;
this.userId = userId;
this.update_time = update_time;
this.create_time = create_time;
this.dr = dr;
this.khlx = khlx;
this.khzt = khzt;
this.ssbm = ssbm;
this.khmc = khmc;
this.dianhua = dianhua;
this.shouji = shouji;
this.dizhi = dizhi;
this.zuixingenjinjilu = zuixingenjinjilu;
this.qianfzr = qianfzr;
this.status = status;
this.genjinriqi = genjinriqi;
this.genjinneirong = genjinneirong;
this.faqiriqi = faqiriqi;
this.gongsimingcheng = gongsimingcheng;
this.gongsidizhi = gongsidizhi;
this.fuzeren = fuzeren;
}
public KH() {
super();
// TODO Auto-generated constructor stub
}
}
vue:
excel导入
//methods中的方法
//excel导入开始
getFile: function (event) {
this.file = event.target.files[0];
this.file.name
console.log(this.file);
},
submit: function (event) {
if(this.file == null){
alert("文件为空,请选择文件进行导入");
}
//阻止元素发生默认的行为
event.preventDefault();
let formData = new FormData();
formData.append("file", this.file);
var url = this.HOST + "/kh/upload";
axios.post(url, formData)
.then(function (response) {
alert(response.data);
console.log(response);
window.location.reload();
})
.catch(function (error) {
alert("上传失败,请核对excel表格数据");
console.log(error);
alert(error);
window.location.reload();
});
},
//excel导入结束
数据库:
客户表:
权限表:
要插入的数据:
运行结果: