Back to Devexpress

Custom Export

aspnetmvc-115984-components-card-view-export-custom-export.md

latest5.4 KB
Original Source

Custom Export

  • Jun 12, 2023
  • 3 minutes to read

Tip

We recommend using the client API and the built-in toolbar commands for ASPxCardView data exporting.

The ASP.NET MVC CardView extension allows users to export the MVCxCardView records to CSV, PDF, RTF, XLS and XLSX format using the methods listed in the Member Table: Custom Export topic.

Declare the master grid settings (a CardViewSettings type object) and an object the grid is bound to. We recommended declaring grid settings in the Controller code (or any other helper code) and passing them to the View code to avoid repeatedly declaring and initializing grid settings.

Example

The code sample below demonstrates how to implement the MVCxCardView data exporting.

Controller code:

csharp
using System.Linq;
using System.Web.Mvc;
using DevExpress.Web.Mvc;

namespace MyProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.GridSettings = GetGridSettings();
            return View();
        }
        // The Entity Framework data context
        MyProject.Models.northwndEntities db = new MyProject.Models.northwndEntities();

        // Handles CardView callbacks.
        [ValidateInput(false)]
        public ActionResult CardViewPartial()
        {
            ViewBag.GridSettings = GetGridSettings();
            var model = db.Products;
            return PartialView("_CardViewPartial", model.ToList());
        }

        // This action method sends a PDF document with the exported grid to response.
        public ActionResult ExportTo()
        {
            var model = db.Products;
            return CardViewExtension.ExportToPdf(GetGridSettings(), model.ToList());
        }

        // Returns the settings of the exported CardView.
        private CardViewSettings GetGridSettings() {
            var settings = new CardViewSettings();
            settings.Name = "CardView";
            settings.CallbackRouteValues = new { Controller = "Home", Action = "CardViewPartial" };

            // Export-specific settings 
            settings.SettingsExport.ExportSelectedCardsOnly = false;
            settings.SettingsExport.FileName = "Report.pdf";
            settings.SettingsExport.PaperKind = System.Drawing.Printing.PaperKind.A4;

            settings.KeyFieldName = "ProductID";
            settings.Columns.Add("ProductName");
            settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";
            settings.Columns.Add("QuantityPerUnit");
            settings.Columns.Add("Discontinued", MVCxCardViewColumnType.CheckBox);

            return settings;
        }
    }
}

View code(“Index”):

csharp
@Html.Action("CardViewPartial")

// When an end-user clicks this button, the button sends a callback to the "ExportTo" action and  
// the server sends the resulting export file to the response.
@Html.DevExpress().Button(settings =>
{
    settings.Name = "Button";
    settings.UseSubmitBehavior = false;
    settings.Text = "ExportTo PDF";
    settings.RouteValues = new { Controller = "Home", Action = "ExportTo" };
}).GetHtml()

Partial View code (“_CardViewPartial”):

csharp
@{
    var grid = Html.DevExpress().CardView(ViewBag.GridSettings);
}
@grid.Bind(Model).GetHtml()

Member Table: Custom Export

MemberDescription
ASPxGridExporterBase.WriteCsvExports the grid’s data to a stream in CSV format.
ASPxGridExporterBase.WritePdfExports the grid’s data to a stream in PDF format.
ASPxGridExporterBase.WriteRtfExports the grid’s data to a stream in RTF format.
ASPxGridExporterBase.WriteXlsExports the grid’s data to a stream in XLS format.
ASPxGridExporterBase.WriteXlsxExports the grid’s data to a stream in XLSX format.
ASPxGridExporterBase.WriteDocxExports the grid’s data to a stream in DOCX format.
CardViewExtension.ExportToCsvExports the grid’s data to CSV format.
CardViewExtension.ExportToDocxExports the grid’s data to DOCX format.
CardViewExtension.ExportToPdfExports the grid’s data to PDF format.
CardViewExtension.ExportToRtfExports the grid’s data to RTF format.
CardViewExtension.ExportToXlsExports the grid’s data to XLS format.
CardViewExtension.ExportToXlsxExports the grid’s data to XLSX format.