From C# to HTML: Rendering Server-Side Data in the Browser

Exporting C# Data to Clean HTML Tables and Reports

Overview

Exporting C# data to HTML produces portable, browser-friendly reports and tables that are easy to share, style, and print. Common scenarios: exporting database query results, serializing objects for email, generating printable reports, or producing static dashboards.

Approaches (choose by scale/needs)

  • Razor / ASP.NET MVC or Razor Pages: best for server-rendered, template-driven HTML with strong separation of markup and logic.
  • StringBuilder / Manual templates: lightweight for small, quick exports without dependencies.
  • Templating libraries (RazorLight, Scriban): embed templates in non-web apps; good for reusable templates.
  • Third-party reporting tools (FastReport, Stimulsoft): when you need complex layouts, charts, paging, and export options beyond plain HTML.
  • CSV → HTML / Client-side rendering: export CSV and let client-side JS render tables (DataTables) when interactivity is needed.

Step-by-step (simple, reusable method using RazorLight)

  1. Define a DTO list in C# (e.g., List).
  2. Create a Razor template file with a table and placeholders for headers and rows.
  3. Use RazorLight to compile the template and render HTML with your model.
  4. Save HTML to a file, return as HTTP response, or embed in email.

Example Razor snippet:

cshtml

<table> <thead> <tr> <th>Order ID</th><th>Date</th><th>Customer</th><th>Total</th> </tr> </thead> <tbody> @foreach(var item in Model) { <tr> <td>@item.Id</td><td>@item.Date.ToString(“yyyy-MM-dd”)</td> <td>@item.Customer</td><td>@item.Total.ToString(“C”)</td> </tr> } </tbody> </table>

Minimal C# example (RazorLight usage)

csharp

var engine = new RazorLight.RazorLightEngineBuilder() .UseEmbeddedResourcesProject(typeof(Program)) .UseMemoryCachingProvider() .Build(); string template = /* load template string */; string result = await engine.CompileRenderAsync(“templateKey”, template, modelList); File.WriteAllText(“report.html”, result);

Styling & Accessibility

  • Use a simple CSS reset and responsive table styles.
  • Add semantic elements (caption, thead, tbody, th scope).
  • Include aria attributes and consider keyboard navigation for interactive tables.

Performance & Security

  • Escape user data (HTML-encode) when injecting into templates.
  • For large datasets, paginate or stream rows to avoid high memory usage.
  • Cache compiled templates for reuse.

When to use which option (short table)

Need Recommended
Server-rendered templates, MVC apps Razor (built-in)
Non-web app template rendering RazorLight or Scriban
Quick one-off export StringBuilder/manual
Complex reports (charts, paging) Reporting tools

Output delivery

  • Save to .html file for downloads.
  • Return as HTTP response with content-type text/html or content-disposition attachment.
  • Embed HTML in emails (inline CSS) — inline styles recommended for email clients.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *