andruhovski icon

Fixed verison

andruhovski | PRO | 12/21/18 08:31:35 PM UTC | 0 ⭐ | 623 👁️ | Never ⏰ | []
C# |

5.8 KB

|

None

|

0 👍

/

0 👎

using System.Collections.Generic;
using System.Globalization;
using Aspose.Html;
 
namespace MakeNewTableUsingAsposeHTML
{
    class Stats
    {
        public string Age { get; set; }
        public double Male { get; set; }
        public double Female { get; set; }
    }
 
 
    class Program
    {
        static void Main()
        {
            const string cssStyles = @"table {border-color: lightgrey;border-collapse: collapse;width: 35%;}
        th, tr {
            border: 1px solid lightgray;
        }
 
        .tablehead {
            font-weight: bold;
            background-color: lightblue;
            text-align: left;
        }
 
        tr:nth-child(even) {
            background-color: #f0f0f0;
        }";
 
            var document = new HTMLDocument();
 
            var stats = new List<Stats>
                {
                    new Stats
                    {
                        Age = "15 - 17",
                        Male = 5.3,
                        Female = 2.8,
                    },
                    new Stats
                    {
                        Age = "18 - 24",
                        Male = 16.2,
                        Female = 17.3,
                    },
                    new Stats
                    {
                        Age = "25 - 34",
                        Male = 25.5,
                        Female = 14.2,
                    },
                    new Stats
                    {
                        Age = "35 - 44",
                        Male = 22.2,
                        Female = 14.1,
                    },
                    new Stats
                    {
                        Age = "45 - 54",
                        Male = 20.7,
                        Female = 17.2,
                    },
                    new Stats
                    {
                        Age = "55 - 64",
                        Male = 18.3,
                        Female = 12.9,
                    },
                    new Stats
                    {
                        Age = "65 - 74",
                        Male = 11.1,
                        Female = 6.9,
                    },
                    new Stats
                    {
                        Age = "75",
                        Male = 4.0,
                        Female = 4.5
                    }
                };
 
            var title = document.CreateElement("title") as HTMLTitleElement;
            if (title != null)
            {
                title.TextContent = "Smoking by age and gender";
            }
            document.FirstChild.FirstChild.AppendChild(title);
 
 
            if (document.CreateElement("link") is HTMLLinkElement styleElement)
            {
                System.IO.File.WriteAllText(@"c:\aspose\main.css",cssStyles);
                styleElement.Href = @"file://c:\aspose\main.css";
                styleElement.Rel = "stylesheet";
                document.FirstChild.FirstChild.AppendChild(styleElement);
            }
 
            var header = document.CreateElement("header") as HTMLElement;
            var heading1 = document.CreateElement("h1") as HTMLHeadingElement;
 
            if ((header != null) && (heading1 != null))
            {
                heading1.TextContent = "Smoking by age and gender";
                header.AppendChild(heading1);
                document.Body.AppendChild(header);
            }
 
            var article = document.CreateElement("article") as HTMLElement;
            var paragraph1 = document.CreateElement("p") as HTMLParagraphElement;
            var paragraph2 = document.CreateElement("p") as HTMLParagraphElement;
 
            if (article != null && paragraph1 != null && paragraph2!=null)
            {
                paragraph1.TextContent = "In males, the prevalence of smoking was";
                paragraph2.TextContent = "highest amongst those aged 25-34 years.";
                article.AppendChild(paragraph1);
                article.AppendChild(paragraph2);
                document.Body.AppendChild(article);
            }
 
            var table = document.CreateElement("table") as HTMLTableElement;
            if (document.CreateElement("thead") is HTMLTableSectionElement tHead)
            {
                tHead.InnerHTML = "<tr class=\"tablehead\"> <th>Age</th> <th>Male</th> <th>Female</th> </tr>";
                if (table != null)
                {
                    table.AppendChild(tHead);
 
                    HTMLTableSectionElement tBody = document.CreateElement("tbody") as HTMLTableSectionElement;
                    foreach (var c in stats)
                    {
                        HTMLTableRowElement row = document.CreateElement("tr") as HTMLTableRowElement;
 
                        HTMLTableCellElement cellage = document.CreateElement("td") as HTMLTableCellElement;
                        cellage.TextContent = c.Age;
                        row.AppendChild(cellage);
 
                        HTMLTableCellElement cellmale = document.CreateElement("td") as HTMLTableCellElement;
                        cellmale.TextContent = c.Male.ToString(CultureInfo.InvariantCulture) + "%";
                        row.AppendChild(cellmale);
 
                        HTMLTableCellElement cellfemale = document.CreateElement("td") as HTMLTableCellElement;
                        cellfemale.TextContent = c.Female.ToString(CultureInfo.InvariantCulture) + "%";
                        row.AppendChild(cellfemale);
 
                        tBody.AppendChild(row);
                    }
 
                    table.AppendChild(tBody);
                    document.Body.AppendChild(table);
                }
            }
 
            document.Save(@"C:\aspose\newdemo.html");
        }
    }
}

Comments