spring 读取文件Excel

hfsqlsce  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(66)

我用JAVA读了一个70,000行10列的大文件excel。我用Apache POI读取和获取数据,但需要1小时左右的时间。我改用fast-excel读取文件,但只能读取2,000行。
有人能建议我如何调整Apache POI吗?或者有其他解决方案?

u59ebvdq

u59ebvdq1#

正如@shmosel提到的,我也觉得你的问题出在文件阅读器上。检查下面的代码是否比你现在的代码运行得更快。
试试下面的代码:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {
    public static void main(String[] args) {
        try (FileInputStream file = new FileInputStream("path/to/your/excel/file.xlsx")) {
            
            Workbook workbook = new XSSFWorkbook(file);

            
            Sheet sheet = workbook.getSheetAt(0);

            
            for (Row row : sheet) {
                // Iterate over cells
                for (Cell cell : row) {
                    
                    System.out.print(cell.toString() + "\t");
                }
                
                System.out.println();
            }

            
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字符串

kcugc4gi

kcugc4gi2#

org.ttzero:eec:0.5.12支持阅读数百万行Excel github links,遗憾的是,只有中文文档可用

try (ExcelReader reader = ExcelReader.read(Paths.get("./abc.xlsx"))) {
    // Print
    reader.sheet(0).rows().forEach(System.out::println);

    // Convert to Java Bean
    List<Item> list =  reader.sheet(0)
        .header(1)  // <- Specify the title line number
        .rows()
        .map(row -> row.to(Item.class)) // <- Convert to Java Bean
        .collect(Collectors.toList());
}

字符串

相关问题