Saturday 3 August 2019

ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV

In this article I will explain with an example, how to export Crystal Report and download in Word, Excel, PDF and CSV file formats in ASP.Net MVC Razor.
Entity Framework will be used to populate Crystal Reports from SQL Server database in ASP.Net MVC 5 Razor.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Creating an Entity Data Model
The very first step is to create an ASP.Net MVC Application and connect it to the Northwind Database using Entity Framework. For more details please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example.
Following is the Entity Data Model of the Customers Table of the Northwind Database which will be used later in this project.
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV
 
 
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Top 5 Customer records are fetched and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(from customer in entities.Customers.Take(5)
                    select customer);
    }
}
 
 
Configuring Crystal Report in ASP.Net MVC
The Crystal Report configuration has been covered in the following article Implementing Crystal Reports in ASP.Net MVC.
 
 
HTML Markup
The HTML Markup consists of a CrystalReportViewer control, a RadioButtonList with the Export file formats and a Button to export Crystal Reports to Word, Excel, PDF and CSV file formats.
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src='<%=ResolveUrl("~/crystalreportviewers13/js/crviewer/crv.js")%>' type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
        Height="400" Width="600" BestFitPage="False" ToolPanelView="None"/>
        Format:
    <asp:RadioButtonList ID="rbFormat" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Text="Word" Value="Word" Selected="True"/>
        <asp:ListItem Text="Excel" Value="Excel"/>
        <asp:ListItem Text="PDF" Value="PDF"/>
        <asp:ListItem Text="CSV" Value="CSV"/>
    </asp:RadioButtonList>
    <br/><br/>
    <asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export"/>
    </form>
</body>
</html>
 
 
Namespaces
You will need to import the following namespace.
using CrystalDecisions.Shared;
 
 
Populating the Crystal Report using Entity Framework
Inside the Page Load event, the Crystal Report is populated with the records of the database using Entity Framework.
CustomerReport crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
    CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
    crystalReport = new CustomerReport();
    NorthwindEntities entities = new NorthwindEntities();
    crystalReport.SetDataSource(from customer in entities.Customers.Take(5)
                                select customer);
    CrystalReportViewer1.ReportSource = crystalReport;
    CrystalReportViewer1.RefreshReport();
}
 
 
Exporting Crystal Report and download Word, Excel, PDF and CSV file formats in ASP.Net
When the Export Button is clicked, based on the selection from the RadioButtonList the ExportFormatType for the Crystal Report is set.
Then based on the ExportFormatType the Crystal Report is exported to the desired format using the ExportToHttpResponse method.
The Crystal Report exported document i.e. Word, Excel, PDF or CSV will be downloaded as Attachment in Browser.
protected void Export(object sender, EventArgs e)
{
    ExportFormatType formatType = ExportFormatType.NoFormat;
    switch (rbFormat.SelectedItem.Value)
    {
        case "Word":
            formatType = ExportFormatType.WordForWindows;
            break;
        case "PDF":
            formatType = ExportFormatType.PortableDocFormat;
            break;
        case "Excel":
            formatType = ExportFormatType.Excel;
            break;
        case "CSV":
            formatType = ExportFormatType.CharacterSeparatedValues;
            break;
    }
 
    crystalReport.ExportToHttpResponse(formatType, Response, true"Crystal");
    Response.End();
}
 
 
Screenshots
The Crystal Report
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV
 
Crystal Report exported to Word Document
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV
 
Crystal Report exported to Excel Spreadsheet
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV
 
Crystal Report exported to PDF file
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV
 
Crystal Report exported to CSV file
ASP.Net MVC: Export Crystal Report to Word Excel PDF and CSV

No comments:

Post a Comment