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.
After completing the installation, restart Visual Studio.
Create MVC Project
Open Visual Studio 2015.
Go to Menu >New > Click Project > it will open New project popup.
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.
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.
In this Report folder, add one Dataset for Server side Data binding and it will be used to design the customizable reports.
Use this DataSet to export for design & developing the report.
Easy way to drag & drop the controls are used in the design, as shown.
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
- public ActionResult ExportExcel()
- {
- List<BookModel> BookList = new List<BookModel>();
- DataSetReport DsReport = new DataSetReport();
- using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbSqlCon"].ConnectionString))
- {
- var cmd = new SqlCommand("BookMaster_SP", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.Add(new SqlParameter("@Mode", SqlDbType.VarChar)).Value = "GET";
- con.Open();
-
- (new SqlDataAdapter(cmd)).Fill(DsReport.Tables["BookList"]);
- }
-
- ReportDocument rd = new ReportDocument();
- rd.Load(Path.Combine(Server.MapPath("~/Report"), "ReportBookList.rpt"));
- rd.SetDataSource(DsReport.Tables["BookList"]);
-
- Response.Buffer = false;
- Response.ClearContent();
- Response.ClearHeaders();
-
-
- Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.ExcelWorkbook);
- stream.Seek(0, SeekOrigin.Begin);
- return File(stream, "application/pdf", "ReportBookList.xlsx");
- }
Proceed, as shown below.
- rd.Load(Path.Combine(Server.MapPath("~/Report"), "ReportBookList.rpt"));
In this article, I have demonstrated only EXCEL & PDF reports.
- Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.ExcelWorkbook);
You can have 16 types of reports format export, using Crystal Reports.
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
- $scope.ExcelReport=function()
- {
- $window.open("Home/ExportExcel", "_blank");
- }
- $scope.PdfReport = function () {
- $window.open("Home/ExportPdf", "_blank");
- }
Before writing this code, you must inject the $Window keyword in Angular Controller.
- uiroute.controller('BookController', function ($scope, BookService, $window)
Call the above JS function in HTML buttons.
- <button class="btn btn-success " ng-click="ExcelReport()">Excel Report</button>
- <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
Output 2
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.