C # - HTML reporting

In my winform application I have 4 datasets that get values ​​from my database. Values ​​such as the number of products that I have in my product table and category information. I was wondering how I can save data on the html info site. I want to create an html template so that I can present the information in a good way. How can i do this? Eny is a good guide that explains how to do this?

+4
source share
4 answers

This seems like a great job for something like RazorEngine . You can define a template using Razor syntax and then render the content using the RazorEngine template service, for example:

@helper RenderItem(Item item) { <tr> <td>item.Name</td> <td>item.Price</td> </tr> } <html> <head></head> <body> <table> @foreach (Item item in Model.Items) { @RenderItem(item) } </table> </body> </html> 
+5
source

You can save the DataSet as XML and then convert it using XSLT .

You can see these samples:

+2
source

I personally recommend creating HTML with Linq in Xml (using System.Xml.Linq)

You don't even need to use a strict XHTML scheme, but you will get a bunch of mileage from Xml.Linq. Here is a snippet from my own codebases:

  #region Table Dump Implementation private static XNode Dump<T>(IEnumerable<T> items, IEnumerable<string> header, params Func<T, string>[] columns) { if (!items.Any()) return null; var html = items.Aggregate(new XElement("table", new XAttribute("border", 1)), (table, item) => { table.Add(columns.Aggregate(new XElement("tr"), (row, cell) => { row.Add(new XElement("td", EvalColumn(cell, item))); return row; } )); return table; }); html.AddFirst(header.Aggregate(new XElement("tr"), (row, caption) => { row.Add(new XElement("th", caption)); return row; })); return html; } private static XNode EvalColumn<T>(Func<T, string> cell, T item) { var raw = cell(item); try { var xml = XElement.Parse(raw); return xml; } catch (XmlException) { return new XText(raw); } } #endregion #region Dot Diagrams public void LinkDiagram(Digraph graph, string id) { if (!graph.AllNodes.Any()) return; var img = Path.GetFileName(GenDiagramFile(graph, _directory, id)); _body.Add( new XElement("a", new XAttribute("href", img), new XElement("h4", "Link naar: " + graph.name), new XElement("img", new XAttribute("border", 1), new XAttribute("src", img), new XAttribute("width", "40%")))); } 

Note that it is extremely easy to use inline HTML text, and (as long as it is valid XML), using a helper helper:

  public void GenericAppend(string content) { if (!string.IsNullOrEmpty(content)) _body.Add(XElement.Parse(content)); } 
+1
source

What you want is pretty simple, so you can just generate the html directly and use some pre-created CSS for styling. However, if you want something more complicated, check out the Windward Reports .

0
source

Source: https://habr.com/ru/post/1346470/


All Articles