简体   繁体   中英

Find the min and max value in every month in excel using c#

在此处输入图片说明 I have an excel having data for entire year.I want to find low and high in every month using some formula or code in c#.I can achieve this by simply applying MIN and MAX formula to each month values by selecting values in column from date 1 to 31 or 30. But i don't want to the repeat the same for each month. Looking for the solution.

You could do it by reading out the data using OleDB, something like this might be what you are looking for...

    static void Main(string[] args)
    {
        string filepath = @"C:\temp\Data.xlsx"; //Location and name of the .xlsx? file
        string connectioninfo = $@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source={ filepath };
                                   Extended Properties = 'Excel 12.0;HDR=YES;IMEX=1;';";

        string query = @"SELECT * FROM [Data$]"; //Worksheet name, if more than one year add a where clause

        List<ExcelDataModel> entries = new List<ExcelDataModel>();

        using (OleDbConnection conn = new OleDbConnection(connectioninfo))
        {
            OleDbCommand command = new OleDbCommand(query, conn);

            conn.Open();

            OleDbDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
                while (reader.Read())
                    entries.Add(new ExcelDataModel { DT = Convert.ToDateTime(reader[0]),
                                                High = double.Parse(reader[1].ToString()),
                                                Low = double.Parse(reader[2].ToString()) });

            conn.Close();
        }

        var values = entries.GroupBy(x => x.DT.Month).Select(i => new { dt = i.Key, High = i.Max(y => y.High), Low = i.Min(y => y.Low) }).ToList();

        //Do whatever you need with the records
        values.ForEach(month => { Console.WriteLine($"Month: { month.dt } \t Highest: { month.High } \t Lowest: { month.Low }"); });

        Console.ReadLine();
    }

I also added a class to temporarily store all the rows from the Excel worksheet:

public class ExcelDataModel
{
    public DateTime DT { get; set; }
    public double High { get; set; }
    public double Low { get; set; }
}

You can use the MAXIFS function . Its arguments are the range to search for a maximum, and then sets of values with a criterium. Some example data:

A             B     
01/01/2018    1
21/01/2018    2
10/02/2018    3
02/03/2018    4
22/03/2018    5
11/04/2018    6

Then you could use these formula's to calculate a per-month max:

C             D
01/01/2018    =MAXIFS(B:B;A:A;">= " & D2;A:A;"<" & EDATE(D2;1))
=EDATE(C2;1)  =MAXIFS(B:B;A:A;">= " & D3;A:A;"<" & EDATE(D3;1))
=EDATE(C3;1)  =MAXIFS(B:B;A:A;">= " & D4;A:A;"<" & EDATE(D4;1))

B:B is the range searched for the maximum. A:A;"> " & D4 is the first criteria that says the date must be after the start of the month. A:A;"<" & EDATE(D4;1) is the second criteria that says the date must be before the end of the month. This calculates as:

C             D
01/01/2018    2
01/02/2018    3
01/03/2018    5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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