欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java excel數(shù)據(jù)導(dǎo)入mysql的實(shí)現(xiàn)示例詳解

 更新時(shí)間:2022年08月12日 09:44:24   作者:赫赫有安  
今天教大家如何使用Java將excel數(shù)據(jù)導(dǎo)入MySQL,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴呢很有幫助,需要的朋友可以參考下

springBoot實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入到mysql數(shù)據(jù)庫

一.新建Excel表并插入測試所需數(shù)據(jù)

二.新建springBoot工程

修改pom.xml文件以及application.properties。

pom.xml

<!--缺少此jar包,導(dǎo)致@Mapper注解無效-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<!--java對象狀態(tài)自動(dòng)映射到關(guān)系數(shù)據(jù)庫中數(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
#配置通過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ù)庫自動(dòng)創(chuàng)建實(shí)體類的表,創(chuàng)建完之后會(huì)仔細(xì)發(fā)現(xiàn)數(shù)據(jù)庫里的字段和實(shí)體類里的字段順序是不一樣,會(huì)出現(xiàn)亂序狀態(tài),這是因?yàn)閔ibernate源碼中用的是TreeMap存儲(chǔ)實(shí)體類字段,TreeMap屬性是無序的。具體的解決辦法如下:

1.找到源碼文件

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

3.將上圖標(biāo)識(shí)的TreeMap 修改為LinkedHashMap修改好之后重新啟動(dòng)項(xiàng)目,會(huì)發(fā)現(xiàn)實(shí)體類和數(shù)據(jù)庫表中順序?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è)對象,用來裝填從頁面上傳的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,對應(yīng)一個(gè)Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一個(gè)sheet,對應(yīng)Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("test");
        // 第三步,在sheet中添加表頭第0行,注意老版本poi對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);
        // 第五步,寫入實(shí)體數(shù)據(jù) 實(shí)際應(yīng)用中這些數(shù)據(jù)從數(shù)據(jù)庫得到,
        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ù)庫的效果如下:

到此這篇關(guān)于Java excel數(shù)據(jù)導(dǎo)入mysql的實(shí)現(xiàn)示例詳解的文章就介紹到這了,更多相關(guān)Java excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • idea創(chuàng)建maven父子工程導(dǎo)致子工程無法導(dǎo)入父工程依賴

    idea創(chuàng)建maven父子工程導(dǎo)致子工程無法導(dǎo)入父工程依賴

    創(chuàng)建maven父子工程時(shí)遇到一個(gè)問題,本文主要介紹了idea創(chuàng)建maven父子工程導(dǎo)致子工程無法導(dǎo)入父工程依賴,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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)

    這篇文章主要介紹了Java的接口調(diào)用時(shí)的權(quán)限驗(yàn)證功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java中使用Apache POI讀取word文件簡單示例

    Java中使用Apache POI讀取word文件簡單示例

    這篇文章主要介紹了Java中使用Apache POI讀取word文件簡單示例,本文著重介紹了一些必要條件,然后給出一個(gè)簡單讀取示例,需要的朋友可以參考下
    2015-06-06
  • 詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ)

    詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ)

    這篇文章主要介紹了詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Intellij IDEA 2018配置Java運(yùn)行環(huán)境的方法步驟

    Intellij IDEA 2018配置Java運(yùn)行環(huán)境的方法步驟

    這篇文章主要介紹了Intellij IDEA 2018配置Java運(yùn)行環(huán)境的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解JNI到底是什么

    詳解JNI到底是什么

    JNI是Java Native Interface的縮寫,通過使用 Java本地接口書寫程序,可以確保代碼在不同的平臺(tái)上方便移植。從Java1.1開始,JNI標(biāo)準(zhǔn)成為java平臺(tái)的一部分,它允許Java代碼和其他語言寫的代碼進(jìn)行交互
    2021-06-06
  • IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序的詳細(xì)流程

    IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序的詳細(xì)流程

    這篇文章主要介紹了IDEA遠(yuǎn)程部署調(diào)試Java應(yīng)用程序,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • SpringMVC?@RequestMapping注解屬性詳細(xì)介紹

    SpringMVC?@RequestMapping注解屬性詳細(xì)介紹

    通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • SpringBoot中VO/DTO/PO的具體使用

    SpringBoot中VO/DTO/PO的具體使用

    VO/DTO/PO等實(shí)體類中字段常常會(huì)存在多數(shù)相同,根據(jù)業(yè)務(wù)需求少數(shù)不同,本文主要介紹了SpringBoot中VO/DTO/PO的具體使用,感興趣的可以了解一下
    2024-03-03
  • java?CompletableFuture異步任務(wù)編排示例詳解

    java?CompletableFuture異步任務(wù)編排示例詳解

    這篇文章主要為大家介紹了java?CompletableFuture異步任務(wù)編排示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評論