public void ExportWorkRota(int weekNo, Year year) { Excel.Application excelApplication = new Excel.Application(); if (excelApplication != null) { Excel.Workbook excelFileBook; Excel.Worksheet excelFileSheet; object misValue = System.Reflection.Missing.Value; excelFileBook = excelApplication.Workbooks.Add(misValue); excelFileSheet = (Excel.Worksheet)excelFileBook.Worksheets.get_Item(1); List <DateTime> weekStartFinish = year.GetWeekStartFinish(weekNo); DateTime startDate = weekStartFinish[0]; DateTime finishDate = weekStartFinish[1]; int loopIndex = 2; DateTime tempStartDate = startDate; while (tempStartDate <= finishDate) { //input date at top of screen excelFileSheet.Cells[1, loopIndex] = "'" + tempStartDate.ToShortDateString(); tempStartDate = tempStartDate.AddDays(1); loopIndex++; } int xLoop = 2; int yLoop = 2; List <EmployeeWorkWeek> employeeRota; foreach (Employee emp in mEmployees) { excelFileSheet.Cells[yLoop, 1] = emp.Name; employeeRota = emp.GetHours(weekNo, year); tempStartDate = startDate; foreach (EmployeeWorkWeek workWeek in employeeRota) { if (workWeek.StoreId == mStoreId) { string displayData = workWeek.RotaStartTime + " - " + workWeek.RotaFinishTime; excelFileSheet.Cells[yLoop, xLoop] = displayData; xLoop++; } else if (workWeek.StoreId == "Holiday") { string displayData = "Holiday"; excelFileSheet.Cells[yLoop, xLoop] = displayData; xLoop++; } else { string displayData = "00:00:00 - 00:00:00"; excelFileSheet.Cells[yLoop, xLoop] = displayData; xLoop++; } } xLoop = 2; yLoop++; } excelFileSheet.Columns.AutoFit(); try { excelFileBook.SaveAs(mExcelDirectory + "\\WorkWeek.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); mSavedExcelSuccess = true; } catch (Exception e) { //open window to save } if (!excelFileBook.Saved) { mSavedExcelSuccess = false; } excelFileBook.Close(0); excelApplication.Quit(); Marshal.ReleaseComObject(excelFileSheet); Marshal.ReleaseComObject(excelFileBook); Marshal.ReleaseComObject(excelApplication); } else { mSavedExcelSuccess = false; //EXCEL not installed on system } notifyExcelObservers(); }