Springboot上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫(kù)的過(guò)程
本文主要描述,Springboot-mybatis框架下上傳excel,并將之導(dǎo)入mysql數(shù)據(jù)庫(kù)的過(guò)程,如果用戶id已存在,則進(jìn)行更新修改數(shù)據(jù)庫(kù)中該項(xiàng)信息,由于用到的是前后端分離技術(shù),這里記錄的主要是后端java部分,通過(guò)與前端接口進(jìn)行對(duì)接實(shí)現(xiàn)功能,使用layui等前端框架與之對(duì)接,也可以自己寫(xiě)前端代碼,本文以Controller開(kāi)始,從導(dǎo)入過(guò)程開(kāi)始講述,其中包括字典表的轉(zhuǎn)換
1.在pom.xml文件中導(dǎo)入注解,主要利用POI
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2.Controller接口
@PostMapping("/save")
public String addUser(@RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
return sysService.batchImport(fileName, file);
} catch (MyException e) {
e.printStackTrace();
return e.getMessage();
}catch(Exception e){
e.printStackTrace();
return "文件異常,導(dǎo)入失敗";
}
}
3.服務(wù)層接口
boolean import(String fileName, MultipartFile file) throws Exception;
4.業(yè)務(wù)層實(shí)現(xiàn)類(lèi)
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public boolean import(String fileName, MultipartFile file) throws Exception {
Map<String, Integer> departmentMap = findDepartment();
Map<String, Integer> roleMap = findRole();
boolean notNull = false;
List<User> userList = new ArrayList<User>();
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
throw new MyException("上傳文件格式不正確");
}
boolean isExcel2003 = true;
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
isExcel2003 = false;
}
InputStream is = file.getInputStream();
Workbook wb = null;
if (isExcel2003) {
wb = new HSSFWorkbook(is);
} else {
wb = new XSSFWorkbook(is);
}
Sheet sheet = wb.getSheetAt(0);
if(sheet!=null){
notNull = true;
}
User user;
for (int r = 1; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
if (row == null){
continue;
}
user = new User();
if( row.getCell(0).getCellType() !=1){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID單元格格式請(qǐng)?jiān)O(shè)為文本格式)");
}
String id = row.getCell(0).getStringCellValue();
if(id==null || id.isEmpty()){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID未填寫(xiě))");
}
String name = row.getCell(1).getStringCellValue();
if(name==null || name.isEmpty()){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,姓名未填寫(xiě))");
}
String department = row.getCell(2).getStringCellValue();
if(departmentMap.get(department)==null){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此單位或單位未填寫(xiě))");
}
String role = row.getCell(3).getStringCellValue();
if(roleMap.get(role)==null){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此角色或角色未填寫(xiě))");
}
Date date;
if(row.getCell(4).getCellType() !=0){
throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,入職日期格式不正確或未填寫(xiě))");
}else{
date = row.getCell(4).getDateCellValue();
}
user.setId(id);
user.setName(name);
user.setDepartmentId((int) departmentMap.get(department));
user.setRoleId((int) roleMap.get(role));
user.setDate(date);
userList.add(user);
}
for (User user : userList) {
String id = user.getId();
int cnt = userMapper.selectById(id);
if (cnt == 0) {
userMapper.addUser(user);
} else {
userMapper.updateUserById(user);
}
}
return notNull;
}
總結(jié)
以上所述是小編給大家介紹的Springboot上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫(kù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 教你使用java將excel數(shù)據(jù)導(dǎo)入MySQL
- 如何將Excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)
- php通過(guò)PHPExcel導(dǎo)入Excel表格到MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
- PHP上傳Excel文件導(dǎo)入數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù)示例
- 使用phpexcel類(lèi)實(shí)現(xiàn)excel導(dǎo)入mysql數(shù)據(jù)庫(kù)功能(實(shí)例代碼)
- php導(dǎo)入excel文件到mysql數(shù)據(jù)庫(kù)的方法
- phpMyAdmin下將Excel中的數(shù)據(jù)導(dǎo)入MySql的圖文方法
- Excel數(shù)據(jù)導(dǎo)入Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- MySQL批量導(dǎo)入Excel數(shù)據(jù)(超詳細(xì))
相關(guān)文章
java Spring整合Freemarker的詳細(xì)步驟
本文對(duì)Spring整合Freemarker步驟做了詳細(xì)的說(shuō)明,按步驟操作一定可以整合通過(guò),這里提供給大家做參考2013-11-11
Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn)
這篇文章主要介紹了Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn),頁(yè)面靜態(tài)化是將原來(lái)的動(dòng)態(tài)網(wǎng)頁(yè)改為通過(guò)靜態(tài)化技術(shù)生成的靜態(tài)網(wǎng)頁(yè),FreeMarker?是一個(gè)用?Java?語(yǔ)言編寫(xiě)的模板引擎,它基于模板來(lái)生成文本輸,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06
SpringBoot集成echarts實(shí)現(xiàn)k線圖功能
ECharts是一款基于JavaScript的數(shù)據(jù)可視化圖表庫(kù),提供直觀,生動(dòng),可交互,可個(gè)性化定制的數(shù)據(jù)可視化圖表,本文給大家介紹了SpringBoot集成echarts實(shí)現(xiàn)k線圖功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-07-07
如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作
本文主要介紹了如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作。首先介紹了MyBatis框架的基本概念和使用方法,然后分別介紹了如何使用MyBatis實(shí)現(xiàn)增刪改查操作。最后,通過(guò)一個(gè)簡(jiǎn)單的示例演示了如何使用MyBatis框架實(shí)現(xiàn)CRUD操作。2023-05-05
Java?中導(dǎo)入excel時(shí)使用?trim()?無(wú)法去除空格的問(wèn)題解決方案
這篇文章主要介紹了Java中導(dǎo)入excel時(shí)使用trim()無(wú)法去除空格的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Activiti開(kāi)發(fā)環(huán)境的搭建過(guò)程詳解
這篇文章主要介紹了Activiti開(kāi)發(fā)環(huán)境的搭建過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
WMTS中TileMatrix與ScaleDenominator淺析
這篇文章主要為大家介紹了WMTS中TileMatrix與ScaleDenominator淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Spring?Boot實(shí)現(xiàn)文件上傳下載
這篇文章主要為大家詳細(xì)介紹了Spring?Boot實(shí)現(xiàn)文件上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

