亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出

 更新時(shí)間:2021年12月21日 09:58:38   作者:Bug終結(jié)者.  
在項(xiàng)目中我們常常需要Excel文件的導(dǎo)入與導(dǎo)出,手動(dòng)輸入相對(duì)有些繁瑣,所以本文教大家如何在Java中輕松導(dǎo)入與導(dǎo)出Excel文件,感興趣的可以學(xué)習(xí)一下

引言

項(xiàng)目中需要Excel文件的導(dǎo)入與導(dǎo)出Excel并下載,例如,導(dǎo)入員工信息,導(dǎo)出員工信息,手動(dòng)輸入比較繁瑣,所以本篇博文教大家如何在Java中導(dǎo)入Excel文件與導(dǎo)出Excel文件

技術(shù)棧

Excel工具:EasyExcel

選用框架:Spring、Spring MVC、MyBatis(SSM)

項(xiàng)目構(gòu)建管理工具:Maven

需求:

1.要求利用excel工具實(shí)現(xiàn)員工信息的導(dǎo)入與導(dǎo)出

2.導(dǎo)出要求為輸出到指定位置并下載

3.導(dǎo)入文件導(dǎo)入后,存入數(shù)據(jù)庫,并顯示在頁面

4.導(dǎo)出文件,點(diǎn)擊導(dǎo)出后寫入指定地址,并下載該文件

效果圖

項(xiàng)目結(jié)構(gòu)

核心源碼

導(dǎo)入阿里巴巴EasyExcel依賴

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.6</version>
</dependency>

這里采用EasyExcel,為什么不采用POI呢?

因?yàn)镋asyExcel是對(duì)POI做的一個(gè)升級(jí),POI相對(duì)于笨重,EasyExcel去除了一些POI比較繁瑣的東西,所以EasyExcel比較輕量級(jí),所以本文采用EasyExcel

EasyExcel是阿里巴巴的產(chǎn)品,POI是Apache基金會(huì)的開源產(chǎn)品,EasyExcel對(duì)POI做了一個(gè)升級(jí)

核心實(shí)體類

package com.wanshi.spring.entity;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

    @ExcelIgnore
    private String noid;

    @ColumnWidth(20)
    @ExcelProperty("員工姓名")
    private String emp_name;

    @ColumnWidth(20)
    @ExcelProperty("員工年齡")
    private Integer emp_age;

    @ExcelIgnore
    private Integer emp_sex;

    //冗余字段
    @ColumnWidth(20)
    @ExcelProperty("員工性別")
    private String str_emp_sex;

    @ColumnWidth(20)
    @ExcelProperty("員工工資")
    private Double emp_salary;

    @ColumnWidth(20)
    @ExcelProperty("員工住址")
    private String emp_address;

    @ColumnWidth(20)
    @ExcelProperty("員工崗位")
    private String emp_position;

    //分頁相關(guān),當(dāng)前頁與每頁的數(shù)據(jù)條數(shù)
    @ExcelIgnore
    private Integer pageNum;
    @ExcelIgnore
    private Integer pageSize;
}

核心監(jiān)聽器類

EmployeeListener類:

package com.wanshi.spring.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wanshi.spring.entity.Employee;

import java.util.ArrayList;
import java.util.List;

public class EmployeeReadListener extends AnalysisEventListener<Employee> {

    //員工集合
    private static List<Employee> employeeList = new ArrayList<>();

    // 每讀一樣,會(huì)調(diào)用該invoke方法一次
    @Override
    public void invoke(Employee data, AnalysisContext context) {
        employeeList.add(data);
        System.out.println("解析到一條數(shù)據(jù):" + data);
    }

    // 全部讀完之后,會(huì)調(diào)用該方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("全部解析完成");
    }

    /**
     * 返回讀取到的員工集合
     * @return
     */
    public static List<Employee> getStudentList() {
        return employeeList;
    }
}

EasyExcel導(dǎo)入文件

Test測(cè)試類實(shí)現(xiàn)文件導(dǎo)入并存入數(shù)據(jù)庫

