Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating an HTML file with StringBuilder
private static void AddResultsFromVotes(StringBuilder html, string name, decimal voteCount, Dictionary<string, int> dictionary, int cancelledVotes)
{
var sum = dictionary.Sum(c => c.Value);
var index = 1;
html.Append($"<br/>");
html.Append($"<h2>Voturi {name}</h2><br />");
html.Append($"Total voturi {name}: {voteCount}<br />");
html.Append($"Total voturi anulate in {name}: {cancelledVotes}<br />");
voteCount -= cancelledVotes;
var countedPercentage = Math.Round(sum / (decimal)voteCount * 100, 2);
html.Append($"Voturi numarate {name}: {sum} - <b>{Math.Round(countedPercentage, 2)}% </b>*sunt sanse ca procentajul voturilor numarate sa fie gresit<br /><br />");
html.Append($"<br/>");
html.Append($"<table >");
html.Append($"<tr>");
html.Append($"<th>Pozitie</th>");
html.Append($"<th>Nume</th>");
html.Append($"<th>Procent</th>");
html.Append($"<th>Voturi</th>");
html.Append($"<th>Distanta fata de cel anterior</th>");
html.Append($"</tr>");
KeyValuePair<string, int> lastKvp = new KeyValuePair<string, int>();
foreach (var kvp in dictionary.OrderByDescending(d => d.Value))
{
decimal percentage = Math.Round((decimal)kvp.Value / (decimal)sum * 100, 2);
html.Append($"<tr>");
html.Append($"<td>{index++}</td><td>{kvp.Key}</td><td><b>{percentage}%</b></td><td>{kvp.Value:N0}</td>");
if (lastKvp.Value != 0)
{
html.Append(
$"<td><b>{(lastKvp.Value - kvp.Value):N0}</b></td>");
}
lastKvp = kvp;
html.Append($"</tr>");
}
html.Append($"</table >");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment