国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫)-----java

這篇具有很好參考價值的文章主要介紹了Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫)-----java。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

一,EasyExcel官網(wǎng):

可以學(xué)習(xí)一些新知識:

EasyExcel官方文檔 - 基于Java的Excel處理工具 | Easy Excel

二,為什么要使用easyexcle

excel的一些優(yōu)點(diǎn)和缺點(diǎn)

java解析excel的框架有很多 :

poi jxl,存在問題:非常的消耗內(nèi)存,

easyexcel 我們遇到再大的excel都不會出現(xiàn)內(nèi)存溢出的問題 能夠?qū)⒁粋€原本3M excel文件,poi來操作將會占用內(nèi)存

100MB,使用easyexcel減低到幾Mb,使用起來更加簡單

poi讀 1、創(chuàng)建xsshworkbook/hssfworkbook(inputstream in)

2、讀取sheet

3、拿到當(dāng)前sheet所有行row

4、通過當(dāng)前行去拿到對應(yīng)的單元格的值


easyexcel擬解決的問題

1.excel讀寫時內(nèi)存溢出

2.使用簡單

3.excel格式解析


工作原理

Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫)-----java,excel,java

三,項目的步驟

依賴包:

基本上要用的的依賴包:

       <!--		mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!--	mysql	-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
        <!--        reds依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>
        <!--     連接池依賴   -->
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>
<!--        數(shù)據(jù)池-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.14</version>
        </dependency>
<!--    mapper    -->
        <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.0.2</version>
        </dependency>

        <!--    easyexcel依賴    -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!--log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

一,導(dǎo)出(寫)流程寫法:

1. Server接口:

package com.example.excel01.generator.service;

import com.example.excel01.generator.domain.Excel;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
* @author zeng
* @description 針對表【excel】的數(shù)據(jù)庫操作Service
* @createDate 2023-08-02 11:10:56
*/
public interface ExcelService extends IService<Excel> {
    public Integer getCount(); //總條數(shù)
    public List<Excel> getListBYPage(Integer pageOn); //分頁查詢

}

2. 2.ServiceImpl類:

package com.example.excel01.generator.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.excel01.generator.domain.Excel;
import com.example.excel01.generator.mapper.ExcelMapper;
import com.example.excel01.generator.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author zeng
* @description 針對表【excel】的數(shù)據(jù)庫操作Service實(shí)現(xiàn)
* @createDate 2023-08-02 11:10:56
*/
@Service
@Transactional
public class ExcelServiceImpl extends ServiceImpl<ExcelMapper, Excel>
    implements ExcelService {
    @Autowired
    private ExcelMapper excelMapper;
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Integer getCount() {
        return excelMapper.selectCount(null);
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Excel> getListBYPage(Integer pageOn) {
        Integer begin=(pageOn-1)*50000+1;
        //第幾頁顯示多少數(shù)據(jù)
        return excelMapper.ListPage(begin,50000);
    }
}

3. 3.Test測試:

    /**
     * 寫數(shù)據(jù)
     * */
    @Test
    void contextLoads4(){
        //保存路徑
        String path="E:\\aaa\\ciy2.xls";
        //查詢數(shù)據(jù)總條數(shù)
        Integer count = excelService.getCount();
        //創(chuàng)建easyexcel的寫出類構(gòu)造器 參數(shù) 告訴構(gòu)造器 我的excel將來要寫到哪里 以及excel中數(shù)據(jù)是基于哪個java對象模板創(chuàng)建的
        ExcelWriter excelWriter = EasyExcel.write(path, Excel.class).build();
        //判斷一頁能放多少條數(shù)據(jù)
        Integer sheetNum = count % 50000 == 0 ? count / 50000 : count / 50000 + 1;
        log.info("sheetNum=={}",sheetNum);
        for(int i=1;i<sheetNum;i++){
            log.info("第"+i+"批次");
            //分頁查詢數(shù)據(jù)庫
            List<Excel> listPage = excelService.getListBYPage(i);
            //創(chuàng)建sheet構(gòu)造器
            WriteSheet sheet = EasyExcel.writerSheet("test").build();
            //使用excel對象將數(shù)據(jù)寫入到創(chuàng)建的sheet當(dāng)中
            excelWriter.write(listPage,sheet);
        }
        excelWriter.finish();
        log.info("導(dǎo)出成功");
    }

二,導(dǎo)入(讀)流程寫法

1. 監(jiān)聽器:

讀取數(shù)據(jù)要通過監(jiān)聽器來實(shí)現(xiàn),監(jiān)聽讀取的每一條數(shù)據(jù):


監(jiān)聽器:

package com.example.excel01.generator.listenner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.excel01.generator.domain.FmAddress;
import com.example.excel01.generator.service.FmAddressService;
import lombok.extern.slf4j.Slf4j;

/**
 * 監(jiān)聽器
 */

@Slf4j
public class EasyExcelListenner extends AnalysisEventListener<FmAddress> {

    static Integer a = 0;

    @Override
    public void invoke(FmAddress fmAddress, AnalysisContext analysisContext) {
        ++a;
        log.info("a={}",a);
        if (a == 1000) {
            try {
                a=0;
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        log.info(String.valueOf(fmAddress));
        //獲取bean
        FmAddressService fmAddressService = SpringJobBeanFactory.getBean(FmAddressService.class);
        fmAddressService.insert(fmAddress);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}

a. 知識點(diǎn)普及:

1.監(jiān)聽器我們是繼承Excel中的AnalysisEventListener方法來

2.該監(jiān)聽器是不被Spring (bean) 容器管理的,我要手動注入容器


2. 手動將監(jiān)聽器注入Spring容器:

package com.example.excel01.generator.listenner;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName: SpringJobBeanFactory*/
@Component
public class SpringJobBeanFactory implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringJobBeanFactory.applicationContext=applicationContext;

    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        if (applicationContext == null){
            return null;
        }
        return (T)applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T>  name) throws BeansException {
        if (applicationContext == null){
            return null;
        }
        return applicationContext.getBean(name);
    }
}
a. 普及知識點(diǎn):

手動注入bean 調(diào)用的是ApplicationContextAware接口文章來源地址http://www.zghlxwxcb.cn/news/detail-647777.html


3.Test測試類:

    /**
     * 讀數(shù)據(jù)
     */
    @Test
    void contextLoads5(){
        //保存路徑
        String path="E:\\aaa\\ciy.xls";
        ExcelReader build = EasyExcel.read(path, FmAddress.class, new EasyExcelListenner()).build();
        ReadSheet build1 = EasyExcel.readSheet(0).build();
        log.info("build1={}",build1);
        build.read(build1);
        build.finish();

    }

到了這里,關(guān)于Easys Excel的表格導(dǎo)入(讀)導(dǎo)出(寫)-----java的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【vue導(dǎo)入導(dǎo)出Excel】vue簡單實(shí)現(xiàn)導(dǎo)出和導(dǎo)入復(fù)雜表頭excel表格功能【純前端版本和配合后端版本】

    【vue導(dǎo)入導(dǎo)出Excel】vue簡單實(shí)現(xiàn)導(dǎo)出和導(dǎo)入復(fù)雜表頭excel表格功能【純前端版本和配合后端版本】

    前言 這是一個常用的功能,就是導(dǎo)入和導(dǎo)出excel表格 但是時常會遇到一些復(fù)雜表頭的表格導(dǎo)出和導(dǎo)入 比如我這個案例里面的三層表頭的表格。 網(wǎng)上看了下發(fā)現(xiàn)了一個非常簡單導(dǎo)出和導(dǎo)入方法 當(dāng)然這個是純前端的版本,會出現(xiàn)分頁不好下載的情況。所以實(shí)際工作中,導(dǎo)出還是

    2024年02月11日
    瀏覽(27)
  • 【工具插件類教學(xué)】NPOI插件使用Excel表格的導(dǎo)入和導(dǎo)出(包含圖片)

    目錄 一.導(dǎo)入Excel?解析讀取 1.選擇導(dǎo)入的目標(biāo)文件 2.解析讀取導(dǎo)入的文件

    2024年01月16日
    瀏覽(33)
  • java poi導(dǎo)入Excel、導(dǎo)出excel

    java poi導(dǎo)入Excel、導(dǎo)出excel ReadPatientExcelUtil PoiUtils FileUtils

    2024年02月15日
    瀏覽(27)
  • Java Poi導(dǎo)出Excel表格詳解

    Java Poi導(dǎo)出Excel表格詳解

    一、導(dǎo)出下面的表格 二、流程詳解 ??????? 1、導(dǎo)出excel需要先將數(shù)據(jù)準(zhǔn)備好 ??????? 2、創(chuàng)建工作傅對象SXSSFWorkbook ??????? 3、使用工作傅對象創(chuàng)建sheet對象(工作頁) ??????? 4、使用sheet對象創(chuàng)建行對象row(行對象) ??????? 5、使用row對象創(chuàng)建cell對象(單元格

    2024年02月10日
    瀏覽(28)
  • Java 導(dǎo)出Excel表格生成下拉框-EasyExcel
  • excel文件導(dǎo)入或?qū)С鯦ava代碼示例

    excel文件導(dǎo)入或?qū)С鯦ava代碼示例

    ? ?判斷excel的格式,同時兼容2003和2007 ?獲取行數(shù)據(jù) ?//獲取excel列表內(nèi)的對應(yīng)數(shù)據(jù)格式 3、以下為可能會用到的導(dǎo)出實(shí)例文件,上傳文件服務(wù)器的過程? File格式轉(zhuǎn)換MultipartFile格式的例子 ? -------------------------------------以下無正文-----------------------------------------------------------

    2024年02月16日
    瀏覽(21)
  • Java中使用JXLS工具類導(dǎo)出復(fù)雜Excel表格

    Java中使用JXLS工具類導(dǎo)出復(fù)雜Excel表格

    前言 ? ?在項目開發(fā)中,我們會遇到各種文件導(dǎo)出的開發(fā)場景,但是這種情況并都不常用,于是本人將自己工作中所用的代碼封裝成工具類,旨在記錄工具類使用方法和技術(shù)分享。 一、Jxls的簡介 ? ?Jxls是一個簡單的、輕量級的excel導(dǎo)出庫,使用特定的標(biāo)記在excel模板文件中來

    2024年02月03日
    瀏覽(24)
  • 關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式

    關(guān)于java操作excel導(dǎo)入導(dǎo)出三種方式

    在平時的業(yè)務(wù)系統(tǒng)開發(fā)中,少不了需要用到導(dǎo)出、導(dǎo)入excel功能,今天我們就一起來總結(jié)一下,如果你正為此需求感到困惑,那么閱讀完本文,你一定會有所收獲! 大概在很久很久以前,微軟的電子表格軟件 Excel 以操作簡單、存儲數(shù)據(jù)直觀方便,還支持打印報表,在誕生之初

    2024年02月05日
    瀏覽(19)
  • Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過模板導(dǎo)入數(shù)據(jù)(一)

    Java導(dǎo)出Excel模板,導(dǎo)出數(shù)據(jù)到指定模板,通過模板導(dǎo)入數(shù)據(jù)(一)

    本文章主要是介紹阿里巴巴的easyexcel的使用 1. 首先需要我們導(dǎo)入easyexcel的依賴包 2. 前期工作準(zhǔn)備 編寫相關(guān)導(dǎo)出模板和導(dǎo)入模板。在項目的resources下創(chuàng)建文件夾,命名為excel 導(dǎo)出模板(此處僅做示例,字段根據(jù)自己項目來): ?導(dǎo)入模板(導(dǎo)入時需要哪些字段根據(jù)自己項目業(yè)

    2024年02月03日
    瀏覽(30)
  • Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡單易懂)

    Java原生POI實(shí)現(xiàn)的Excel導(dǎo)入導(dǎo)出(簡單易懂)

    首先是Controller入口方法 這個接口在postman上傳參是下面這樣的: 注意里面的參數(shù)名稱要和接口上的一致,不然會拿不到值 還有file那里key的類型要選file類型的,這樣就可以在后面value里面選擇文件 然后是Service方法 首先是Controller入口 strJson是用來接受其它參數(shù)的,一般導(dǎo)出的

    2024年02月11日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包