Advertisement
andruhovski

Fixed verison

Dec 21st, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using Aspose.Html;
  4.  
  5. namespace MakeNewTableUsingAsposeHTML
  6. {
  7.     class Stats
  8.     {
  9.         public string Age { get; set; }
  10.         public double Male { get; set; }
  11.         public double Female { get; set; }
  12.     }
  13.  
  14.  
  15.     class Program
  16.     {
  17.         static void Main()
  18.         {
  19.             const string cssStyles = @"table {border-color: lightgrey;border-collapse: collapse;width: 35%;}
  20.        th, tr {
  21.            border: 1px solid lightgray;
  22.        }
  23.  
  24.        .tablehead {
  25.            font-weight: bold;
  26.            background-color: lightblue;
  27.            text-align: left;
  28.        }
  29.  
  30.        tr:nth-child(even) {
  31.            background-color: #f0f0f0;
  32.        }";
  33.  
  34.             var document = new HTMLDocument();
  35.  
  36.             var stats = new List<Stats>
  37.                 {
  38.                     new Stats
  39.                     {
  40.                         Age = "15 - 17",
  41.                         Male = 5.3,
  42.                         Female = 2.8,
  43.                     },
  44.                     new Stats
  45.                     {
  46.                         Age = "18 - 24",
  47.                         Male = 16.2,
  48.                         Female = 17.3,
  49.                     },
  50.                     new Stats
  51.                     {
  52.                         Age = "25 - 34",
  53.                         Male = 25.5,
  54.                         Female = 14.2,
  55.                     },
  56.                     new Stats
  57.                     {
  58.                         Age = "35 - 44",
  59.                         Male = 22.2,
  60.                         Female = 14.1,
  61.                     },
  62.                     new Stats
  63.                     {
  64.                         Age = "45 - 54",
  65.                         Male = 20.7,
  66.                         Female = 17.2,
  67.                     },
  68.                     new Stats
  69.                     {
  70.                         Age = "55 - 64",
  71.                         Male = 18.3,
  72.                         Female = 12.9,
  73.                     },
  74.                     new Stats
  75.                     {
  76.                         Age = "65 - 74",
  77.                         Male = 11.1,
  78.                         Female = 6.9,
  79.                     },
  80.                     new Stats
  81.                     {
  82.                         Age = "75",
  83.                         Male = 4.0,
  84.                         Female = 4.5
  85.                     }
  86.                 };
  87.  
  88.             var title = document.CreateElement("title") as HTMLTitleElement;
  89.             if (title != null)
  90.             {
  91.                 title.TextContent = "Smoking by age and gender";
  92.             }
  93.             document.FirstChild.FirstChild.AppendChild(title);
  94.  
  95.  
  96.             if (document.CreateElement("link") is HTMLLinkElement styleElement)
  97.             {
  98.                 System.IO.File.WriteAllText(@"c:\aspose\main.css",cssStyles);
  99.                 styleElement.Href = @"file://c:\aspose\main.css";
  100.                 styleElement.Rel = "stylesheet";
  101.                 document.FirstChild.FirstChild.AppendChild(styleElement);
  102.             }
  103.  
  104.             var header = document.CreateElement("header") as HTMLElement;
  105.             var heading1 = document.CreateElement("h1") as HTMLHeadingElement;
  106.  
  107.             if ((header != null) && (heading1 != null))
  108.             {
  109.                 heading1.TextContent = "Smoking by age and gender";
  110.                 header.AppendChild(heading1);
  111.                 document.Body.AppendChild(header);
  112.             }
  113.  
  114.             var article = document.CreateElement("article") as HTMLElement;
  115.             var paragraph1 = document.CreateElement("p") as HTMLParagraphElement;
  116.             var paragraph2 = document.CreateElement("p") as HTMLParagraphElement;
  117.  
  118.             if (article != null && paragraph1 != null && paragraph2!=null)
  119.             {
  120.                 paragraph1.TextContent = "In males, the prevalence of smoking was";
  121.                 paragraph2.TextContent = "highest amongst those aged 25-34 years.";
  122.                 article.AppendChild(paragraph1);
  123.                 article.AppendChild(paragraph2);
  124.                 document.Body.AppendChild(article);
  125.             }
  126.  
  127.             var table = document.CreateElement("table") as HTMLTableElement;
  128.             if (document.CreateElement("thead") is HTMLTableSectionElement tHead)
  129.             {
  130.                 tHead.InnerHTML = "<tr class=\"tablehead\"> <th>Age</th> <th>Male</th> <th>Female</th> </tr>";
  131.                 if (table != null)
  132.                 {
  133.                     table.AppendChild(tHead);
  134.  
  135.                     HTMLTableSectionElement tBody = document.CreateElement("tbody") as HTMLTableSectionElement;
  136.                     foreach (var c in stats)
  137.                     {
  138.                         HTMLTableRowElement row = document.CreateElement("tr") as HTMLTableRowElement;
  139.  
  140.                         HTMLTableCellElement cellage = document.CreateElement("td") as HTMLTableCellElement;
  141.                         cellage.TextContent = c.Age;
  142.                         row.AppendChild(cellage);
  143.  
  144.                         HTMLTableCellElement cellmale = document.CreateElement("td") as HTMLTableCellElement;
  145.                         cellmale.TextContent = c.Male.ToString(CultureInfo.InvariantCulture) + "%";
  146.                         row.AppendChild(cellmale);
  147.  
  148.                         HTMLTableCellElement cellfemale = document.CreateElement("td") as HTMLTableCellElement;
  149.                         cellfemale.TextContent = c.Female.ToString(CultureInfo.InvariantCulture) + "%";
  150.                         row.AppendChild(cellfemale);
  151.  
  152.                         tBody.AppendChild(row);
  153.                     }
  154.  
  155.                     table.AppendChild(tBody);
  156.                     document.Body.AppendChild(table);
  157.                 }
  158.             }
  159.  
  160.             document.Save(@"C:\aspose\newdemo.html");
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement