Jan Blaha

Blog about software development

Excel reports in c# and asp.net

jsreport

Quite some time ago I blogged about rendering pdf reports in c#. Recently we have added excel reports into jsreport and it was released with a little delay also into .NET. This means you should be able to use both html-to-xlsx and xlsx recipes to create excel files from your .NET environments now.

Quick Start

First you need to get jsreport. You can use standalone server, jsreportonline service or run jsreport instance through .NET jsreport sdk which will start jsreport along with your .NET process. The easiest is to start with the last option.

So first install jsreport.Local and jsreport.Binary nugets:

Install-Package jsreport.Local
Install-Package jsreport.Binary

Then initialize local rendering service using the following code:

var rs = new LocalReporting().UseBinary(JsReportBinary.GetBinary()).AsUtility().Create();

Now you are ready for rendering excel files. The easiest is to use html-to-xlsx recipe which will convert input html table into xlsx file:

var report = await rs.RenderAsync(new RenderRequest()
{
    Template = new Template()
    {
        Content = "<table><tr><td>Hello World</td></tr></table>",
        Recipe = Recipe.HtmlToXlsx
    }
});

The report.Content now contains stream to xslx file. Pretty simple right? There are several limitations described in the documentation. The most important is to keep in mind that you need provide a valid table inside html. Everything outside table wont be transformed to the excel file. You can also use several css styles like width, height, borders, align or colors.

Html rendering

You probably won't assemble html using a StringBuilder. Better is to use javascript templating engines jsreport evaluates. Extending previous RenderRequest with some input data and dynamic html rendering using jsrender looks following:

new RenderRequest() {
    Template = new Template() {
        Content =
            "<table>" +
            "{{for people}}" +
            "<tr><td>{{:#data}}</td></tr>" +
            "{{/for}}" +
            "</table>",
        Recipe = Recipe.HtmlToXlsx,
        Engine = Engine.JsRender
    },
    Data = new  {
        people = new [] { "Jan Blaha", "John Lennon"}
    }
}

ASP.NET MVC

This may sound crazy but you can also render excel report directly from your ASP.NET MVC views. This can be done using jsreport.MVC nuget package. It will let you to assemble html using for example Razor engine inside your views and convert to pdf or excel afterwards before sending out from the server.

First install jsreport.MVC nuget package:

Install-Package jsreport.MVC

Then register JsReportFilterAttribute asp.net mvc filter

filters.Add(new JsReportFilterAttribute(new LocalReporting()
        .UseBinary(JsReportBinary.GetBinary())
        .AsUtility()
        .Create()));

Now you can tag any controller action with EnableJsReport and specify how shall be the output transformed. For example to transform html into excel you can use:

[EnableJsReport()]
public ActionResult Xlsx()
{
  HttpContext.JsReportFeature().Recipe(Recipe.HtmlToXlsx);
  return View();
}

Conclusion

Excel reports is just a small part of jsreport platform. You can render also pdf, xml or csv reports in the same unified way. You can also let your customers to customize reports and jsreport will safely render desired outputs or completely separate your reporting out from your application into a loosely coupled reporting server.

I encourage you to check out jsreport.net and in particular .NET jsreport sdk for more .NET demos, videos and tutorials.

last blog posts


09-12-2017 18:40 jsreport

Quite some time ago I blogged about rendering pdf reports in c#. Recently we have added excel reports into jsreport and it was released with a little delay also into .NET. This means you should be able to use both html-to-xlsx and xlsx recipes to create excel files from your .NET environments now.

read more

09-10-2015 14:09 AWS

Such a very common thing like adding an existing external volume to Amazon elastic beanstalk is not easily supported out of the box. The official blog mentions only how to attach a snapshot or how to attach and overwrite a new volume every time the service starts. It took me a while to make the config file actually adding an existing volume without formatting it every time so I share it here with you...

read more

04-10-2015 14:09 jsreport

The best practice when adding email notifications feature to your system is to separate as much as you can from email body assembling to email sending outside of the core system. The emails templates quite likely often changes and you don't want to deploy the system because of every single notification change. The best is to just separate everything into an external system and give the access to your PR or Marketing department so they change emails as the time goes without affecting the core system.

read more

Jan Blaha

About author

Hi! My name is Jan Blaha. I'm software developer and startup enthusiastic. See my current work.