private void btnExportToExcel_Click(object sender, EventArgs e) { SaveFileDialog save2Excel = new SaveFileDialog(); save2Excel.Filter = @"Excel files|*.xls"; save2Excel.DefaultExt = "ProfessorsSchedule.xls"; save2Excel.FileName = "ProfessorsSchedule.xls"; if (save2Excel.ShowDialog() == DialogResult.OK) { CheckListCellsTextDataForm CLCTDF = new CheckListCellsTextDataForm(); // // check default items CLCTDF.chklstExportCell.Items.Clear(); CLCTDF.chklstExportCell.Items.Add("Course Code", false); CLCTDF.chklstExportCell.Items.Add("Course Name", true); CLCTDF.chklstExportCell.Items.Add("Room Name", false); // CheckedListBox chklstCTD_buffer = CLCTDF.chklstExportCell; CLCTDF.Location = new Point(btnExportToExcel.Location.X + this.Location.X + 7, this.Location.Y + btnExportToExcel.Location.Y + btnExportToExcel.Size.Height + 32); CLCTDF.ShowDialog(); this.Cursor = Cursors.WaitCursor; try { // // delete exist file's // if (System.IO.File.Exists(save2Excel.FileName)) { System.IO.File.Delete(save2Excel.FileName); } // // If using Professional version, put your serial key below. Otherwise, keep following // line commented out as Free version doesn't have SetLicense method. // SpreadsheetInfo.SetLicense("YOUR-SERIAL-KEY-HERE"); ExcelFile ef = new ExcelFile(); ef.LimitReached += new LimitEventHandler(ef_LimitReached); ef.LimitNear += new LimitEventHandler(ef_LimitNear); ExcelWorksheet ws = ef.Worksheets.Add("ProfessorsSchedule"); int numberSheet = 1; int numberFile = 1; int startRow_No = 0; #region Professor Name Style's CellStyle ProfessorTitleStyle = new CellStyle(); ProfessorTitleStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; ProfessorTitleStyle.VerticalAlignment = VerticalAlignmentStyle.Center; ProfessorTitleStyle.Font.Weight = ExcelFont.BoldWeight; ProfessorTitleStyle.FillPattern.SetSolid(Color.Azure); ProfessorTitleStyle.Font.Color = Color.RoyalBlue; ProfessorTitleStyle.WrapText = false; ProfessorTitleStyle.Borders.SetBorders(MultipleBorders.Right | MultipleBorders.Top, Color.Black, LineStyle.Thin); #endregion #region Header Style's (Example First Professor Row[2~3] , Column[0~7]) CellStyle headerStyle = new CellStyle(); headerStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; headerStyle.VerticalAlignment = VerticalAlignmentStyle.Center; headerStyle.FillPattern.SetSolid(Color.Chocolate); headerStyle.Font.Weight = ExcelFont.BoldWeight; headerStyle.Font.Color = Color.White; headerStyle.WrapText = true; headerStyle.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin); #endregion #region Header Text Data object[,] headerText = new object[1, 8] { { "Time Slot", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" } }; #endregion #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion #region Any Cells Style's CellStyle AnyCellStyle = new CellStyle(); AnyCellStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; AnyCellStyle.VerticalAlignment = VerticalAlignmentStyle.Center; AnyCellStyle.Font.Size = 14 * 16; AnyCellStyle.Font.Weight = ExcelFont.BoldWeight; AnyCellStyle.FillPattern.SetSolid(Color.White); AnyCellStyle.Font.Color = Color.Black; AnyCellStyle.WrapText = true; AnyCellStyle.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin); #endregion #region Read any Professor in DataGridView and Save in Excel files foreach (var p in _professors) { checkLoop: if (startRow_No + 16 <= 149) // Write in Exist Worksheet { #region Save in Excel Row by database.Professors // // set Professor Name Style ws.Cells.GetSubrangeAbsolute(0 + startRow_No, 0, 1 + startRow_No, 7).Style = ProfessorTitleStyle; // // set Professor Name text in Title ws.Cells[0 + startRow_No, 0].Value = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Professor: {0} {1} , ID: {2}", p.Value.Name_Professor, string.IsNullOrEmpty(p.Value.Email) ? "" : "Email: " + p.Value.Email, p.Value.ID); // // set Header Styles ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 0, 3 + startRow_No, 7).Style = headerStyle; // // set Header Text data for (int j = 0; j < 8; j++) { ws.Cells[3 + startRow_No, j].Value = headerText[0, j]; } // // set Merged Band #region Merged Band // // First Row for Group Name ws.Cells.GetSubrangeAbsolute(0 + startRow_No, 0, 1 + startRow_No, 7).Merged = true; // // Second Row for Group Header "Time Slot" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 0, 3 + startRow_No, 0).Merged = true; // // Second Row for Group Header "Saturday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 1, 3 + startRow_No, 1).Merged = true; // // Second Row for Group Header "Sunday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 2, 3 + startRow_No, 2).Merged = true; // // Second Row for Group Header "Monday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 3, 3 + startRow_No, 3).Merged = true; // // Second Row for Group Header "Tuesday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 4, 3 + startRow_No, 4).Merged = true; // // Second Row for Group Header "Wednesday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 5, 3 + startRow_No, 5).Merged = true; // // Second Row for Group Header "Thursday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 6, 3 + startRow_No, 6).Merged = true; // // Second Row for Group Header "Friday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 7, 3 + startRow_No, 7).Merged = true; #endregion // // set Time Slot Styles for (int row = 4; row < 12 + 4; row++) { ws.Cells[row + startRow_No, 0].Style = headerStyle; // headerStyle is equal by timeSlot Style ws.Cells[row + startRow_No, 0].Value = string.Format("{0} ~ {1}", (row - 4) + 8, (row - 4) + 9); } // // set all cells style's for (int row = 0; row < 12; row++) { for (int column = 1; column < 8; column++) { ws.Cells[row + startRow_No + 4, column].Style = AnyCellStyle; } } // // read any Classroom_Time for this Professor and save that #region Read and Save Cells by this Professor classrooms data //------------------------------------------------------------------------------------------------- // // Find any classroom for this Professor List <MakeClassSchedule.Classroom_Time> lstClassrooms = p.Value.Classroom_Times.ToList(); // // Set Free Time Slot Colors for this Professor ProfessorInfoCompiler PIC = new ProfessorInfoCompiler(); PIC.StartScanner(p.Value.Schedule); for (int t = 0; t < 12; t++) { for (int d = 1; d < 8; d++) { if (PIC.CompiledData[t, d]) { ws.Cells[t + startRow_No + 4, d].Style.FillPattern.SetSolid(freeTimeBackColor); } } } // // Read any classroom for this Professor foreach (var cr in lstClassrooms) { // // 0. Course Code // 1. Course Name // 2. Room Name string classesValue = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}{1}{2}", (chklstCTD_buffer.GetItemChecked(1) ? cr.Class.Course.Name_Course + Environment.NewLine : string.Empty), // Course Name (chklstCTD_buffer.GetItemChecked(0) ? (cr.Class.Course.CourseCode.HasValue ? cr.Class.Course.CourseCode.Value : 0) + "\r\n" : ""), // Course Code (chklstCTD_buffer.GetItemChecked(2) ? cr.Room.Name_Room : "")); // Room Name int row = cr.StartTime; int column = cr.Day_No + 1; ws.Cells[row + startRow_No + 4, column].Value = classesValue; ws.Cells[row + startRow_No + 4, column].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.DashDotDot); try { if (cr.Duration > 1) { ws.Cells.GetSubrangeAbsolute(row + startRow_No + 4, column, (row + startRow_No + 4) + (cr.Duration - 1), column).Merged = true; } } catch { } // maybe two class overlap in a cell for (int d = 0; d < cr.Duration; d++) { ws.Rows[row + startRow_No + 4 + d].AutoFit(); } // ws.Rows[row + startRow_No + 4 + d].Height = 10 * 256; } //-------------------------------------------------------------------------------------------------- #endregion // // set to next step (add (2_Title + 2_Headers + 12_Row + 2_SpaceRow) = 18 step to start row) startRow_No += 18; #endregion } else if (numberSheet > 5) // Go to Next File's { #region Save and open New Excel file's try { // // Save in File *.xls // numberFile++; string NextFile = save2Excel.FileName.Substring(0, save2Excel.FileName.Length - 5) + numberFile.ToString() + ".xls"; ef.SaveXls(NextFile); ef = new ExcelFile(); ef.LimitReached += new LimitEventHandler(ef_LimitReached); ef.LimitNear += new LimitEventHandler(ef_LimitNear); ws = ef.Worksheets.Add("ProfessorsSchedule"); // // Try to open created excel file's // System.Diagnostics.Process.Start(NextFile); } finally { numberSheet = 1; startRow_No = 0; #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion } goto checkLoop; #endregion } else // 150 row is complete go to next worksheet { #region Create New WorkSheet startRow_No = 0; numberSheet++; ws = ef.Worksheets.Add("ProfessorsSchedule (" + numberSheet.ToString() + ")"); // // set column width again #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion // // go to check again goto checkLoop; #endregion } } #endregion #region Save and open Excel file's try { // // Save in File *.xls // string NextFile; if (numberFile <= 1) { NextFile = save2Excel.FileName; } else { NextFile = save2Excel.FileName.Substring(0, save2Excel.FileName.Length - 5) + numberFile.ToString() + ".xls"; } ef.SaveXls(NextFile); // // Try to open created excel file's // System.Diagnostics.Process.Start(NextFile); } catch { } #endregion } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source); } finally { this.Cursor = Cursors.Default; } } }
private void btnExportToExcel_Click(object sender, EventArgs e) { SaveFileDialog save2Excel = new SaveFileDialog(); save2Excel.Filter = @"Excel files|*.xls"; save2Excel.DefaultExt = "GroupsSchedule.xls"; save2Excel.FileName = "GroupsSchedule.xls"; if (save2Excel.ShowDialog() == DialogResult.OK) { CheckListCellsTextDataForm CLCTDF = new CheckListCellsTextDataForm(); // // check default items CLCTDF.chklstExportCell.Items.Clear(); CLCTDF.chklstExportCell.Items.Add("Course Code", false); CLCTDF.chklstExportCell.Items.Add("Course Name", true); CLCTDF.chklstExportCell.Items.Add("Professor Name", false); CLCTDF.chklstExportCell.Items.Add("Room Name", false); // CheckedListBox chklstCTD_buffer = CLCTDF.chklstExportCell; CLCTDF.Location = new Point(btnExportToExcel.Location.X + this.Location.X + 7, this.Location.Y + btnExportToExcel.Location.Y + btnExportToExcel.Size.Height + 32); CLCTDF.ShowDialog(); this.Cursor = Cursors.WaitCursor; try { // // delete exist file's // if (System.IO.File.Exists(save2Excel.FileName)) System.IO.File.Delete(save2Excel.FileName); // // If using Professional version, put your serial key below. Otherwise, keep following // line commented out as Free version doesn't have SetLicense method. // SpreadsheetInfo.SetLicense("YOUR-SERIAL-KEY-HERE"); ExcelFile ef = new ExcelFile(); ef.LimitReached += new LimitEventHandler(ef_LimitReached); ef.LimitNear += new LimitEventHandler(ef_LimitNear); ExcelWorksheet ws = ef.Worksheets.Add("GroupsSchedule"); int numberSheet = 1; int numberFile = 1; int startRow_No = 0; #region Group Name Style's CellStyle GroupTitleStyle = new CellStyle(); GroupTitleStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; GroupTitleStyle.VerticalAlignment = VerticalAlignmentStyle.Center; GroupTitleStyle.Font.Weight = ExcelFont.BoldWeight; GroupTitleStyle.FillPattern.SetSolid(Color.Azure); GroupTitleStyle.Font.Color = Color.RoyalBlue; GroupTitleStyle.WrapText = false; GroupTitleStyle.Borders.SetBorders(MultipleBorders.Right | MultipleBorders.Top, Color.Black, LineStyle.Thin); #endregion #region Header Style's (Example First Group Row[2~3] , Column[0~7]) CellStyle headerStyle = new CellStyle(); headerStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; headerStyle.VerticalAlignment = VerticalAlignmentStyle.Center; headerStyle.FillPattern.SetSolid(Color.Chocolate); headerStyle.Font.Weight = ExcelFont.BoldWeight; headerStyle.Font.Color = Color.White; headerStyle.WrapText = true; headerStyle.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin); #endregion #region Header Text Data object[,] headerText = new object[1, 8] { { "Time Slot", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" } }; #endregion #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion #region Any Cells Style's CellStyle AnyCellStyle = new CellStyle(); AnyCellStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center; AnyCellStyle.VerticalAlignment = VerticalAlignmentStyle.Center; AnyCellStyle.Font.Size = 14 * 16; AnyCellStyle.Font.Weight = ExcelFont.BoldWeight; AnyCellStyle.FillPattern.SetSolid(Color.White); AnyCellStyle.Font.Color = Color.Black; AnyCellStyle.WrapText = true; AnyCellStyle.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin); #endregion Random rand = new Random(); #region Read any Group in DataGridView and Save in Excel files foreach (var g in _groups) { checkLoop: if (startRow_No + 16 <= 149) // Write in Exist Worksheet { #region Save in Excel Row by database.Groups // // set Group Name Style ws.Cells.GetSubrangeAbsolute(0 + startRow_No, 0, 1 + startRow_No, 7).Style = GroupTitleStyle; // // set Group Name text in Title ws.Cells[0 + startRow_No, 0].Value = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Group: {0} - {1} {2}-{3} , ID: {4}", g.Value.Branch.Branch_Name, g.Value.Branch.Degree, g.Value.Semester_Entry_Year, g.Value.Semester_Entry_FS ? "1" : "2", g.Value.ID); // // set Header Styles ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 0, 3 + startRow_No, 7).Style = headerStyle; // // set Header Text data for (int j = 0; j < 8; j++) ws.Cells[3 + startRow_No, j].Value = headerText[0, j]; // // set Merged Band #region Merged Band // // First Row for Group Name ws.Cells.GetSubrangeAbsolute(0 + startRow_No, 0, 1 + startRow_No, 7).Merged = true; // // Second Row for Group Header "Time Slot" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 0, 3 + startRow_No, 0).Merged = true; // // Second Row for Group Header "Saturday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 1, 3 + startRow_No, 1).Merged = true; // // Second Row for Group Header "Sunday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 2, 3 + startRow_No, 2).Merged = true; // // Second Row for Group Header "Monday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 3, 3 + startRow_No, 3).Merged = true; // // Second Row for Group Header "Tuesday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 4, 3 + startRow_No, 4).Merged = true; // // Second Row for Group Header "Wednesday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 5, 3 + startRow_No, 5).Merged = true; // // Second Row for Group Header "Thursday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 6, 3 + startRow_No, 6).Merged = true; // // Second Row for Group Header "Friday" ws.Cells.GetSubrangeAbsolute(2 + startRow_No, 7, 3 + startRow_No, 7).Merged = true; #endregion // // set Time Slot Styles for (int row = 4; row < 12 + 4; row++) { ws.Cells[row + startRow_No, 0].Style = headerStyle; // headerStyle is equal by timeSlot Style ws.Cells[row + startRow_No, 0].Value = string.Format("{0} ~ {1}", (row - 4) + 8, (row - 4) + 9); } // // set all cells style's for (int row = 0; row < 12; row++) for (int column = 1; column < 8; column++) { ws.Cells[row + startRow_No + 4, column].Style = AnyCellStyle; } // // read any Classroom_Time for this Group and save that #region Read and Save Cells by this Group classrooms data //------------------------------------------------------------------------------------------------- // // Find any classroom for this group List<MakeClassSchedule.Classroom_Time> lstClassrooms = (from ng in DB.New_GroupsPerClasses join cr in DB.Classroom_Times on new { ng.Room_ID, ng.StartTime, ng.Day_No, ng.Class_ID } equals new { cr.Room_ID, cr.StartTime, cr.Day_No, cr.Class_ID } where g.Value.ID == ng.Group_ID select cr).ToList(); Color cellColor = Color.FromArgb(rand.Next(70, 250), rand.Next(70, 250), rand.Next(70, 250)); foreach (var cr in lstClassrooms) { // // 0. Course Code // 1. Course Name // 2. Professor Name // 3. Room Name string classesValue = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}{1}{2}{3}", (chklstCTD_buffer.GetItemChecked(1) ? cr.Class.Course.Name_Course + Environment.NewLine : string.Empty), // Course Name (chklstCTD_buffer.GetItemChecked(0) ? (cr.Class.Course.CourseCode.HasValue ? cr.Class.Course.CourseCode.Value : 0) + "\r\n" : ""), // Course Code (chklstCTD_buffer.GetItemChecked(2) ? cr.Professor.Name_Professor + Environment.NewLine : ""), // Professor Name (chklstCTD_buffer.GetItemChecked(3) ? cr.Room.Name_Room : "")); // Room Name int row = cr.StartTime; int column = cr.Day_No + 1; ws.Cells[row + startRow_No + 4, column].Value = classesValue; ws.Cells[row + startRow_No + 4, column].Style.FillPattern.SetSolid(cellColor); ws.Cells[row + startRow_No + 4, column].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.DashDotDot); try { if (cr.Duration > 1) ws.Cells.GetSubrangeAbsolute(row + startRow_No + 4, column, (row + startRow_No + 4) + (cr.Duration - 1), column).Merged = true; } catch { } // maybe two class overlap in a cell for (int d = 0; d < cr.Duration; d++) ws.Rows[row + startRow_No + 4 + d].AutoFit(); // ws.Rows[row + startRow_No + 4 + d].Height = 10 * 256; } //-------------------------------------------------------------------------------------------------- #endregion // // set to next step (add (2_Title + 2_Headers + 12_Row + 2_SpaceRow) = 18 step to start row) startRow_No += 18; #endregion } else if (numberSheet > 5) // Go to Next File's { #region Save and open New Excel file's try { // // Save in File *.xls // numberFile++; string NextFile = save2Excel.FileName.Substring(0, save2Excel.FileName.Length - 5) + numberFile.ToString() + ".xls"; ef.SaveXls(NextFile); ef = new ExcelFile(); ef.LimitReached += new LimitEventHandler(ef_LimitReached); ef.LimitNear += new LimitEventHandler(ef_LimitNear); ws = ef.Worksheets.Add("GroupsSchedule"); // // Try to open created excel file's // System.Diagnostics.Process.Start(NextFile); } finally { numberSheet = 1; startRow_No = 0; #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion } goto checkLoop; #endregion } else // 150 row is complete go to next worksheet { #region Create New WorkSheet startRow_No = 0; numberSheet++; ws = ef.Worksheets.Add("GroupsSchedule (" + numberSheet.ToString() + ")"); // // set column width again #region Column width // Column width of 16, 22, 22, 22, 22, 22, 22, 22 characters. ws.Columns[0].Width = 16 * 226; ws.Columns[1].Width = 22 * 226; ws.Columns[2].Width = 22 * 226; ws.Columns[3].Width = 22 * 226; ws.Columns[4].Width = 22 * 226; ws.Columns[5].Width = 22 * 226; ws.Columns[6].Width = 22 * 226; ws.Columns[7].Width = 22 * 226; #endregion // // go to check again goto checkLoop; #endregion } } #endregion #region Save and open Excel file's try { // // Save in File *.xls // string NextFile; if (numberFile <= 1) NextFile = save2Excel.FileName; else { NextFile = save2Excel.FileName.Substring(0, save2Excel.FileName.Length - 5) + numberFile.ToString() + ".xls"; } ef.SaveXls(NextFile); // // Try to open created excel file's // System.Diagnostics.Process.Start(NextFile); } catch { } #endregion } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source); } finally { this.Cursor = Cursors.Default; } } }