easyexcel源码分析

MAVEN依赖包

        <!--easyexcel依赖-->

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.29</version>
        </dependency>

demo代码

package com.example.demoeasyexcel.read;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;

import lombok.extern.slf4j.Slf4j;

/**
 * 读的常见写法
 *
 * @author Jiaju Zhuang
 */

@Slf4j
public class ReadTest {

    public static void main(String[] args) {
        String fileName = "D:\\新建文本文档.csv";
        // 这里 需要指定读用哪个class去读,然后读取第一个sheet
        EasyExcel
                .read(fileName, ExceptionDemoData.class, new DemoExceptionListener())
                .sheet()
                .doRead();
    }

}

com.alibaba.excel.read.builder.ExcelReaderSheetBuilder#doRead

com.alibaba.excel.ExcelReader#read(java.util.List<com.alibaba.excel.read.metadata.ReadSheet>)

com.alibaba.excel.analysis.ExcelAnalyser#analysis

com.alibaba.excel.analysis.ExcelAnalyserImpl#analysis

com.alibaba.excel.analysis.ExcelReadExecutor#execute

com.alibaba.excel.analysis.csv.CsvExcelReadExecutor#execute

往后用了org.apache.commons.csv中的CSVParser来读取excel

============

所以直接再写个org.apache.commons.csv的demo

package com.example.demoeasyexcel.read;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CsvReaderDemo {

    public static void main(String[] args) {
        String fileName = "D:\\新建文本文档.csv";

        try {
            Reader reader = new FileReader(fileName);

            // Create a CSVParser with the appropriate format
            CSVFormat aDefault = CSVFormat.DEFAULT;
            CSVParser csvParser = new CSVParser(reader,aDefault);

            // Loop through each record (row) in the CSV file
            for (CSVRecord record : csvParser) {
                // Access data from each record by column index or column name
                String column1Value = record.get(0); // Get data from the first column
                String column2Value = record.get(1); // Get data from the second column
                // and so on...

                // Process or print the data as needed
                System.out.println("Column 1: " + column1Value + ", Column 2: " + column2Value);
            }

            // Close the CSVParser and underlying Reader
            csvParser.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在CSVFormat aDefault = CSVFormat.DEFAULT;这行代码中,指定了后续读取CSV文件的一些规则,比如分隔符(delimiter)是逗号,每行数据的分隔符(recordSeparator)是CRLF(\r\n)。

开始迭代读取

org.apache.commons.csv.CSVParser#nextRecord

org.apache.commons.csv.Lexer#nextToken

每次读一个char

org.apache.commons.csv.Lexer#parseSimpleToken

拼接完成

token.content.append((char) ch);
ch = reader.read(); // continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值