1.:导入Apache POI依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2.:ResponseTemplate 代码
package com.systop.common.bean;
import lombok.Data;
@Data
public class ResponseTemplate {
private int code;
private String msg;
private Object data;
public static ResponseTemplate success() {
ResponseTemplate template = new ResponseTemplate();
template.setCode(200);
template.setMsg("请求成功!");
template.setData(null);
return template;
}
public static ResponseTemplate success(String msg, Object data) {
ResponseTemplate template = new ResponseTemplate();
template.setCode(200);
template.setMsg(msg);
template.setData(data);
return template;
}
public static ResponseTemplate success(String msg) {
ResponseTemplate template = new ResponseTemplate();
template.setCode(200);
template.setMsg(msg);
return template;
}
public static ResponseTemplate fail() {
ResponseTemplate template = new ResponseTemplate();
template.setCode(500);
template.setMsg("请求失败!");
template.setData(null);
return template;
}
public static ResponseTemplate fail(String message, Object data) {
ResponseTemplate template = new ResponseTemplate();
template.setCode(500);
template.setMsg(message);
template.setData(data);
return template;
}
}
3.:controller代码
@RequestMapping("importExcel")
public ResponseTemplate importExcel(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value="file")MultipartFile file) {
return consultationService.importExcel(request,response,getCurrentUser(),file);
}
4.:service业务逻辑代码
@Transactional
@Override
public ResponseTemplate importExcel(HttpServletRequest request, HttpServletResponse response,
User user,MultipartFile file) {
XSSFWorkbook wb = null;
try {
InputStream inputStream = file.getInputStream();
wb = new XSSFWorkbook(inputStream);
XSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
int columnNum = 0;
if(rowNum!=0&&sheet.getRow(0)!=null) {
columnNum = sheet.getRow(0).getPhysicalNumberOfCells();
}
for (int i = 2; i <=rowNum; i++) {
XSSFRow row = sheet.getRow(i);
if(row == null) {
continue;
}
int j = 0;
String name = row.getCell(j)==null?"":row.getCell(j).getStringCellValue();
if(StringUtils.isEmpty(name)) {
continue;
}
if(StringUtils.isEmpty(name)) {
Object NOTNAME="NO NAME";
return ResponseTemplate.fail("姓名为必填项!",NOTNAME);
}
String gender = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
if(StringUtils.isEmpty(gender)) {
Object NOGENDER="NO GENDER";
return ResponseTemplate.fail("性别为必填项!",NOGENDER);
}else
{
Dictionary dictionary = dictionaryService.findByText(gender.trim());
if(dictionary==null){
Object NOTPHONE="NO PHONE";
return ResponseTemplate.fail("请填写正确的性别!",NOTPHONE);
}
}
String contacts = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
++j;
if(contacts.equals(0)){
Object NOTcontacts="NO contacts";
return ResponseTemplate.fail("联系人为必填项!",NOTcontacts);
}
String contactPhone = "";
if(StringUtils.isEmpty(row.getCell(j)==null?"":row.getCell(j).getNumericCellValue())){
Object NOTPHONE="NO PHONE";
return ResponseTemplate.fail("联系电话为必填项!",NOTPHONE);
}
DecimalFormat df = new DecimalFormat("#");
if(row.getCell(j).getCellTypeEnum() == CellType.NUMERIC) {
String phoneD = df.format(row.getCell(j).getNumericCellValue());
if(phoneD!=null) {
contactPhone = phoneD;
}
}else {
contactPhone = row.getCell(j)==null?"":df.format(row.getCell(j).getNumericCellValue());
}
String Intentionality = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String Communicate = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String IntentionCourse = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String IntentionClass = row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String followUpStatus=row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String ChannelName=row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String LastTime=row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String Campus=row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
String SalesPerson=row.getCell(++j)==null?"":row.getCell(j).getStringCellValue();
if(StringUtils.isEmpty(ChannelName)){
Object NOTChannelName="NO ChannelName";
return ResponseTemplate.fail("渠道为必填项!",NOTChannelName);
}
StudentConsultation consultation = new StudentConsultation();
if(StringUtils.isEmpty(followUpStatus)){
followUpStatus="待跟进";
}
if(StringUtils.isEmpty(Intentionality)){
Intentionality="?";
}
List<Student> students = studentDao.findByNameAndPhone(user.getEmployee().getSchool().getId(), contactPhone);
if(students!=null&&!students.isEmpty()) {
Integer employeeId = channelCategoryMBService.employeeId(user.getUsername(), user.getSuffix());
for (Student su:students){
channelCategoryMBService.UpdateStudentCreateId(employeeId,su.getId());
}
}else {
Student student = new Student();
student.setName(name);
if(!StringUtils.isEmpty(gender.trim())) {
Dictionary dictionary = dictionaryService.findByText(gender.trim());
if(dictionary!=null) {
student.setGender(Integer.valueOf(dictionary.getCode()));
}
}
if(!StringUtils.isEmpty(contacts.trim())) {
if(contacts.trim().equals("其他")){
Dictionary dictionary = dictionaryService.findByTestAndId(contacts.trim());
student.setContacts(Integer.valueOf(dictionary.getCode()));
}else
{
Dictionary dictionary = dictionaryService.findByText(contacts.trim());
if(dictionary!=null) {
student.setContacts(Integer.valueOf(dictionary.getCode()));
}else {
Object NOTChannelName="NO ChannelName";
return ResponseTemplate.fail("请填写正确的联系人!",NOTChannelName);
}
}
}
student.setContactPhone(contactPhone);
student.setSchool(user.getEmployee().getSchool());
if("?".equals(Intentionality.trim())) {
consultation.setIntentionality(0);
}else if("低".equals(Intentionality.trim())) {
consultation.setIntentionality(1);
}else if("中".equals(Intentionality.trim())) {
consultation.setIntentionality(2);
}else if("高".equals(Intentionality.trim())) {
consultation.setIntentionality(3);
}else {
Object NOTChannelName="NO ChannelName";
return ResponseTemplate.fail("请填写正确的意向度! 分为 低 高 中",NOTChannelName);
}
consultation.setRemark(Communicate);
if(!StringUtils.isEmpty(IntentionCourse)){
List<Course> courses = channelCategoryMBService.courseList(user.getEmployee().getSchool().getId(), IntentionCourse);
if(courses.size()==0){
Object NOTCHANNE="NO CHANNELNAME";
return ResponseTemplate.fail("请填写已存在的课程!",NOTCHANNE);
}else
{
consultation.setCourse1(courses.get(0));
}
}
if(!StringUtils.isEmpty(IntentionClass)){
List<ClassGrade> classrooms = classGradeDao.classgradelist(IntentionClass,user.getEmployee().getSchool());
if(classrooms.size()==0){
Object NOTCHANNE="NO CHANNELNAME";
return ResponseTemplate.fail("请填写已存在的班级!",NOTCHANNE);
}else
{
consultation.setGrade1(classrooms.get(0));
}
}
consultation.setFollowUpStatus(FollowUpStatusEnum.valueOf(followUpStatus.trim()));
Long schoolId;
if(user.getEmployee().getSchool().getSchoolLevel() == SchoolLevelEnum.校区){
schoolId=user.getEmployee().getSchool().getId();
}
else
{
schoolId=user.getEmployee().getSchool().getPid();
}
List<Channel> channels=channelDao.findBySchoolName(ChannelName,schoolId);
if(channels.size()!=0) {
Channel chann = channels.get(0);
consultation.setSourceChannel(chann);
chann.setConsultationVolume(chann.getConsultationVolume()+1);
if(StringUtils.isEmpty(followUpStatus)&&FollowUpStatusEnum.已成交.toString().equals(followUpStatus)) {
chann.setTurnover(chann.getTurnover() + 1);
}
double rate = (double) chann.getTurnover() / chann.getConsultationVolume() * 100;
chann.setTransactionRate(MathUtil.roundDouble(rate, 2) + "%");
consultation.setStu(student);
consultation.setSalesperson(user.getEmployee());
consultation.setUpdateTime(consultation.getCreatime());
studentDao.save(student);
consultationDao.save(consultation);
channelDao.save(chann);
buildParent2StudentRelation(student, student.getContacts(), contactPhone, user.getEmployee().getSchool().getId());
}else
{
Object NOTCHANNE="NO CHANNELNAME";
return ResponseTemplate.fail("请填写已存在的渠道名称!",NOTCHANNE);
}
}
}
wb.close();
inputStream.close();
return ResponseTemplate.success("导入成功!");
}
catch (IllegalArgumentException e){
return ResponseTemplate.fail("请输入正确的跟进方式!", e.getMessage());
}catch (IllegalStateException e){
e.printStackTrace();
return ResponseTemplate.fail("只能输入汉字!", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return ResponseTemplate.fail("导入失败,请检查数据是否符合导入要求!", e.getMessage());
}
}