使用C#将几个Excel文件合并去重分类

需要将几个Excel表格里面的数据去重,然后将每个站点的数据另存为一张Sheet上。

几个表格如下所示:

 

 

实现效果如下所示:

 

具体实现

需要使用EPPlus操作Excel

安装EPPlus如下所示:

 

为了更好的演示与说明,把步骤进行了拆分,先导入Excel数据,再去重,再进行数据分类,最后再导出为Excel数据,设计了一个窗体,如下所示:

 

导入Excel数据

首先定义一个类,用来保存相关数据,类的设计如下:

 public class WaterData
 {
      public int Id { get; set; }
      public string? Name { get; set; }
      public string? WaterLevel { get; set; }
      public string? WaterChange { get; set; }
      public string? Source { get; set; }
     
 }

点击导入Excel数据按钮的代码如下:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files (*.xlsx; *.xls;*.csv)|*.xlsx; *.xls;*.csv";
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
​
   if (openFileDialog.ShowDialog() == DialogResult.OK)
      {
          filePath = openFileDialog.FileName;       
          ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
          using (ExcelPackage package = new ExcelPackage(filePath))
           {
​
               ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
​
                //获取表格的列数和行数
                int rowCount = worksheet.Dimension.Rows;
                int colCount = worksheet.Dimension.Columns;
​
                for (int i = 0; i < rowCount - 1; i++)
                {
                   //创建一个realData类保存数据
                   var data = new WaterData();
             data.Id = n;
             data.Name = (string)worksheet.Cells[i + 2, 3].Value;
             data.WaterLevel = Convert.ToString(worksheet.Cells[i + 2, 4].Value);
             data.WaterChange = Convert.ToString(worksheet.Cells[i + 2, 5].Value);
             data.Source = (string)worksheet.Cells[i + 2, 2].Value;
             waterList.Add(data);
                     n++;
                    }         
             package.Save();
​
                }
            }
            else
            {
​
                MessageBox.Show("您本次没有选择任何文件!!!");
            }
        }

上面的n是static int,初始值为0。

导入Excel数据的效果如下所示:

数据去重

执行数据去重,依据的是C#LINQ中的DistinctBy方法,本例中不使用id是因为每条数据id都不一样,即使是重复的数据但是id也不一样,本例依据的中Name属性和Soure属性,只要这两个数据一样,就认为是重复数据。

点击数据去重按钮的代码如下:

private void button2_Click(object sender, EventArgs e)
{    
    distinctList = waterList.DistinctBy(x => new { x.Name, x.Source }).ToList(); 
}

只需要一行代码:

distinctList = waterList.DistinctBy(x => new { x.Name, x.Source }).ToList();

执行去重的效果如下所示:

执行数据分类

本例中要求将同一个站点的数据放在同一张Sheet上,那么首先需要知道到底有多少个不同的站名,代码如下:

 var Names = distinctList.Select(x => x.Name).Distinct().ToList(); 

实现效果如下:

由于这些数据是由图片文字识别而来的,因此可能识别有误,如果一个站名的数据不足50条,就不需要,代码如下:

 for (int i = 0; i < Names.Count; i++)
  {
     var nameList = distinctList.Where(x => x.Name == Names[i]).ToList();
     if (nameList.Count > 50) 
         {
               list.Add(nameList);
          }            
   }  

实现数据分类也只需要一行代码:

var nameList = distinctList.Where(x => x.Name == Names[i]).ToList();

实现效果如下所示:

导出为Excel文件

本例中导出为Excel文件的思路是先让用户选定一个文件夹,然后就将导出的Excel文件保存在这个文件夹下面,导出为Excel文件的代码如下:

 private void button4_Click(object sender, EventArgs e)
        {
            // 创建一个FolderBrowserDialog对象
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
​
            // 设置对话框的标题
            folderBrowserDialog.Description = "选择保存各站点数据的文件夹";
​
            // 设置默认的根文件夹,如果需要的话
            // folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
​
            // 显示文件夹选择对话框
            DialogResult result = folderBrowserDialog.ShowDialog();
​
            if (result == DialogResult.OK)
            {
                // 用户选择了一个文件夹
                selectedFolderPath = folderBrowserDialog.SelectedPath;
                richTextBox1.Text += $"选择的Excel保存文件夹为:{selectedFolderPath}\r\n";
                richTextBox1.Text += "正在执行导出为Excel文件...";
                using (ExcelPackage excelPackage = new ExcelPackage())
                {                 
                    for(int i =0; i < list.Count; i++) 
                    {
                        ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add(list[i][0].Name);
                        for (int j = 0; j < list[i].Count; j++)
                        {
                            worksheet.Cells[j + 1, 1].Value = list[i][j].Id;
                            worksheet.Cells[j + 1, 2].Value = list[i][j].Name;
                            worksheet.Cells[j + 1, 3].Value = list[i][j].WaterLevel;
                            worksheet.Cells[j + 1, 4].Value = list[i][j].WaterChange;
                            worksheet.Cells[j + 1, 5].Value = list[i][j].Source;
                        }
                    }
​
                    // 保存 Excel 文件
                    FileInfo excelFile = new FileInfo($"{selectedFolderPath}\\各站点数据.xlsx");
                    excelPackage.SaveAs(excelFile);
                    richTextBox1.Text += "导出为Excel文件完成\r\n";
                }
            }
        }

实现效果如下所示:

 

最后

操作Excel大家一般使用VBA、Python比较多,本文通过一个实例演示了如何通过C#来简化我们的办公(处理Excel数据)。

本实例全部源代码如下:

using OfficeOpenXml;
using System.Collections;
using System.Collections.Generic;
​
namespace Excel数据处理
{
    public partial class Form1 : Form
    {
        string filePath;
        string selectedFolderPath;
        static int n = 0;
        List<WaterData> waterList = new List<WaterData>();
        List<WaterData> distinctList = new List<WaterData>();
        List<List<WaterData>> list = new List<List<WaterData>>();
        public class WaterData
        {
            public int Id { get; set; }
            public string? Name { get; set; }
            public string? WaterLevel { get; set; }
            public string? WaterChange { get; set; }
            public string? Source { get; set; }
​
        }
​
​
        public Form1()
        {
            InitializeComponent();
        }
​
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files (*.xlsx; *.xls;*.csv)|*.xlsx; *.xls;*.csv";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
​
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
                richTextBox1.Text += $"您选中的文件路径为:{filePath}\r\n";
                richTextBox1.Text += $"正在导入Excel数据...\r\n";
                ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
                using (ExcelPackage package = new ExcelPackage(filePath))
                {
​
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
​
                    //获取表格的列数和行数
                    int rowCount = worksheet.Dimension.Rows;
                    int colCount = worksheet.Dimension.Columns;
​
                    for (int i = 0; i < rowCount - 1; i++)
                    {
                        //创建一个realData类保存数据
                        var data = new WaterData();
                        data.Id = n;
                        data.Name = (string)worksheet.Cells[i + 2, 3].Value;
                        data.WaterLevel = Convert.ToString(worksheet.Cells[i + 2, 4].Value);
                        data.WaterChange = Convert.ToString(worksheet.Cells[i + 2, 5].Value);
                        data.Source = (string)worksheet.Cells[i + 2, 2].Value;
                        waterList.Add(data);
                        n++;
                    }
                    richTextBox1.Text += $"导入Excel数据成功,数据量为:{rowCount - 1}\r\n";
                    package.Save();
​
                }
            }
            else
            {
​
                MessageBox.Show("您本次没有选择任何文件!!!");
            }
        }
​
        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "正在执行数据去重...\r\n";
            distinctList = waterList.DistinctBy(x => new { x.Name, x.Source }).ToList();
            richTextBox1.Text += $"数据去重已完成,去重后数据量为:{distinctList.Count}\r\n";
        }
​
        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "正在执行数据分类...\r\n";
            var Names = distinctList.Select(x => x.Name).Distinct().ToList();          
            for (int i = 0; i < Names.Count; i++)
            {
                var nameList = distinctList.Where(x => x.Name == Names[i]).ToList();
                if (nameList.Count > 50) 
                {
                    list.Add(nameList);
                }            
            }
            richTextBox1.Text += $"执行数据分类完成,类数为:{list.Count}\r\n";
        }
​
        private void button4_Click(object sender, EventArgs e)
        {
            // 创建一个FolderBrowserDialog对象
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
​
            // 设置对话框的标题
            folderBrowserDialog.Description = "选择保存各站点数据的文件夹";
​
            // 设置默认的根文件夹,如果需要的话
            // folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
​
            // 显示文件夹选择对话框
            DialogResult result = folderBrowserDialog.ShowDialog();
​
            if (result == DialogResult.OK)
            {
                // 用户选择了一个文件夹
                selectedFolderPath = folderBrowserDialog.SelectedPath;
                richTextBox1.Text += $"选择的Excel保存文件夹为:{selectedFolderPath}\r\n";
                richTextBox1.Text += "正在执行导出为Excel文件...";
                using (ExcelPackage excelPackage = new ExcelPackage())
                {                 
                    for(int i =0; i < list.Count; i++) 
                    {
                        ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add(list[i][0].Name);
                        for (int j = 0; j < list[i].Count; j++)
                        {
                            worksheet.Cells[j + 1, 1].Value = list[i][j].Id;
                            worksheet.Cells[j + 1, 2].Value = list[i][j].Name;
                            worksheet.Cells[j + 1, 3].Value = list[i][j].WaterLevel;
                            worksheet.Cells[j + 1, 4].Value = list[i][j].WaterChange;
                            worksheet.Cells[j + 1, 5].Value = list[i][j].Source;
                        }
                    }
​
                    // 保存 Excel 文件
                    FileInfo excelFile = new FileInfo($"{selectedFolderPath}\\各站点数据.xlsx");
                    excelPackage.SaveAs(excelFile);
                    richTextBox1.Text += "导出为Excel文件完成\r\n";
                }
            }
        }
    }
}

如果你也对C#感兴趣,欢迎关注微信公众号DotNet学习交流~
posted @ 2023-11-15 10:32  mingupupup  阅读(1217)  评论(1编辑  收藏  举报