-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.axaml.cs
135 lines (113 loc) · 4.75 KB
/
MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using MessageBox.Avalonia;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace LuckyDrawApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private async void ButtonDraw_Click(object sender, RoutedEventArgs e)
{
// Get the participants from the multiline textbox
var participants = textBoxParticipants.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
int numWinners = 5;
if (participants.Length < numWinners)
{
await MessageBoxManager.GetMessageBoxStandardWindow("Error", $"Please enter at least {numWinners} participants.").Show();
return;
}
// Shuffle the participants array
var random = new Random();
for (int i = participants.Length - 1; i > 0; i--)
{
int j = random.Next(i + 1);
string temp = participants[i];
participants[i] = participants[j];
participants[j] = temp;
}
// Get the first 5 winners from the shuffled participants
string[] winners = participants.Take(numWinners).ToArray();
// Display the winners in the Winners textbox
textBoxWinners.Text = string.Join(Environment.NewLine, winners);
}
private async void ButtonLoadExcel_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Title = "Select Excel file",
AllowMultiple = false,
Filters = new List<FileDialogFilter>
{
new FileDialogFilter { Name = "Excel files", Extensions = { "xlsx", "xls" } },
new FileDialogFilter { Name = "All files", Extensions = { "*" } }
}
};
string[] result = await openFileDialog.ShowAsync(this);
if (result != null && result.Length > 0)
{
LoadExcelData(result[0]);
}
}
private void LoadExcelData(string filePath)
{
List<string> participants = new List<string>();
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = document.WorkbookPart;
SharedStringTablePart sharedStringTablePart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
SharedStringTable sharedStringTable = sharedStringTablePart.SharedStringTable;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
int columnIndex = 1; // Change this value to the desired column index (1-based)
foreach (Row row in sheetData.Elements<Row>().Skip(1))
{
Cell cell = row.Descendants<Cell>().FirstOrDefault(c => GetColumnIndex(c.CellReference.Value) == columnIndex);
if (cell != null)
{
string cellValue;
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
int sharedStringIndex = int.Parse(cell.InnerText);
cellValue = sharedStringTable.Elements<SharedStringItem>().ElementAt(sharedStringIndex).InnerText;
}
else
{
cellValue = cell.InnerText;
}
if (!string.IsNullOrEmpty(cellValue))
{
participants.Add(cellValue);
}
}
}
}
textBoxParticipants.Text = string.Join(Environment.NewLine, participants);
}
private int GetColumnIndex(string cellReference)
{
string columnReference = new string(cellReference.ToCharArray().Where(c => char.IsLetter(c)).ToArray());
int columnIndex = 0;
foreach (char c in columnReference)
{
columnIndex = columnIndex * 26 + (c - 'A' + 1);
}
return columnIndex;
}
}
}