springboot導入Excel數據

1.導入Excel一般都會給一個Excel數據模板,模板下載請看本博客另一篇模板下載文章

2.數據庫數據導入效果圖

3.前臺簡陋圖

4,需要引入mybatis、mysql、poi、io等依賴

<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.13</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.6</version>
		</dependency>

 5.Excel工具類

工具類上傳csdn了,有積分的可以自行下載,沒積分的聯繫我發你們郵箱(僅限2020年3月1日之前免費提供,2020.3月1日之後加微信獲取工具類,微信二維碼在文章最下方

下載地址:https://download.csdn.net/download/royal1235/11166530

6.功能結構圖:

7.controller代碼

package com.ansheng.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.MultipartConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ansheng.entity.Notice;
import com.ansheng.service.QueryTestService;
import com.ansheng.util.ExcelUtils;

@RestController
public class UploadFile {
     @Autowired
     private QueryTestService queryTestService;
	 @RequestMapping("/uploadFile")
	 public Map<String,Object> bathAddData(@RequestParam("file") MultipartFile file){ 
		 InputStream is = null;
		 Map<String,Object> map=new HashMap<String,Object>();
		    try {
	            is = file.getInputStream();
	            List<Object> entityList = ExcelUtils.importDataFromExcel(new Notice(),is,file.getOriginalFilename());
	            System.out.println(entityList.size()+"---------");
	            if (entityList.size() == 0){
	                map.put("status", "0");
	                map.put("msg", "導入數據不能爲空");
	            }else if (entityList.size() >10){
	            	map.put("status", "1");
		            map.put("msg", "數據條數不準大於10條");
	            } else {
	                Map<String,Object> Resmap= queryTestService.batchAddData(entityList);
	               // System.out.println(Resmap.get("success")+"---"+Resmap.get("data"));
	                int totalNum = entityList.size();
	                int failed = totalNum -Integer.parseInt(Resmap.get("success").toString());
	                map.put("status", "2");
	                map.put("msg", "導入成功");
	                map.put("success",Resmap.get("success"));
	                map.put("totalNum",totalNum);
	                map.put("failed",failed);
	            }

	        } catch (Exception e) {
	            e.printStackTrace();
	        }    finally {
	            is = null;
	        }
		    System.out.println(map.get("msg")+"--"+map.get("status"));
		 return map;
	 }
}

8.service代碼

Map<String,Object>  batchAddData(List<Object> entityList);

9.serviceImpl代碼

	@Override
	public Map<String, Object> batchAddData(List<Object> entityList) {
		// TODO Auto-generated method stub
		int sum=entityList.size();
		int success=0;
		StringBuilder successIds = new StringBuilder("");
        StringBuilder failedIds = new StringBuilder("");
        List<Notice> lis=new ArrayList<Notice>();
        Map<String,Object> map=new HashMap<String,Object>();
		for (Object object : entityList) {
			Notice notice=(Notice) object;
			int i=queryTestMapper.insertDataToTable(notice);
			if(i>0) {
				success++;
			}else {
				//失敗
				lis.add(notice);
			}
		}
		map.put("success",success);
		map.put("data",lis);
		return map;
	}

10.mapper接口

int insertDataToTable(Notice notice);

11.xml

 <insert id="insertDataToTable" parameterType="com.ansheng.entity.Notice">
	 insert into notice (id,descr,createtime,name,updatetime,delstatus) values (#{id},#{descr},#{createtime},#{name},#{updatetime},#{delstatus})
	 </insert>

注意:sql可以使用動態sql進行導入數據

 

需要工具類的掃碼加微信:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章