简体   繁体   English

如何使用C#和EPPlus库比较2个.xlsx文件?

[英]How to compare 2 .xlsx files using C# and EPPlus library?

I am struggling with comparing 2 .xlsx files using the mentioned library. 我正在努力使用提到的库比较2个.xlsx文件。 My goal is to compare 2 columns in both files respectively and if the cells match each other, perform some work. 我的目标是分别比较两个文件中的2列,如果单元格彼此匹配,请执行一些工作。 Here is my code, which doesn't seem to work. 这是我的代码,似乎无效。 What's wrong with it? 它出什么问题了?

FileInfo plik1 = new FileInfo(file1);
        FileInfo plik2 = new FileInfo(file2);

        using (ExcelPackage xlPackage2 = new ExcelPackage(plik2))
        {
            var worksheet2 = xlPackage2.Workbook.Worksheets[1];

            //var start = worksheet.Dimension.Start;
            //var end = worksheet.Dimension.End;
            //var col = worksheet.Column(1);

            using (ExcelPackage xlPackage = new ExcelPackage(plik1))
            {
                var worksheet = xlPackage.Workbook.Worksheets[1];

                int j = 0;
                    for (j=1; j<2500000; j++)
                    {
                        if (worksheet.Cells[j, 1].Value == worksheet2.Cells[j, 2].Value)
                        {

                            worksheet.Cells[j, 13].Value = worksheet2.Cells[j, 14].Value;
                            worksheet.Cells[j, 14].Value = worksheet2.Cells[j, 16].Value;

                        }

                    }

                string path = "C:\\Users\\bddddz\\Desktop\\asd\\final.xlsx";

                xlPackage.SaveAs(new FileInfo(path));


                xlPackage.Stream.Close();
                xlPackage2.Stream.Close();

            }
        }

Use a separate ExcelPackage for the new file you want to save. 对要保存的新文件使用单独的ExcelPackage。

    FileInfo plik1 = new FileInfo(file1);
    FileInfo plik2 = new FileInfo(file2);
    FileInfo path = new FileInfo("C:\\Users\\bddddz\\Desktop\\asd\\final.xlsx");

    using (ExcelPackage xlPackageNew = new ExcelPackage(path))
    using (ExcelPackage xlPackage = new ExcelPackage(plik1))
    using (ExcelPackage xlPackage2 = new ExcelPackage(plik2))
    {
        var worksheet2 = xlPackage2.Workbook.Worksheets[1];
        var worksheet = xlPackage.Workbook.Worksheets[1];
        var worksheet3 = xlPackageNew.Workbook.Worksheets.Add("new"); 

        int j = 0;
        for (j=1; j<2500000; j++)
        {
            if (worksheet.Cells[j, 1].Value == worksheet2.Cells[j, 2].Value)
            {
                worksheet3.Cells[j, 13].Value = worksheet2.Cells[j, 14].Value;
                worksheet3.Cells[j, 14].Value = worksheet2.Cells[j, 16].Value;
            }
        }

        xlPackageNew.Save();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM