Controller层
package com.xue.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;
import com.xue.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 页面
*/
@RequestMapping("/index")
public String index(){
return "index";
}
/**
* 导入excel
*/
@RequestMapping("/import")
@ResponseBody
public String excelImport(@RequestParam(value="filename")MultipartFile file,HttpSession session){
int result = 0;
try {
result = userService.addUser(file);
} catch (Exception e) {
e.printStackTrace();
}
if(result > 0){
return "excel文件数据导入成功!";
}else{
return "excel文件数据导入成功!";
}
}
}
Service层
(1)接口(UserService.java)
package com.xue.service;
import org.springframework.web.multipart.MultipartFile;
public interface UserService {
/*传入文件内容*/
public int addUser(MultipartFile file) throws Exception;
}
(2)实现类(UserServiceImpl.java)
package com.xue.service.Impl;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
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.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.xue.dao.UserMapper;
import com.xue.entity.SendMessage;
import com.xue.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(MultipartFile file) throws Exception{
int result = 0;
//存放excel表中所有SendMessage细腻
List userList = new ArrayList<>();
/**
*
* 判断文件版本
*/
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
InputStream ins = file.getInputStream();
Workbook wb = null;
if(suffix.equals("xlsx")){
wb = new XSSFWorkbook(ins);
}else{
wb = new HSSFWorkbook(ins);
}
/**
* 获取excel表单
*/
Sheet sheet = wb.getSheetAt(0);
/**
* line = 1:从表的第二行开始获取记录
*
*/
if(null != sheet){
for(int line = 1; line <= sheet.getLastRowNum();line++){
SendMessage sendmessage = new SendMessage();
Row row = sheet.getRow(line);
if(null == row){
continue;
}
//将从Excel表中读取的内容转成String格式(如果Excel表格中纯数字,不执行一下操作,会报错,建议一下操作全部执行。)
row.getCell(0).setCellType(CellType.STRING);
row.getCell(1).setCellType(CellType.STRING);
row.getCell(2).setCellType(CellType.STRING);
row.getCell(3).setCellType(CellType.STRING);
row.getCell(4).setCellType(CellType.STRING);
row.getCell(5).setCellType(CellType.STRING);
row.getCell(6).setCellType(CellType.STRING);
row.getCell(7).setCellType(CellType.STRING);
row.getCell(8).setCellType(CellType.STRING);
row.getCell(9).setCellType(CellType.STRING);
row.getCell(10).setCellType(CellType.STRING);
row.getCell(11).setCellType(CellType.STRING);
/**
* 获取第一个单元格的内容
*/
String smid = row.getCell(0).getStringCellValue();
/**
* 获取第二个单元格的内容
*/
String company = row.getCell(1).getStringCellValue();
String org = row.getCell(2).getStringCellValue();
String dest = row.getCell(3).getStringCellValue();
String senddata = row.getCell(4).getStringCellValue();
//Date类型
String insert_date = row.getCell(5).getStringCellValue();
Date date = null;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
date = format.parse(insert_date);
//Integer类型
Integer i = null;
String status = row.getCell(6).getStringCellValue();
if(status.equals("") || status.equals(null)) {
i = null;
}else {
i = Integer.parseInt(status);
}
String msg_group = row.getCell(7).getStringCellValue();
String report_status = row.getCell(8).getStringCellValue();
String error_code = row.getCell(9).getStringCellValue();
String receive_date = row.getCell(10).getStringCellValue();
Date date2 = null;
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
date2 = format2.parse(receive_date);
String send_date = row.getCell(11).getStringCellValue();
Date date3 = null;
DateFormat format3 = new SimpleDateFormat("yyyy-MM-dd");
date3 = format3.parse(receive_date);
sendmessage.setSmid(smid);
sendmessage.setCompany(company);
sendmessage.setOrg(org);;
sendmessage.setDest(dest);
sendmessage.setSenddata(senddata);
sendmessage.setInsertDate(date);
sendmessage.setStatus(i);
sendmessage.setMsgGroup(msg_group);
sendmessage.setReportStatus(report_status);
sendmessage.setErrorCode(error_code);
sendmessage.setReceiveDate(date2);
sendmessage.setSendDate(date3);
userList.add(sendmessage);
}
for(SendMessage sendmessage:userList){
result = userMapper.addUser(sendmessage);
}
}
return result;
}
}
数据库数据添加(Mybatis实现)
(1)接口(UserMapper.java)
package com.xue.dao;
import com.xue.entity.SendMessage;
public interface UserMapper {
int addUser(SendMessage sendmessage);;
}
(2)Mybatis(UserMapper.xml)
insert into sg_sendsms
(smid,company,org,dest,senddata,insert_date,status,msg_group,report_status,error_code,receive_date,send_date)
values
(#{smid},#{company},#{org},#{dest},#{senddata},
#{insertDate},#{status},#{msgGroup},#{reportStatus},#{errorCode},#{receiveDate},#{sendDate})
实体类(SendMessage.java)
package com.xue.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
@Data
@Entity(name="sg_sendsms")
public class SendMessage {
@Id
@GeneratedValue(generator="id")
@GenericGenerator(name="id",strategy="native")
private Integer id;
private String smid;
//所属公司
private String company;
private String org;
//是否发送成功
private String dest;
private String senddata;
//创建时间
@DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
private Date insertDate;
//是否发送成功?success or fail
private Integer status;
private String msgGroup;
private String reportStatus;
private String errorCode;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date receiveDate;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date sendDate;
}
主类(SpringbootImportExcelApplication.java)
package com.xue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.xue.dao")
public class SpringbootImportExcelApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootImportExcelApplication.class, args);
}
}
前端页面代码(index.html)
Insert title here
导入EXCEL
相关pom依赖(pom.xml)
4.0.0
Import-Excel
Import-Excel
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
org.springframework.boot
spring-boot-maven-plugin
true
true
org.apache.maven.plugins
maven-surefire-plugin
true
false
org.apache.maven.plugins
maven-compiler-plugin
1.8
maven-resources-plugin
copy-yml
validate
copy-resources
${project.build.directory}
src/main/resources
application.yml
org.apache.poi
poi
3.15
org.apache.poi
poi-ooxml-schemas
3.15
org.apache.poi
poi-ooxml
3.15
com.alibaba
fastjson
1.2.7
net.sourceforge.jexcelapi
jxl
2.6.10
junit
junit
4.12
org.springframework.boot
spring-boot-starter-test
test
2.0.2.RELEASE
org.springframework
spring-beans
5.0.5.RELEASE
compile
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-cache
net.sf.ehcache
ehcache
org.springframework.boot
spring-boot-starter-data-jpa
com.alibaba
druid
1.0.9
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.springframework.boot
spring-boot-starter-thymeleaf
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.apache.commons
commons-lang3
org.projectlombok
lombok
provided
com.alibaba
fastjson
1.2.32
com.google.guava
guava
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
provided
com.h2database
h2
test
org.springframework.cloud
spring-cloud-dependencies
Finchley.M9
pom
import
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
releases
infra realease
http://172.16.50.180:8085/nexus/content/repositories/releases/
snapshots
infra snapshotRepository
http://112.94.224.249:8085/nexus/content/repositories/snapshots/
相关配置文件(application.yml)
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/build?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 112233
initialize: true
init-db: true
mybatis:
type-aliases-package: com.southgis.imap.maintain.entity
configuration:
map-underscore-to-camel-case: true