Exemple #1
0
        public static void ImportStudent(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, xlRange.Cells[i, 2].Text, xlRange.Cells[i, 3].Text, xlRange.Cells[i, 4].Text, xlRange.Cells[i, 5].Text, xlRange.Cells[i, 6].Text, xlRange.Cells[i, 7].Text, xlRange.Cells[i, 8].Text, xlRange.Cells[i, 9]);
                    DAO_Admin.InsertStudentWithExcel(xlRange, i);
                }

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