Exemple #1
0
        public static void ImportTeacher(string Path, DataGridView gridView)
        {
            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook    xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet   xlWorkSheet;
            Microsoft.Office.Interop.Excel.Range       xlRange;

            if (Path != string.Empty)
            {
                xlApp       = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook  = xlApp.Workbooks.Open(Path);
                xlWorkSheet = xlWorkBook.Worksheets["Sheet1"];
                xlRange     = xlWorkSheet.UsedRange;

                gridView.DataSource  = null;
                gridView.ColumnCount = xlRange.Columns.Count;
                int n = 0;
                for (int i = 1; i < gridView.ColumnCount; i++)
                {
                    gridView.Columns[n].Name = xlRange.Cells[1, i].Text;
                    n++;
                }

                for (int i = 2; i <= xlRange.Rows.Count; i++)
                {
                    gridView.Rows.Add(xlRange.Cells[i, 1].Text.ToString(), xlRange.Cells[i, 2].Text.ToString(), xlRange.Cells[i, 3].Text.ToString(), xlRange.Cells[i, 4].Text.ToString(), xlRange.Cells[i, 5].Text.ToString(), xlRange.Cells[i, 6].Text.ToString(), xlRange.Cells[i, 7].Text.ToString(), xlRange.Cells[i, 8].Text.ToString(), xlRange.Cells[i, 9].Text.ToString());
                    DAO_Admin.InsertTeacherWithExcel(xlRange, i);
                }

                xlWorkBook.Close();
                xlApp.Quit();
            }
        }