Stumbled upon this problem to find the median ...Here is the solution
ArrayList ar = new ArrayList();
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=D:\temp\median.xlsx;Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
ar.Add(int.Parse(dr[0].ToString()));
}
}
}
ar.Sort();
double ar_median = 0;
double ar_low = 0;
double ar_high = 0;
int lo = 0;
int hi = 0;
int arlo = 0;
int arhi = 0;
if ((ar.Count % 2) == 0)
{
ar_low = Math.Floor((double)(ar.Count + 1) / 2);
ar_high = Math.Ceiling((double)(ar.Count + 1) / 2);
}
else
{
ar_low = Math.Floor((double)(ar.Count) / 2);
ar_high = Math.Ceiling((double)(ar.Count) / 2);
}
lo = int.Parse(ar_low.ToString())-1;
hi = int.Parse(ar_high.ToString())-1;
arlo = int.Parse(ar[lo].ToString());
arhi = int.Parse(ar[hi].ToString());
double arav = Convert.ToDouble(arlo + arhi);
ar_median = Math.Round(arav / 2);
MessageBox.Show(ar_median.ToString());
ArrayList ar = new ArrayList();
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=D:\temp\median.xlsx;Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
ar.Add(int.Parse(dr[0].ToString()));
}
}
}
ar.Sort();
double ar_median = 0;
double ar_low = 0;
double ar_high = 0;
int lo = 0;
int hi = 0;
int arlo = 0;
int arhi = 0;
if ((ar.Count % 2) == 0)
{
ar_low = Math.Floor((double)(ar.Count + 1) / 2);
ar_high = Math.Ceiling((double)(ar.Count + 1) / 2);
}
else
{
ar_low = Math.Floor((double)(ar.Count) / 2);
ar_high = Math.Ceiling((double)(ar.Count) / 2);
}
lo = int.Parse(ar_low.ToString())-1;
hi = int.Parse(ar_high.ToString())-1;
arlo = int.Parse(ar[lo].ToString());
arhi = int.Parse(ar[hi].ToString());
double arav = Convert.ToDouble(arlo + arhi);
ar_median = Math.Round(arav / 2);
MessageBox.Show(ar_median.ToString());
Comments