What is the best way to produce the report in asp.net core?

16,250

I am doing a project in Asp.Net Core 2.1. I used Rotativa.AspNetCore for reporting feature. It is free you can get from the NUGET just install that and thats it.

It Convert the view to pdf. You just pass your model to view and create html table or what you want and along with css. This library convert your view to PDF.

[HttpGet]
public async Task<IActionResult> Registration(int id)
{
    var reg = await _context.Registrations
        .Include(x => x.Property.Block)
        .Include(x => x.Property.Phase)
        .Include(x => x.Property.Street)
        .Include(x => x.Nomines)
        .FirstOrDefaultAsync(x => x.Id == id);
    var report = new ViewAsPdf("Registration")
    {
        PageMargins = { Left = 5, Bottom = 5, Right = 5, Top = 5 },
        Model = reg
    };
    return report;
}

Here is the html for that view. I remove long html stuff but you can check my code.

<div class="row">
        <h1 class="report-heading text-center">Registration Form</h1>
        <div class="col-xs-9">
            <div class="form-group row">
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Date:-</label>
                    <label class="col-xs-9 report-field">@(Model.DateTime?.ToString("dd-MMM-yyyy"))</label>
                </div>
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-4 report-label">Form #</label>
                    <label class="col-xs-8 report-field">@(Model.FormNo)</label>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-5 report-label">Plot/House #</label>
                    <label class="col-xs-7 report-field">@(Model?.Property?.No)</label>
                </div>
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Street:-</label>
                    <label class="col-xs-9 report-field">@(Model?.Property?.Street?.Name)</label>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Marla:-</label>
                    <label class="col-xs-9 report-field">@(Model?.Property?.Marla)</label>
                </div>
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Sqft:- </label>
                    <label class="col-xs-9 report-field">@(Model?.Property?.Sqft)</label>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Block:-</label>
                    <label class="col-xs-9 report-field">@(Model?.Property?.Block?.Name)</label>
                </div>
                <div class="col-xs-6 no-padding">
                    <label class="col-xs-3 report-label">Phase:-</label>
                    <label class="col-xs-9 report-field">@(Model?.Property?.Phase?.Name)</label>
                </div>
            </div>
        </div>
</div>

And i you want to export that view to excel then you just need to add header before returning the view. It is a sample.

public ActionResult Export()
{
    Response.AddHeader("Content-Type", "application/vnd.ms-excel");
    return View();
}

I hope this will help you.

Share:
16,250

Related videos on Youtube

Atiar Rahman Nayeem
Author by

Atiar Rahman Nayeem

Updated on October 15, 2022

Comments

  • Atiar Rahman Nayeem
    Atiar Rahman Nayeem over 1 year

    Can anyone advise me how to create the report in a asp.net core program. I would like to export the report as excel, pdf and word. What is the best way to produce the report in asp.net core? I am looking for advise from expertise

  • Waleed Naveed
    Waleed Naveed almost 3 years
    Not a good option. This library has multiple issues and there is no answer/update by the developer on github page. Highly recommend not to use this library else your life will be hell.