private void Form1_Load(object sender, EventArgs e)
{
const int color1Index = 3;
const int color2Index = 5;
const int color3Index = 10;
int sumColor1 = 0, countColor1 = 0, countColor1GTZ = 0;
int sumColor2 = 0, countColor2 = 0, countColor2GTZ = 0;
int sumColor3 = 0, countColor3 = 0, countColor3GTZ = 0;
object missing = Type.Missing;
myApp = new Microsoft.Office.Interop.Excel.Application();
myApp.Visible = true;
Excel.Workbook myWB = myApp.Workbooks.Open(@"C:\test.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet myWS = myApp.ActiveSheet as Excel.Worksheet;
Excel.Range myRange = null;
for (int i = 1; i <= 10; i++)
{
myRange = myApp.Cells[i, 1] as Excel.Range;
if (Convert.ToInt32(myRange.Interior.ColorIndex) == color1Index)
{
int content = Convert.ToInt32(myRange.Text);
countColor1++;
sumColor1 += content;
if (content > 0)
{
countColor1GTZ++;
}
}
else if (Convert.ToInt32(myRange.Interior.ColorIndex) == color2Index)
{
int content = Convert.ToInt32(myRange.Text);
countColor2++;
sumColor2 += content;
if (content > 0)
{
countColor2GTZ++;
}
}
else if (Convert.ToInt32(myRange.Interior.ColorIndex) == color3Index)
{
int content = Convert.ToInt32(myRange.Text);
countColor3++;
sumColor3 += content;
if (content > 0)
{
countColor3GTZ++;
}
}
else
{
MessageBox.Show("There are still colors which are not dealt with");
}
}
MessageBox.Show(sumColor1.ToString()+ " " +countColor1.ToString() + " " + countColor1GTZ.ToString()+ "\n"
+ sumColor2.ToString() + " " + countColor2.ToString() + " " + countColor2GTZ.ToString() + "\n"
+ sumColor3.ToString() + " " + countColor3.ToString() + " " + countColor3GTZ.ToString() + "\n");
}