Java excel數(shù)據(jù)導(dǎo)入mysql的實(shí)現(xiàn)示例詳解
springBoot實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入到mysql數(shù)據(jù)庫(kù)
一.新建Excel表并插入測(cè)試所需數(shù)據(jù)

二.新建springBoot工程
修改pom.xml文件以及application.properties。
pom.xml
<!--缺少此jar包,導(dǎo)致@Mapper注解無(wú)效-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--java對(duì)象狀態(tài)自動(dòng)映射到關(guān)系數(shù)據(jù)庫(kù)中數(shù)據(jù)上-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--實(shí)現(xiàn)類與xml之間的相互轉(zhuǎn)換-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<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>application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.url=jdbc:mysql://localhost:3306/excel?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
#配置通過(guò)jpa自動(dòng)創(chuàng)建表
spring.jpa.hibernate.ddl-auto=create
#打印SQL
spring.jpa.show-sql=true
三.創(chuàng)建實(shí)體類進(jìn)行關(guān)系的映射
啟動(dòng)項(xiàng)目就會(huì)在數(shù)據(jù)庫(kù)自動(dòng)創(chuàng)建實(shí)體類的表,創(chuàng)建完之后會(huì)仔細(xì)發(fā)現(xiàn)數(shù)據(jù)庫(kù)里的字段和實(shí)體類里的字段順序是不一樣,會(huì)出現(xiàn)亂序狀態(tài),這是因?yàn)閔ibernate源碼中用的是TreeMap存儲(chǔ)實(shí)體類字段,TreeMap屬性是無(wú)序的。具體的解決辦法如下:
1.找到源碼文件

2.在當(dāng)前項(xiàng)目中創(chuàng)建一個(gè)和源碼類一樣的包結(jié)構(gòu)和一樣名字的類,直接復(fù)制源碼文件所有代碼到新建的類中。

3.將上圖標(biāo)識(shí)的TreeMap 修改為L(zhǎng)inkedHashMap修改好之后重新啟動(dòng)項(xiàng)目,會(huì)發(fā)現(xiàn)實(shí)體類和數(shù)據(jù)庫(kù)表中順序?qū)R了。

4.Excel的DAO類接口,與Excel有關(guān)的持久化操作方法。

5.創(chuàng)建service層接口,與Excel有關(guān)的業(yè)務(wù)邏輯方法。

6.service層方法的實(shí)現(xiàn),與用戶信息有關(guān)的業(yè)務(wù)邏輯方法。詳細(xì)代碼如下:
package com.wxy.excel.service;
import com.wxy.excel.mapper.ExcelRepository;
import com.wxy.excel.entity.Excel;
import com.wxy.excel.mapper.UserMapper;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class ExcelServiceImpl implements ExcelService{
@Autowired
private ExcelRepository excelRepository;
@Autowired
private UserMapper userMapper;
@Override
public boolean getExcel(MultipartFile file) throws Exception {
List<Excel> list = new ArrayList<Excel>();
//1.得到上傳的表
Workbook workbook2 = WorkbookFactory.create(file.getInputStream());
//2.獲取test工作表 注意test就是excel下面的sheet名稱
Sheet sheet2 = workbook2.getSheet("test");
//3.獲取表的總行數(shù)
int num = sheet2.getLastRowNum();
//4.獲取表總列數(shù)
int col = sheet2.getRow(0).getLastCellNum();
//5.遍歷excel每一行
for (int j = 0; j <num; j++) {
Row row1 = sheet2.getRow(j);
// 如果單元格中有數(shù)字或者其他格式的數(shù)據(jù),則調(diào)用setCellType()轉(zhuǎn)換為string類型
Cell cell1 = row1.getCell(0);
cell1.setCellType(CellType.STRING);
//獲取表中第i行,第2列的單元格
Cell cell2 = row1.getCell(1);
cell2.setCellType(CellType.STRING);
//獲取excel表的第i行,第3列的單元格
Cell cell3 = row1.getCell(2);
cell3.setCellType(CellType.STRING);
Cell cell4 = row1.getCell(3);
cell4.setCellType(CellType.STRING);
Cell cell5 = row1.getCell(4);
cell5.setCellType(CellType.STRING);
//這里new 一個(gè)對(duì)象,用來(lái)裝填從頁(yè)面上傳的Excel數(shù)據(jù),字段根據(jù)上傳的excel決定
Excel excel= new Excel();
excel.setId(cell1.getStringCellValue());
excel.setUsername(cell2.getStringCellValue());
excel.setEmail(cell3.getStringCellValue());
excel.setPassword(cell4.getStringCellValue());
excel.setRole(cell5.getStringCellValue());
list.add(excel);
System.out.println("excel"+excel);
}
excelRepository.saveAll(list);
return true;
}
@Override
public void exportExcel(HttpServletResponse response) throws IOException {
// 第一步,創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("test");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)Excel的行數(shù)列數(shù)有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中
HSSFCellStyle style = wb.createCellStyle();
// 創(chuàng)建一個(gè)居中格式
style.setAlignment(HorizontalAlignment.CENTER);
/*此處根據(jù)情況自己自定義樣式*/
HSSFCell cell = row.createCell(0);
cell.setCellValue("ID");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("郵箱");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("密碼");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("角色");
cell.setCellStyle(style);
// 第五步,寫(xiě)入實(shí)體數(shù)據(jù) 實(shí)際應(yīng)用中這些數(shù)據(jù)從數(shù)據(jù)庫(kù)得到,
List<Excel> list = userMapper.getAllUser();
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
Excel excel = list.get(i);
// 創(chuàng)建單元格,并設(shè)置值
row.createCell(0).setCellValue(excel.getId());
row.createCell(1).setCellValue(excel.getUsername());
row.createCell(2).setCellValue(excel.getEmail());
row.createCell(3).setCellValue(excel.getPassword());
row.createCell(4).setCellValue(excel.getRole());
}
//第六步,輸出Excel文件
OutputStream output = response.getOutputStream();
response.reset();
//設(shè)置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
// 獲取當(dāng)前系統(tǒng)時(shí)間
String fileName = df.format(new Date());
//設(shè)置導(dǎo)出文件表頭(即文件名)
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
//設(shè)置返回內(nèi)容類型
response.setContentType("application/msexcel");
wb.write(output);
output.close();
}
}7.Controller層實(shí)現(xiàn)。

8.前端html實(shí)現(xiàn).

9.最終把excel數(shù)據(jù)導(dǎo)入到mysql數(shù)據(jù)庫(kù)的效果如下:

到此這篇關(guān)于Java excel數(shù)據(jù)導(dǎo)入mysql的實(shí)現(xiàn)示例詳解的文章就介紹到這了,更多相關(guān)Java excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea創(chuàng)建maven父子工程導(dǎo)致子工程無(wú)法導(dǎo)入父工程依賴
創(chuàng)建maven父子工程時(shí)遇到一個(gè)問(wèn)題,本文主要介紹了idea創(chuàng)建maven父子工程導(dǎo)致子工程無(wú)法導(dǎo)入父工程依賴,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java的接口調(diào)用時(shí)的權(quán)限驗(yàn)證功能的實(shí)現(xiàn)
這篇文章主要介紹了Java的接口調(diào)用時(shí)的權(quán)限驗(yàn)證功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java中使用Apache POI讀取word文件簡(jiǎn)單示例
這篇文章主要介紹了Java中使用Apache POI讀取word文件簡(jiǎn)單示例,本文著重介紹了一些必要條件,然后給出一個(gè)簡(jiǎn)單讀取示例,需要的朋友可以參考下2015-06-06
詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ)
這篇文章主要介紹了詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Intellij IDEA 2018配置Java運(yùn)行環(huán)境的方法步驟
這篇文章主要介紹了Intellij IDEA 2018配置Java運(yùn)行環(huán)境的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序的詳細(xì)流程
這篇文章主要介紹了IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
SpringMVC?@RequestMapping注解屬性詳細(xì)介紹
通過(guò)@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
java?CompletableFuture異步任務(wù)編排示例詳解
這篇文章主要為大家介紹了java?CompletableFuture異步任務(wù)編排示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

