Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Reflection;
- namespace SYATP1
- {
- class Program
- {
- static string[,] dataFromTheTable;
- static Dictionary<string, string> groupsAndPhrases;
- static Dictionary<int, int> countingTriads;
- static int numberOfPhrases;
- static int numberOfNames;
- static int numberOfGroups;
- static HashSet<string> groups1;
- static HashSet<string> groups2;
- static List<string> names;
- static int[] rangesByGroups;
- static int k = 0;
- static int randomName;
- static int randomGroup ;
- static int randomPhrases;
- static void Readingdata()
- {
- Excel.Application excelApp = new Excel.Application();
- //excelApp.Visible = true;
- string file_path = @"C:\Users\margo\source\repos\SYATP1\Входные_данные.xlsx";
- Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
- Excel.Worksheet ws1 = wb.Worksheets["Имена"];
- Excel.Worksheet ws2 = wb.Worksheets["Группы и пожелания"];
- Excel.Worksheet ws3 = wb.Worksheets["Настройки"];
- numberOfPhrases = ws2.Rows.CurrentRegion.EntireRow.Count;
- numberOfNames = ws1.Rows.CurrentRegion.EntireRow.Count;
- dataFromTheTable = new string [numberOfPhrases,2];
- for (int j = 0; j < 2; j++)
- for (int i = 0; i < numberOfPhrases; i++)
- dataFromTheTable[i, j] = ws2.Cells[i + 1, j + 1].Text.ToString();
- for (int j = 0; j < 2; j++)
- for (int i = 0; i < numberOfPhrases; i++)
- Console.WriteLine (dataFromTheTable[i, j]);
- groupsAndPhrases = new Dictionary<string, string>();
- for (int i = 0; i < numberOfPhrases; i++)
- {
- groupsAndPhrases.Add(dataFromTheTable[i, 1], dataFromTheTable[i, 0]);
- }
- groupsAndPhrases = groupsAndPhrases.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
- groups1 = new HashSet<string>();
- groups2 = new HashSet<string>();
- foreach (var item in groupsAndPhrases)
- groups1.Add( item.Value) ;
- numberOfGroups = groups1.Count;
- Console.WriteLine(numberOfGroups);
- rangesByGroups = new int[numberOfGroups + 1];
- for (int i = 0; i < numberOfGroups + 1; i++)
- {
- rangesByGroups[i] = 1;
- }
- foreach (var item in groupsAndPhrases)
- { if (!groups2.Add(item.Value))
- rangesByGroups[k]++;
- else
- k++;
- }
- for (int i = 1; i < numberOfGroups; i++)
- {
- rangesByGroups[i + 1] += rangesByGroups[i];
- }
- for (int i = 1; i < numberOfGroups+1; i++)
- Console.WriteLine(rangesByGroups[i]);
- names = new List<string>();
- for (int i = 0; i < numberOfNames; i++)
- names.Add( ws1.Cells[i+1, 1].Text.ToString());
- foreach (var item in names)
- {
- Console.WriteLine(item);
- }
- excelApp.Quit();
- }
- static void Random()
- {
- countingTriads = new Dictionary<int, int>();
- Random rnd = new Random();
- randomName = rnd.Next(1, numberOfNames + 1);
- randomGroup = rnd.Next(1, numberOfGroups+1);
- randomPhrases = rnd.Next(rangesByGroups[randomGroup - 1], rangesByGroups[randomGroup] + 1);
- }
- static void writingToTheWord()
- {
- object MissingObject = Missing.Value;
- Word.Application wordApp = new Word.Application();
- wordApp.Visible = true;
- string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
- Word.Document document = null;
- try
- {
- document = wordApp.Documents.Add(wordDocTemplate);
- document.Bookmarks["name"].Range.Text = names[randomName-1];
- document.Bookmarks["wish"].Range.Text = "счастья";
- var endOfFile = document.Bookmarks["\\endofdoc"].Range;
- endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
- endOfFile.InsertFile(@"C:\Users\margo\source\repos\SYATP1\Поздравления.docx");
- }
- catch (Exception e)
- {
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- Console.WriteLine(e.Message);
- }
- }
- static void Main(string[] args)
- {
- Readingdata();
- Random();
- writingToTheWord();
- //wordApp.Quit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement