Advertisement
Margoshinka

пока так хули

Jan 28th, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Word = Microsoft.Office.Interop.Word;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. using System.Reflection;
  9.  
  10. namespace SYATP1
  11. {
  12.    
  13.  
  14.     class Program
  15.     {  
  16.         static string[,] dataFromTheTable;
  17.         static Dictionary<string, string> groupsAndPhrases;
  18.         static Dictionary<int, int> countingTriads;
  19.         static int numberOfPhrases;
  20.         static int numberOfNames;
  21.         static int numberOfGroups;
  22.         static HashSet<string> groups1;
  23.         static HashSet<string> groups2;
  24.         static List<string> names;
  25.         static int[] rangesByGroups;
  26.         static int k = 0;
  27.         static int randomName;
  28.          static int randomGroup ;
  29.         static int randomPhrases;
  30.  
  31.  
  32.         static void Readingdata()
  33.         {
  34.             Excel.Application excelApp = new Excel.Application();
  35.             //excelApp.Visible = true;
  36.             string file_path = @"C:\Users\margo\source\repos\SYATP1\Входные_данные.xlsx";
  37.             Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
  38.             Excel.Worksheet ws1 = wb.Worksheets["Имена"];
  39.             Excel.Worksheet ws2 = wb.Worksheets["Группы и пожелания"];
  40.             Excel.Worksheet ws3 = wb.Worksheets["Настройки"];
  41.             numberOfPhrases = ws2.Rows.CurrentRegion.EntireRow.Count;
  42.             numberOfNames = ws1.Rows.CurrentRegion.EntireRow.Count;
  43.             dataFromTheTable = new string [numberOfPhrases,2];
  44.             for (int j = 0; j < 2; j++)
  45.                 for (int i = 0; i < numberOfPhrases; i++)
  46.                     dataFromTheTable[i, j] = ws2.Cells[i + 1, j + 1].Text.ToString();
  47.             for (int j = 0; j < 2; j++)
  48.                 for (int i = 0; i < numberOfPhrases; i++)
  49.                  Console.WriteLine (dataFromTheTable[i, j]);
  50.             groupsAndPhrases = new Dictionary<string, string>();
  51.             for (int i = 0; i < numberOfPhrases; i++)
  52.             {
  53.                 groupsAndPhrases.Add(dataFromTheTable[i, 1], dataFromTheTable[i, 0]);
  54.             }
  55.             groupsAndPhrases = groupsAndPhrases.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
  56.             groups1 = new HashSet<string>();
  57.             groups2 = new HashSet<string>();
  58.             foreach (var item in groupsAndPhrases)
  59.                 groups1.Add( item.Value) ;
  60.              numberOfGroups = groups1.Count;
  61.                Console.WriteLine(numberOfGroups);
  62.             rangesByGroups = new int[numberOfGroups + 1];
  63.             for (int i = 0; i < numberOfGroups + 1; i++)
  64.             {
  65.                 rangesByGroups[i] = 1;
  66.  
  67.             }
  68.  
  69.            
  70.             foreach (var item in groupsAndPhrases)
  71.             {   if (!groups2.Add(item.Value))
  72.  
  73.                     rangesByGroups[k]++;
  74.  
  75.                
  76.                 else
  77.                
  78.                      k++;
  79.                
  80.  
  81.             }
  82.            
  83.             for (int i = 1; i < numberOfGroups; i++)
  84.             {
  85.                
  86.                 rangesByGroups[i + 1] += rangesByGroups[i];
  87.                
  88.             }
  89.  
  90.  
  91.  
  92.  
  93.                 for (int i = 1; i < numberOfGroups+1; i++)
  94.            
  95.                 Console.WriteLine(rangesByGroups[i]);
  96.  
  97.            
  98.             names = new List<string>();
  99.             for (int i = 0; i < numberOfNames; i++)
  100.                 names.Add( ws1.Cells[i+1, 1].Text.ToString());
  101.             foreach (var item in names)
  102.             {
  103.                 Console.WriteLine(item);
  104.             }
  105.             excelApp.Quit();
  106.         }
  107.         static void Random()
  108.         {
  109.             countingTriads = new Dictionary<int, int>();
  110.             Random rnd = new Random();
  111.              randomName = rnd.Next(1, numberOfNames + 1);
  112.             randomGroup = rnd.Next(1, numberOfGroups+1);
  113.             randomPhrases = rnd.Next(rangesByGroups[randomGroup - 1], rangesByGroups[randomGroup] + 1);
  114.         }
  115.         static void writingToTheWord()
  116.         {
  117.             object MissingObject = Missing.Value;
  118.             Word.Application wordApp = new Word.Application();
  119.             wordApp.Visible = true;
  120.             string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
  121.             Word.Document document = null;
  122.             try
  123.             {
  124.                 document = wordApp.Documents.Add(wordDocTemplate);
  125.                 document.Bookmarks["name"].Range.Text = names[randomName-1];
  126.                 document.Bookmarks["wish"].Range.Text = "счастья";
  127.                 var endOfFile = document.Bookmarks["\\endofdoc"].Range;
  128.                 endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
  129.                 endOfFile.InsertFile(@"C:\Users\margo\source\repos\SYATP1\Поздравления.docx");
  130.             }
  131.             catch (Exception e)
  132.             {
  133.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  134.                 wordApp.Quit();
  135.                 document = null;
  136.                 wordApp = null;
  137.                 Console.WriteLine(e.Message);
  138.             }
  139.            
  140.  
  141.         }
  142.  
  143.         static void Main(string[] args)
  144.         {
  145.            
  146.             Readingdata();
  147.             Random();
  148.             writingToTheWord();
  149.             //wordApp.Quit();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement