xlWorkBook = xlApp.Workbooks.Add ' first worksheet If xlApp.Application.Sheets.Count() < 1 Then cmd.CommandText = "Select * From students" cmd.Connection = connection da.SelectCommand = cmd da.Fill(dt) dgrid.DataSource = dt location = SaveFileDialog1.FileName xlWorkSheet = CType(xlWorkBook.Worksheets.Add(), Excel.Worksheet) xlWorkSheet = xlWorkBook.Sheets("Students")
I believe this statement is your problem: xlWorkSheet = xlWorkBook.Sheets("Students")
Unless your Normal template has a Students worksheet in it, xlWorkSheet will be unassigned (Nothing).