//Преобразовать строки с датами формата "с хх.хх.ххг. по хх.хх.ххг.", "хх,хх,хх.хх.ххг." в отдельные записи public void GetEntry(Data data) { if (data.data != "") { DateTime date1, date2; string[] splitDate = Regex.Split(data.date, ";"); for (int i = 0; i < splitDate.Length; i++) { if (Regex.IsMatch(splitDate[i], @"[с]\W\d\d.\d\d.\d\d[г].\W[п][о]\W\d\d.\d\d.\d\d[г].")) { var index1 = splitDate[i].IndexOf("с"); var index2 = splitDate[i].IndexOf("по"); date1 = DateTime.ParseExact(splitDate[i].Substring(index1 + 2, 8), "dd.MM.yy", CultureInfo.InvariantCulture); date2 = DateTime.ParseExact(splitDate[i].Substring(index2 + 3, 8), "dd.MM.yy", CultureInfo.InvariantCulture); int shutdownCounter = 0; while (date1 != date2) { DataEntry entry = new DataEntry(data.number, date1.ToString("dd.MM.yy"), data.subject, data.professor, data.group, data.lectureHall, data.type); RemoveSpareSimbolsInDate(entry); entryList.Add(entry); //Заполнение списка дат if (!dateList.Contains(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture))) { dateList.Add(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture)); } //Заполнение списка дат date1 = date1.AddDays(7); if (date1 == date2) { entry = new DataEntry(data.number, date1.ToString("dd.MM.yy"), data.subject, data.professor, data.group, data.lectureHall, data.type); RemoveSpareSimbolsInDate(entry); entryList.Add(entry); //Заполнение списка дат if (!dateList.Contains(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture))) { dateList.Add(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture)); } //Заполнение списка дат } if (shutdownCounter == 25) { break; } shutdownCounter++; } } else if (splitDate[i].Contains(",")) { var index = Regex.Match(splitDate[i], @"\d\d[.]\d\d[г]"); string monthAndYear = splitDate[i].Substring(index.Index, 5); string[] splitStr = Regex.Split(splitDate[i], ","); for (int j = 0; j < splitStr.Length - 1; j++) { splitStr[j] = splitStr[j] + "." + monthAndYear; } for (int k = 0; k < splitStr.Length; k++) { DataEntry entry = new DataEntry(data.number, splitStr[k], data.subject, data.professor, data.group, data.lectureHall, data.type); RemoveSpareSimbolsInDate(entry); entryList.Add(entry); //Заполнение списка дат if (!dateList.Contains(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture))) { dateList.Add(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture)); } //Заполнение списка дат } } else if (Regex.IsMatch(splitDate[i], @"\d\d[.]\d\d[.]\d\d[г].")) { DataEntry entry = new DataEntry(data.number, splitDate[i], data.subject, data.professor, data.group, data.lectureHall, data.type); RemoveSpareSimbolsInDate(entry); entryList.Add(entry); //Заполнение списка дат if (!dateList.Contains(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture))) { dateList.Add(DateTime.ParseExact(entry.date, "dd.MM.yy", CultureInfo.InvariantCulture)); } //Заполнение списка дат } } } }
public void CreateExcelFile(DataEntry entry) { var info = entry; Excel.Application xlApp = new Excel.Application(); if (xlApp == null) { MessageBox.Show("Excel is not properly installed!!"); return; } object misValue = System.Reflection.Missing.Value; var xlWorkBook = xlApp.Workbooks.Add(misValue); var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); var cells = xlWorkSheet.Cells; cells[3, 1] = "Понедельник"; cells[11, 1] = "Вторник"; cells[19, 1] = "Среда"; cells[30, 1] = "Четверг"; cells[41, 1] = "Пятница"; cells[52, 1] = "Суббота"; //Понедельник var range = xlWorkSheet.Range[cells[3, 1], cells[10, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //Вторник range = xlWorkSheet.Range[cells[11, 1], cells[18, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //Среда range = xlWorkSheet.Range[cells[19, 1], cells[26, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //Четверг range = xlWorkSheet.Range[cells[27, 1], cells[34, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //Пятница range = xlWorkSheet.Range[cells[35, 1], cells[42, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //Суббота range = xlWorkSheet.Range[cells[43, 1], cells[50, 1]]; range.Merge(); range.Cells.Orientation = Excel.XlOrientation.xlUpward; range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; foreach (string key in lections.Keys) { switch (DateTime.ParseExact(key, "dd.MM.yy", CultureInfo.InvariantCulture).DayOfWeek) { case DayOfWeek.Monday: { mon++; break; } case DayOfWeek.Tuesday: { tue++; break; } case DayOfWeek.Wednesday: { wed++; break; } case DayOfWeek.Thursday: { thur++; break; } case DayOfWeek.Friday: { fri++; break; } case DayOfWeek.Saturday: { sat++; break; } } } int y = 2; int a1 = 3; int a2 = 3; int a3 = 3; int a4 = 3; int a5 = 3; int a6 = 3; foreach (string key in lections.Keys) { switch (DateTime.ParseExact(key, "dd.MM.yy", CultureInfo.InvariantCulture).DayOfWeek) { case DayOfWeek.Monday: { CreateBlockTemplate(a1, y, xlWorkSheet, cells, key); FillBlock(a1, y, xlWorkSheet, cells, key, lections[key]); a1 += 8; break; } case DayOfWeek.Tuesday: { CreateBlockTemplate(a2, y + 8, xlWorkSheet, cells, key); FillBlock(a2, y + 8, xlWorkSheet, cells, key, lections[key]); a2 += 8; break; } case DayOfWeek.Wednesday: { CreateBlockTemplate(a3, y + 16, xlWorkSheet, cells, key); FillBlock(a3, y + 16, xlWorkSheet, cells, key, lections[key]); a3 += 8; break; } case DayOfWeek.Thursday: { CreateBlockTemplate(a4, y + 24, xlWorkSheet, cells, key); FillBlock(a4, y + 24, xlWorkSheet, cells, key, lections[key]); a4 += 8; break; } case DayOfWeek.Friday: { CreateBlockTemplate(a5, y + 32, xlWorkSheet, cells, key); FillBlock(a5, y + 32, xlWorkSheet, cells, key, lections[key]); a5 += 8; break; } case DayOfWeek.Saturday: { CreateBlockTemplate(a6, y + 40, xlWorkSheet, cells, key); FillBlock(a6, y + 40, xlWorkSheet, cells, key, lections[key]); a6 += 8; break; } } } SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "Файл Excel|*.XLSX;*.XLS"; saveDialog.RestoreDirectory = true; xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); }