Skip to main content

Custom Export

  • 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:

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”):

@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”):

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

Member Table: Custom Export

Member Description
ASPxGridExporterBase.WriteCsv Exports the grid’s data to a stream in CSV format.
ASPxGridExporterBase.WritePdf Exports the grid’s data to a stream in PDF format.
ASPxGridExporterBase.WriteRtf Exports the grid’s data to a stream in RTF format.
ASPxGridExporterBase.WriteXls Exports the grid’s data to a stream in XLS format.
ASPxGridExporterBase.WriteXlsx Exports the grid’s data to a stream in XLSX format.
ASPxGridExporterBase.WriteDocx Exports the grid’s data to a stream in DOCX format.
CardViewExtension.ExportToCsv Exports the grid’s data to CSV format.
CardViewExtension.ExportToDocx Exports the grid’s data to DOCX format.
CardViewExtension.ExportToPdf Exports the grid’s data to PDF format.
CardViewExtension.ExportToRtf Exports the grid’s data to RTF format.
CardViewExtension.ExportToXls Exports the grid’s data to XLS format.
CardViewExtension.ExportToXlsx Exports the grid’s data to XLSX format.