easyexcel源码分析

MAVEN依赖包

        

        
            com.alibaba
            easyexcel
            3.3.2
        
        
            com.alibaba.fastjson2
            fastjson2
            2.0.29
        

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.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

你可能感兴趣的:(源码阅读,java,windows,开发语言)