Here is the code for creating a new Microsoft excel file open it and open a new sheet of it and to insert data from the data table to it and save the a copy of a application in your hard drive.

Excel.Application application;

Excel.Workbook book;

Excel.Worksheet sheet;

application = new Excel.ApplicationClass();

           

DataTable dt = new DataTable();

dt = dtCouseSchedule.Copy();

book = application.Workbooks.Add(Type.Missing);

sheet = (Excel.Worksheet)book.Worksheets[1];

sheet.Name = “SampleExcel”;

for (int i = 1; i <= dt.Rows.Count; i++)

   {

      for (int j = 1; j <= dt.Columns.Count; j++)

      {

   ((Excel.Range)sheet.Cells[i, j]).Value2 = dt.Rows[i – 1].ItemArray[j – 1].ToString();

 

       }

    }

application.Visible = true;

book.SaveCopyAs(@”” + ConfigurationSettings.AppSettings[“Reports”] + “Course Schedules for “ + lblProviderName.Text + “.xls”);

Advertisements