Introduction

In this article, we will learn MVC, using AngularJS & Crystal Report from Server side data, using Visual Studio 2015.
What is Crystal Report?

Crystal Report is a business intelligence Application. Crystal Report is powered by SAP. It is used to design and generate reports from wide range of data sources.

Why using SAP Crystal Report

  • Provide powerful report tool.
  • Create real-time operational report.
  • Use Web, Windows & mobile devices.
  • Make flexible & customizable report.

Tools Installation

We will download the crystal report IDE from this link. After registration, “exe” will be downloaded automatically.

MVC

After completing the installation, restart Visual Studio.

Create MVC Project

Open Visual Studio 2015.

MVC

Go to Menu >New > Click Project > it will open New project popup.

MVC

Select ASP.NET project and give to the solution name, followed by clicking OK button. Again one popup should appear as New ASP.NET Web Application.

MVC

Select MVC Template and click OK to start the project. Configure an AngularJS in this MVC project.

If you have any doubts about configuration, visit the links given below.

Create new folder as Report and right click on the folder. Add Crystal Report in Solution Explorer.

MVC

In this Report folder, add one Dataset for Server side Data binding and it will be used to design the customizable reports.

MVC

Use this DataSet to export for design & developing the report.

MVC

Easy way to drag & drop the controls are used in the design, as shown.

MVC

Write a method to access the data and bind to the data from the Server in to Crystal Report in Controller. I am writting the code given below in my home controller file.

C# code

  1. public ActionResult ExportExcel()
  2.         {
  3.             List<BookModel> BookList = new List<BookModel>();
  4.             DataSetReport DsReport = new DataSetReport();
  5.             using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DbSqlCon”].ConnectionString))
  6.             {
  7.                 var cmd = new SqlCommand(“BookMaster_SP”, con);
  8.                 cmd.CommandType = CommandType.StoredProcedure;
  9.                 cmd.Parameters.Add(new SqlParameter(“@Mode”, SqlDbType.VarChar)).Value = “GET”;
  10.                 con.Open();
  11.                 (new SqlDataAdapter(cmd)).Fill(DsReport.Tables[“BookList”]);
  12.             }
  13.             ReportDocument rd = new ReportDocument();
  14.             rd.Load(Path.Combine(Server.MapPath(“~/Report”), “ReportBookList.rpt”));
  15.             rd.SetDataSource(DsReport.Tables[“BookList”]);
  16.             Response.Buffer = false;
  17.             Response.ClearContent();
  18.             Response.ClearHeaders();
  19.             Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.ExcelWorkbook);
  20.             stream.Seek(0, SeekOrigin.Begin);
  21.             return File(stream, “application/pdf”“ReportBookList.xlsx”);
  22.         }

Proceed, as shown below.

  1. rd.Load(Path.Combine(Server.MapPath(“~/Report”), “ReportBookList.rpt”));

In this article, I have demonstrated only EXCEL & PDF reports.

  1. Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.ExcelWorkbook);

You can have 16 types of reports format export, using Crystal Reports.

MVC

Now, we have done the server part of the work. Start writing the code to HTML & Angular JS controller file. Already, I have written the article for data load in datatable. I will be utilizing the same code in this article.

JS code

  1. $scope.ExcelReport=function()
  2.     {
  3.         $window.open(“Home/ExportExcel”“_blank”);
  4.     }
  5.     $scope.PdfReport = function () {
  6.         $window.open(“Home/ExportPdf”“_blank”);
  7.     }

Before writing this code, you must inject the $Window keyword in Angular Controller.

  1. uiroute.controller(‘BookController’function ($scope, BookService, $window)

Call the above JS function in HTML buttons.

  1. <button class=“btn btn-success “ ng-click=“ExcelReport()”>Excel Report</button>
  2. <button class=“btn btn-danger “ ng-click=“PdfReport()”>Pdf Report</button>

Yes we have done all the client side part of work. Now, you run the Application.

Output 1

MVC

Output 2

MVC

Finally, we succeded to export, using Crystal Report in MVC AngularJS.

Source code download

Conclusion

In this article, we learned MVC, using AngularJS & Crystal Report. If you have any queries, please tell me through the comments section.

Happy coding.

Advertisements