咱們在前面的章節已經講了如何用jpa或者mybatis來操做mysql數據庫。這一節咱們就來結合具體案例,來說解下excel表格的上傳,與excel表裏數據的識別。並把識別後的數據批量導入到mysql數據庫html
jpa的使用咱們在上一節已經給你們講過了,不知道如何建立的親,記得去翻看上一節的文章:《java入門018~springboot2使用JPA操做mysql數據庫》java
1,使用idea建立springboot項目 mysql
點擊finish便可<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 操做excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
記得從新Reimport web
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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 java.util.List;
import java.util.Map;
/**
* 2019-10-07 18:35
* author: 編程小石頭
* wechat:2501902696
* desc: 把excel裏的數據保存到mysql數據庫裏
*/
@Controller
public class ExcelController {
@GetMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/uploadExcel")
@ResponseBody
public String uploadExcel(@RequestParam("file") MultipartFile file,
Map<String, Object> map) {
String name = file.getOriginalFilename();
if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
return "文件格式錯誤";
}
List<ExcelBean> list = null;
try {
list = ExcelUtils.excelToShopIdList(file.getInputStream());
if (list == null || list.size() <= 0) {
return "導入的數據爲空";
}
//excel的數據保存到數據庫
try {
for (ExcelBean excel : list) {
System.out.println(excel.toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
return e.getMessage();
}
} catch (Exception e) {
System.out.println(e.getMessage());
return e.getMessage();
}
return "保存成功";
}
}
複製代碼
簡單講解下上面代碼的步驟spring
package com.example.demo;
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.ss.usermodel.WorkbookFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/*
* 操做excel
* */
public class ExcelUtils {
public static List<ExcelBean> excelToShopIdList(InputStream inputStream) {
List<ExcelBean> list = new ArrayList<>();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(inputStream);
inputStream.close();
//工做表對象
Sheet sheet = workbook.getSheetAt(0);
//總行數
int rowLength = sheet.getLastRowNum();
// System.out.println("總行數有多少行" + rowLength);
//工做表的列
Row row = sheet.getRow(0);
//總列數
int colLength = row.getLastCellNum();
// System.out.println("總列數有多少列" + colLength);
//獲得指定的單元格
Cell cell = row.getCell(0);
for (int i = 1; i <= rowLength; i++) {
ExcelBean jiFenExcel = new ExcelBean();
row = sheet.getRow(i);
for (int j = 0; j < colLength; j++) {
//列: 0姓名 1人員編號 2餐補 3部門
cell = row.getCell(j);
// System.out.print(cell + ",");
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
String data = cell.getStringCellValue();
data = data.trim();
// System.out.print(data);
// if (StringUtils.isNumeric(data)) {
if (j == 0) {
jiFenExcel.setName(data);
} else if (j == 1) {
jiFenExcel.setJobNum(data);
} else if (j == 2) {
jiFenExcel.setCanBu(Integer.parseInt(data));
} else if (j == 3) {
jiFenExcel.setBumen(data);
}
// }
}
}
list.add(jiFenExcel);
// System.out.println("====");
}
} catch (Exception e) {
}
return list;
}
}
複製代碼
咱們的index.html位於resources下的static裏 sql
代碼以下<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上傳excel</title>
</head>
<body>
<h1>上傳excel文件並存入到mysql數據庫</h1>
<form action="/uploadExcel" method="post" enctype="multipart/form-data">
<p>文件上傳</p>
<input type="file" name="file">
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
複製代碼
運行起來後,咱們經過index.html網頁,來上傳咱們桌面的excel文件。 數據庫
咱們excel表格內容以下 apache
咱們經過上面第七步,上傳excel到服務器後,識別出來的數據以下 經過上圖能夠看出,咱們成功的識別出了excel裏的數據。今天就先到這裏,下一節來說如何把這些數據存到mysql數據庫裏。編程