@Test
public void test1(){
    ExcelReaderBuilder workBook = EasyExcel.read
        ("C:\\Users\\王會(huì)稱\\Desktop\\員工.xlsx", Employee.class, new EmployeeReadListener());

    // 封裝工作表
    ExcelReaderSheetBuilder sheet1 = workBook.sheet();
    // 讀取
    sheet1.doRead();

    //寫入數(shù)據(jù)庫
    List<Employee> studentList = EmployeeReadListener.getStudentList();
    for (Employee employee : studentList) {
        employee.setNoid(PbSecretUtils.uuid());
        employeeMapper.insert(employee);
    }
}

通過頁面點(diǎn)擊導(dǎo)入文件并存入數(shù)據(jù)庫

EmployeeController類:

@PostMapping("/import_employee_excel")
public String importEmployeeExcel(MultipartFile emp_excel) {
    employeeService.importExcel(emp_excel);
    return "redirect:/employee/list";
}

EmployeeService類:

/**
     * 獲取用戶選擇的文件并將文件存入指定位置再將數(shù)據(jù)存入數(shù)據(jù)庫
     * @param emp_excel
     * @return
     */
public Integer importExcel(MultipartFile emp_excel) {
    try {
        String fileName = FileUploadUtil.upload(emp_excel, "");
        ExcelReaderBuilder workBook = EasyExcel.read
            (GlobalSet.upload_url+fileName, Employee.class, new EmployeeReadListener());

        // 封裝工作表
        ExcelReaderSheetBuilder sheet1 = workBook.sheet();
        // 讀取
        sheet1.doRead();

        List<Employee> studentList = EmployeeReadListener.getStudentList();
        for (Employee employee : studentList) {
            employee.setNoid(PbSecretUtils.uuid());
            if ("男".equals(employee.getStr_emp_sex())) {
                employee.setEmp_sex(1);
            } else {
                employee.setEmp_sex(2);
            }
            employeeMapper.insert(employee);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

EasyExcel導(dǎo)出文件

Test測(cè)試類導(dǎo)出文件到指定文件

@Test
public void test2() throws FileNotFoundException {

    List<Employee> employeeList = initData();


    ExcelWriterBuilder workBook = EasyExcel.write(GlobalSet.download_url, Employee.class);

    // sheet方法參數(shù): 工作表的順序號(hào)(從0開始)或者工作表的名字
    workBook.sheet("測(cè)試數(shù)據(jù)表").doWrite(employeeList);
    System.out.println("寫入完成!");
}

/**
     * 生成測(cè)試數(shù)據(jù)
     * @return
     */
public List<Employee> initData() {
    List<Employee> employeeList = new ArrayList<>();
    for (int i = 1; i < 100; i++) {
        Employee employee = new Employee();
        employee.setEmp_name("小王說:"+i);
        employee.setEmp_age(19);
        if (i % 10 == 0) {
            employee.setEmp_sex(1);
        } else {
            employee.setEmp_sex(2);
        }
        employee.setEmp_salary(19999.00+i);
        employee.setEmp_address("北京市朝陽區(qū)"+i);
        employee.setEmp_position("Java高級(jí)工程師");
        employeeList.add(employee);
    }
    return employeeList;
}

通過頁面導(dǎo)出到指定文件后并下載文件

EmployeeController類

@GetMapping("/export_employee_excel")
    public void exportEmployeeExcel(HttpServletResponse response) {
        try {
            employeeService.exportEmployeeExcel(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

EmployeeService類:

public void exportEmployeeExcel(HttpServletResponse response) throws IOException {
        List<Employee> kspwStudentSeatList = list();
        try {
            ExcelWriterBuilder workBook = EasyExcel.write(GlobalSet.download_url, Employee.class);
            // sheet方法參數(shù): 工作表的順序號(hào)(從0開始)或者工作表的名字
            workBook.sheet("員工信息").doWrite(kspwStudentSeatList);
            downloadTempalate(response);
            System.out.println("寫入完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下載文件
     * @param response
     * @throws IOException
     */
    public static void downloadTempalate(HttpServletResponse response) throws IOException {
        // 告訴瀏覽器用什么軟件可以打開此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下載文件的默認(rèn)名稱
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("員工信息.xlsx", "utf-8"));
        //4. 創(chuàng)建輸入、輸出流
        FileInputStream input = new FileInputStream(GlobalSet.download_url);
        ServletOutputStream sos = response.getOutputStream();

        //IO流獲取文件的字節(jié)流,然后再響應(yīng)給瀏覽器
        byte[] arr = new byte[1024];
        int res = 0;
        while((res = input.read(arr)) > 0){
            //將讀取的內(nèi)容輸出到輸出流中
            sos.write(arr, 0, res);
        }

        input.close();
        sos.close();
    } 

以上就是Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出的詳細(xì)內(nèi)容,更多關(guān)于Java EasyExcel文件的導(dǎo)入導(dǎo)出的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 關(guān)于Jar包部署命令全面解析

    關(guān)于Jar包部署命令全面解析

    這篇文章主要介紹了Jar包部署命令全面解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 快速解決idea打開某個(gè)項(xiàng)目卡住的問題

    快速解決idea打開某個(gè)項(xiàng)目卡住的問題

    這篇文章主要介紹了解決idea打開某個(gè)項(xiàng)目卡住的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作

    springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作

    這篇文章主要介紹了springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下
    2021-02-02
  • 解析spring boot與ireport 整合問題

    解析spring boot與ireport 整合問題

    本文通過實(shí)例代碼給大家介紹了spring boot 與 ireport 整合問題,關(guān)于pom文件依賴的問題通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-10-10
  • Spring Security 實(shí)現(xiàn)用戶名密碼登錄流程源碼詳解

    Spring Security 實(shí)現(xiàn)用戶名密碼登錄流程源碼詳解

    在服務(wù)端的安全管理使用了Spring Security,用戶登錄成功之后,Spring Security幫你把用戶信息保存在Session里,但是具體保存在哪里,要是不深究你可能就不知道,今天小編就帶大家具體了解一下Spring Security實(shí)現(xiàn)用戶名密碼登錄的流程
    2021-11-11
  • 常用的Java數(shù)據(jù)結(jié)構(gòu)知識(shí)點(diǎn)匯總

    常用的Java數(shù)據(jù)結(jié)構(gòu)知識(shí)點(diǎn)匯總

    這篇文章主要介紹了常用的Java數(shù)據(jù)結(jié)構(gòu)知識(shí)點(diǎn)匯總,數(shù)據(jù)結(jié)構(gòu)分線性數(shù)據(jù)結(jié)構(gòu)和非線性數(shù)據(jù)結(jié)構(gòu),下面對(duì)此作詳細(xì)介紹,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)或工作有所幫助
    2022-03-03
  • 使用Spring自定義命名空間

    使用Spring自定義命名空間

    這篇文章主要介紹了使用Spring自定義命名空間方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringCloud gateway如何修改返回?cái)?shù)據(jù)

    SpringCloud gateway如何修改返回?cái)?shù)據(jù)

    這篇文章主要介紹了SpringCloud gateway如何修改返回?cái)?shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 基于SpringBoot啟動(dòng)類靜態(tài)資源路徑問題

    基于SpringBoot啟動(dòng)類靜態(tài)資源路徑問題

    這篇文章主要介紹了SpringBoot啟動(dòng)類靜態(tài)資源路徑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 你知道Java判斷字符串是否為數(shù)字的多種方式嗎

    你知道Java判斷字符串是否為數(shù)字的多種方式嗎

    在編程的時(shí)候經(jīng)常遇到要判斷一個(gè)字符串中的字符是否是數(shù)字(0-9),所以下面這篇文章主要給大家介紹了關(guān)于Java判斷字符串是否為數(shù)字的多種方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07

最新評(píng)論