【问题标题】:How to compare single columned two Excel files which are having 500,000k rows如何比较具有 500,000k 行的单列两个 Excel 文件
【发布时间】:2020-03-10 04:44:45
【问题描述】:

目标:比较两个 excel 文件,每个 excel 只有一列但有 50 万行。 我只想看看两个excel之间不常见的值。

列:只有 1 个 行数:500,000

语言:JAVA

到目前为止我尝试了什么:

  1. 使用了 Apache POI(请参见下面的程序 1)
  2. ArrayList(请看下面的program-2)
  3. 我尝试了一些网站,我们可以在其中上传倍数 Excel 以查看差异 (https://www.textcompare.org/excel/)
  4. 我将堆大小增加到 256 到 2048m,但对我不起作用。

(我的程序适用于少量数据或少量记录)

上述努力对我不起作用。

program-1::

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public static ExcelReader excel3 = null;
public static ExcelReader excel2 = null;

excel3 = new ExcelReader("C:\\Users\\DataExcelCompare\\Book3.xlsx");
excel2 = new ExcelReader("C:\\Users\\DataExcelCompare\\Book2.xlsx");

    File f3 = new File("C:\\Users\\DataExcelCompare\\Book3.xlsx");      
        FileInputStream fi3 = new FileInputStream(f3);
        Workbook workbook3 = WorkbookFactory.create(fi3);
        Sheet sheet3 = workbook3.getSheet("book3");     

        File f2 = new File("C:\\Users\\DataExcelCompare\\Book2.xlsx");      
        FileInputStream fi2 = new FileInputStream(f2);
        Workbook workbook2 = WorkbookFactory.create(fi2);
        Sheet sheet2 = workbook2.getSheet("book2"); 

         int firstRow2 = sheet2.getFirstRowNum();
         int lastRow2 = sheet2.getLastRowNum();

         int firstRow3 = sheet3.getFirstRowNum();
         int lastRow3 = sheet3.getLastRowNum();

            for(int i=firstRow2; i <= lastRow2; i++) { 
             for(int j=firstRow3; j <= lastRow3; j++) {

                String ele2 = sheet2.getRow(i).getCell(0).toString().trim();
                String ele3 = sheet3.getRow(j).getCell(0).toString().trim() ;

                 if(ele2.toString() !=  ele3.toString())
                 {
                    // some operation 
                 }
                 else
                 {
                    // some operation   
                 }

             }

program-2::

ArrayList<String> listOne = new ArrayList<String>();
ArrayList<String> listTwo =new ArrayList<String>();

         for(int i=firstRow2; i <= lastRow2; i++) {
             listOne.add(sheet2.getRow(i).getCell(0).toString().trim());
             System.out.println("added: " + sheet2.getRow(i).getCell(0).toString().trim());
         }

         for(int j=firstRow3; j <= lastRow3; j++) {
             listTwo.add(sheet3.getRow(j).getCell(0).toString().trim());
             System.out.println("added: " + sheet3.getRow(j).getCell(0).toString().trim());
         }

            listTwo.removeAll(listOne);

            System.out.println("list two : " + listTwo);

【问题讨论】:

标签: java excel


【解决方案1】:

使用 Apache POI 读取 Excel 文件非常消耗内存。

使用少于 50,000 行的 Excel 文件时出现内存错误并不少见(更不用说 500,000 x 2)。

它永远不会起作用。

在这些 Excel 文件到达您的 Java 程序之前,找到一个将这些 Excel 文件转换为 .txt 的第三方程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    相关资源
    最近更新 更多