コード例 #1
0
        private void Previw_Load(object sender, EventArgs e)
        {
            if (path != null)
            {
                Excel excel = new Excel(path, "r");
                if (!excel.isOpen())
                {
                    return;
                }
                excel.setCheet(excel.getSheets());
                int           w     = 0;
                List <Label>  label = new List <Label>();
                List <string> s     = new List <string>();
                q = Controller.checkFile(path);
                if (q == "notes") //fichier = notes
                {
                    panel2.Visible = true;
                    panel3.Visible = false;
                    label          = new List <Label>()
                    {
                        label10, label11, label12, label13, label14, label15, label16, label17, label18, label19
                    };
                    s = new List <string>()
                    {
                        "أكاديمية :", "الإقليمية", "مؤسسة", "المستوى  :", "القسم  :", "الاستاذ", "الدورة  :", "نقط :", "المادة", "السنة الدراسية :"
                    };
                    for (int i = 0; i < excel.find("ID")[0]; i++)
                    {
                        try
                        {
                            for (int j = 0; j < excel.sheet.GetRow(i).LastCellNum; j++)
                            {
                                string f = excel.getContent(i, j);
                                if (f.Contains(s[w]))
                                {
                                    bool t = true;
                                    for (int k = j + 1; k < excel.sheet.GetRow(i).LastCellNum; k++)
                                    {
                                        string b = excel.getContent(i, k);
                                        if (b != "" && !s.Contains(b))
                                        {
                                            label[w].Text = excel.getContent(i, k);
                                            w++;
                                            t = false;
                                            break;
                                        }
                                    }
                                    if (t)
                                    {
                                        label[w].Text = ""; w++;
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                    int x  = excel.find("ID")[0];
                    int y  = excel.find("ID")[1];
                    int we = excel.sheet.GetRow(x).LastCellNum;
                    for (int j = y; j < excel.sheet.GetRow(x).LastCellNum; j++)
                    {
                        if (excel.getContent(x, j).Length == 0 && excel.getContent(x, j + 1).Length == 0 && excel.getContent(x, j + 2).Length == 0)
                        {
                            we = j;
                            break;
                        }
                    }
                    for (int i = x; i < excel.GetLastRow(); i++)
                    {
                        if (i == x)
                        {
                            int f = 0;
                            dataGridView1.ColumnCount = we - y;
                            for (int j = y; j < we; j++)
                            {
                                dataGridView1.Columns[f++].HeaderText = excel.getContent(i, j) + " " + excel.getContent(i + 1, j);
                            }
                        }
                        else
                        {
                            if (excel.getContent(i + 1, y) == "")
                            {
                                break;
                            }
                            dataGridView1.Rows.Add();
                            for (int j = y; j < we; j++)
                            {
                                try
                                {
                                    if ((float.Parse(excel.getContent(i + 1, j)) > 20 || float.Parse(excel.getContent(i + 1, j)) < 0) && j != y)
                                    {
                                        dataGridView1.Rows[i - x - 1].Cells[j - y].Style.BackColor = Color.DarkRed;
                                        Importer_b.Visible = false;
                                    }
                                }
                                catch { }
                                dataGridView1.Rows[i - x - 1].Cells[j - y].Value = excel.getContent(i + 1, j);
                            }
                        }
                    }
                }
                else if (q == "info")
                {
                    panel2.Visible = true;
                    panel3.Visible = true;
                    label          = new List <Label>()
                    {
                        label28, label29, label30, label31, label32, label33
                    };
                    s = new List <string>()
                    {
                        "أكاديمية :", "المذيرية الإقليمية :", "مؤسسة  :", "السنة الدراسية", "المستوى :", "القسم"
                    };
                    for (int i = 0; i < excel.find("رقم التلميذ")[0] - 1; i++)
                    {
                        try
                        {
                            for (int j = 0; j < excel.sheet.GetRow(i).LastCellNum; j++)
                            {
                                string f = excel.getContent(i, j);
                                if (f.Contains(s[w]))
                                {
                                    bool t = true;
                                    for (int k = j + 1; k < excel.sheet.GetRow(i).LastCellNum; k++)
                                    {
                                        string b = excel.getContent(i, k);
                                        if (b != "" && !s.Contains(b))
                                        {
                                            label[w].Text = excel.getContent(i, k);
                                            w++;
                                            t = false;
                                            break;
                                        }
                                    }
                                    if (t)
                                    {
                                        label[w].Text = ""; w++;
                                    }
                                }
                            }
                        }
                        catch { }
                    }

                    int x = excel.find("رقم التلميذ")[0] - 1;
                    int y = excel.find("رقم التلميذ")[1] - 1;

                    for (int i = x; i < excel.GetLastRow(); i++)
                    {
                        if (i == x)
                        {
                            int f = 0;
                            dataGridView1.ColumnCount = excel.sheet.GetRow(i).LastCellNum - 1;
                            for (int j = y; j < excel.sheet.GetRow(i).LastCellNum; j++)
                            {
                                dataGridView1.Columns[f++].HeaderText = excel.getContent(i, j) + " " + excel.getContent(i + 1, j);
                            }
                        }
                        else
                        {
                            dataGridView1.Rows.Add();
                            if (excel.getContent(i + 1, y) == "")
                            {
                                return;
                            }
                            for (int j = y; j < excel.sheet.GetRow(i + 1).LastCellNum; j++)
                            {
                                dataGridView1.Rows[i - x - 1].Cells[j - y].Value = excel.getContent(i + 1, j);
                            }
                        }
                    }
                }
                if (s != null)
                {
                    for (int i = 0; i < s.Count; i++)
                    {
                        if (label[i].Text == "")
                        {
                            materialLabel1.Text += "il faut saisir " + s[i] + "\n";
                        }
                    }
                    if (dataGridView1.Rows.Count == 0)
                    {
                        materialLabel1.Text += "aucun eleves n'est trouve";
                    }
                    if (materialLabel1.Text != "")
                    {
                        groupBox1.Visible = true; materialLabel1.ForeColor = Color.Red;
                    }
                }
                if (panel2.Visible || panel3.Visible || !groupBox1.Visible)
                {
                    Importer_b.Visible = false;
                }
            }
        }