dashboard-119766-web-dashboard-integrate-dashboard-component-aspnet-mvc-dashboard-extension-manage-exporting-capabilities.md
The Web Dashboard allows users to export an entire dashboard or individual dashboard items.
The ASP.NET MVC Dashboard extension’s server-side API allows you to customize the default export options, customize export documents at runtime, etc.
DashboardExtensionSettings.AllowExportDashboardGets or sets whether end-users can export a dashboard.DashboardExtensionSettings.AllowExportDashboardItemsGets or sets whether end-users can export dashboard items.DashboardExtensionSettings.PdfExportOptionsProvides access to options related to exporting a dashboard/dashboard item to PDF format.DashboardExtensionSettings.ImageExportOptionsProvides access to options related to exporting a dashboard/dashboard item to an image.DashboardExtensionSettings.ExcelExportOptionsProvides access to options related to exporting a dashboard item to Excel format.DashboardConfigurator.BeforeExportDocumentAllows you to hide specific dashboard items when exporting the entire dashboard.DashboardConfigurator.CustomExportAllows you to customize the exported document.DashboardConfigurator.CustomizeExportDocumentAllows you to customize the stream containing the resulting document (such as PDF, Image, or Excel).
The WebDashboardExporter allows you to implement server export for the ASP.NET MVC Dashboard extension.
WebDashboardExporter.ExportDashboardItemToExcelExports the dashboard item to the specified stream in Excel format.WebDashboardExporter.ExportDashboardItemToImageExports the dashboard item to the specified stream in Image format.WebDashboardExporter.ExportDashboardItemToPdfExports the dashboard item to the specified stream in PDF format.WebDashboardExporter.ExportToExcelExports a dashboard to the specified stream in Excel format.WebDashboardExporter.ExportToImageExports a dashboard to the specified stream in Image format.WebDashboardExporter.ExportToPdfExports a dashboard to the specified stream in PDF format.
To use WebDashboardExporter API, pass the default DashboardConfigurator instance to the WebDashboardExporter constructor.
using DevExpress.DashboardWeb;
//...
WebDashboardExporter exporter = new WebDashboardExporter(DashboardConfigurator.Default);
Imports DevExpress.DashboardWeb
'...
Private exporter As New WebDashboardExporter(DashboardConfigurator.Default)
Then, use the required method exposed by WebDashboardExporter to export a dashboard or dashboard item (such as WebDashboardExporter.ExportToPdf or WebDashboardExporter.ExportDashboardItemToImage).
View Example: How to add custom information to the exported dashboard
You can use the non-visual DashboardExporter component to implement server-side export of a dashboard or dashboard items without referencing dashboard UI controls ( DashboardDesigner, DashboardViewer, ASPxDashboard, and so on) or DashboardConfigurator.
To integrate the DashboardExporter into a service, register the DevExpress NuGet feed as a package source and install the DevExpress.Dashboard.Core package. See the DashboardExporter class description for details.
View Example: Export Dashboards in a Console Application
View Example: How to Email a Dashboard
View Example: Email a Dashboard with Different Data Depending on the Addressee
The WebDashboardExporter class allows you to implement server export for the ASP.NET MVC Dashboard extension and ASP.NET Core Dashboard control. You can specify a dashboard state and export options to be applied to the resulting document. Refer to the WebDashboardExporter class description for more information.
This example demonstrates how to export a dashboard displayed using the ASP.NET MVC Dashboard extension on the server side using the WebDashboardExporter class. The following API is used to implement this capability:
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.IO;
using System.Web.Mvc;
namespace MvcDashboard_ServerExport.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult ExportDashboardToPdf(string DashboardID, string DashboardState) {
using (MemoryStream stream = new MemoryStream()) {
string dashboardID = DashboardID;
DashboardState dashboardState = new DashboardState();
dashboardState.LoadFromJson(DashboardState);
DashboardPdfExportOptions pdfOptions = new DashboardPdfExportOptions();
pdfOptions.ExportFilters = true;
pdfOptions.DashboardStatePosition = DashboardStateExportPosition.Below;
string dateTimeNow = DateTime.Now.ToString("yyyyMMddHHmmss");
string filePath = "~/App_Data/Export/" + dashboardID + "_" + dateTimeNow + ".pdf";
ASPxDashboardExporter exporter = new ASPxDashboardExporter(DashboardConfigurator.Default);
exporter.ExportToPdf(dashboardID, stream, new System.Drawing.Size(1024, 768), dashboardState, pdfOptions);
SaveFile(stream, filePath);
ContentResult result = new ContentResult();
result.Content = filePath;
return result;
}
}
private void SaveFile(MemoryStream stream, string path) {
var fileStream = System.IO.File.Create(Server.MapPath(path));
stream.WriteTo(fileStream);
fileStream.Close();
}
}
}
Imports System.IO
Imports System.Web.Mvc
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Namespace MvcDashboard_ServerExport.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
<HttpPost> _
Public Function ExportDashboardToPdf(ByVal DashboardID As String, ByVal DashboardState As String) As ActionResult
Using stream As New MemoryStream()
Dim dashboardID_Renamed As String = DashboardID
Dim dashboardState_Renamed As New DashboardState()
dashboardState_Renamed.LoadFromJson(DashboardState)
Dim pdfOptions As New DashboardPdfExportOptions()
pdfOptions.ExportFilters = True
pdfOptions.DashboardStatePosition = DashboardStateExportPosition.Below
Dim dateTimeNow As String = Date.Now.ToString("yyyyMMddHHmmss")
Dim filePath As String = "~/App_Data/Export/" & dashboardID_Renamed & "_" & dateTimeNow & ".pdf"
Dim exporter As New ASPxDashboardExporter(DashboardConfigurator.Default)
exporter.ExportToPdf(dashboardID_Renamed, stream, New System.Drawing.Size(1024, 768), dashboardState_Renamed, pdfOptions)
SaveFile(stream, filePath)
Dim result As New ContentResult()
result.Content = filePath
Return result
End Using
End Function
Private Sub SaveFile(ByVal stream As MemoryStream, ByVal path As String)
Dim fileStream = System.IO.File.Create(Server.MapPath(path))
stream.WriteTo(fileStream)
fileStream.Close()
End Sub
End Class
End Namespace
function onBeforeRender(sender) {
var control = sender.getDashboardControl();
control.registerExtension(new DevExpress.Dashboard.DashboardPanelExtension(control));
$("#buttonContainer").dxButton({
text: "Export to PDF",
onClick: function (param) {
var dashboardID = control.dashboardContainer().id;
var dashboardStateJson = control.dashboard().state();
$.ajax({
url: 'Home/ExportDashboardToPdf',
data: {
DashboardID: dashboardID,
DashboardState: JSON.stringify(dashboardStateJson)
},
type: 'POST',
}).success(function (result) {
DevExpress.ui.notify('A dashboard was exported to ' + result, 'success', 5000);
});
}
});
}
You can use the Web Dashboard’s client-side API for exporting to various formats, customizing export options, etc. For this, use the members of the DashboardExportExtension:
exportToPdfExports the entire dashboard to a PDF file.exportToImageExports the entire dashboard to an image.exportToExcelExports the entire dashboard to an Excel file.exportDashboardItemToPdf(itemName)Exports a dashboard item to a PDF file.exportDashboardItemToImage(itemName)Exports a dashboard item to an image.exportDashboardItemToExcel(itemName)Exports a dashboard item to an Excel file.getPdfExportOptionsReturns options related to exporting a dashboard/dashboard item to PDF format.getImageExportOptionsReturns options related to exporting a dashboard/dashboard item to the Image format.getExcelExportOptionsReturns options related to exporting a dashboard/dashboard item to Excel format.setPdfExportOptions(options)Sets options related to exporting a dashboard/dashboard item to PDF format.setImageExportOptions(options)Sets options related to exporting a dashboard/dashboard item to the Image format.setExcelExportOptions(options)Sets options related to exporting a dashboard/dashboard item to Excel format.showExportDashboardDialog(format)Invokes the dialog that allows end-users to export the entire dashboard to the specified format.showExportDashboardItemDialog(itemComponentName, format)Invokes the dialog that allows end users to export the dashboard item to the specified format.
The Dashboard Control raises the ASPxDashboard.CustomExport/DashboardConfigurator.CustomExport event before saving the exported document to the PDF and Image formats. The event allows you to customize the exported document.
The following table illustrates dashboard items and their corresponding printable XRControls:
The following example shows how to customize dashboard items in the exported document when you handle the ASPxDashboard.CustomExport / DashboardConfigurator.CustomExport events. You can use the CustomExportWebEventArgs.GetPrintableControls method to obtain the printable controls.
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.XtraCharts;
using DevExpress.XtraReports.UI;
using System;
using System.Drawing;
using System.Linq;
protected void CustomizeExport(object sender, CustomExportWebEventArgs e) {
foreach(var printControl in e.GetPrintableControls()) {
if(printControl.Value is XRGaugeDashboardItem) {
var gaugeItemName = printControl.Key;
var gaugeDashboardItem = e.GetDashboardItem(gaugeItemName) as GaugeDashboardItem;
foreach(var dashGaugeElement in gaugeDashboardItem.Gauges) {
foreach(var gaugePanel in e.GetGaugeContext(gaugeItemName).GetPrintableGauges(dashGaugeElement).Cast<XRDashboardGauge>()) {
if(gaugePanel != null) {
gaugePanel.MainSeriesLabel.ForeColor = Color.Red;
}
}
}
}
if(printControl.Value is XRChart) {
var chartItemName = printControl.Key;
var chartDashboardItem = e.GetDashboardItem(chartItemName) as ChartDashboardItem;
foreach(var pane in chartDashboardItem.Panes) {
if(pane.Series.Count > 0) {
foreach(var dashSeries in pane.Series) {
if(dashSeries != null) {
var controlSeries = e.GetChartContext(chartItemName).GetControlSeries(dashSeries);
if(controlSeries != null) {
foreach(var ser in controlSeries) {
LineSeriesView view = ser.View as LineSeriesView;
if(view != null) {
view.LineStyle.DashStyle = DashStyle.DashDot;
}
}
}
}
}
}
}
}
}
}
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports DevExpress.XtraCharts
Imports DevExpress.XtraReports.UI
Imports System
Imports System.Drawing
Imports System.Linq
Protected Sub ASPxDashboard1_CustomExport(ByVal sender As Object, ByVal e As CustomExportWebEventArgs)
For Each printControl In e.GetPrintableControls()
If TypeOf printControl.Value Is XRGaugeDashboardItem Then
Dim gaugeItemName = printControl.Key
Dim gaugeDashboardItem = TryCast(e.GetDashboardItem(gaugeItemName), GaugeDashboardItem)
For Each dashGaugeElement In gaugeDashboardItem.Gauges
For Each gaugePanel In e.GetGaugeContext(gaugeItemName).GetPrintableGauges(dashGaugeElement).Cast(Of XRDashboardGauge)()
If gaugePanel IsNot Nothing Then
gaugePanel.MainSeriesLabel.ForeColor = Color.Red
End If
Next gaugePanel
Next dashGaugeElement
End If
If TypeOf printControl.Value Is XRChart Then
Dim chartItemName = printControl.Key
Dim chartDashboardItem = TryCast(e.GetDashboardItem(chartItemName), ChartDashboardItem)
For Each pane In chartDashboardItem.Panes
If pane.Series.Count > 0 Then
For Each dashSeries In pane.Series
If dashSeries IsNot Nothing Then
Dim controlSeries = e.GetChartContext(chartItemName).GetControlSeries(dashSeries)
If controlSeries IsNot Nothing Then
For Each ser In controlSeries
Dim view As LineSeriesView = TryCast(ser.View, LineSeriesView)
If view IsNot Nothing Then
view.LineStyle.DashStyle = DashStyle.DashDot
End If
Next ser
End If
End If
Next dashSeries
End If
Next pane
End If
Next printControl
End Sub
The following example shows how to specify header and footer content of an exported dashboard. For this, the CustomExport event is used.
View Example: ASP.NET Web Forms
The following example shows how to add a custom header to each sheet for the exported Excel workbook. For this, the CustomizeExportDocument event is used.
View Example: ASP.NET Web Forms
The following example shows how to export a dashboard with different dashboard states (different master filter value) to separate pages and join them to a single PDF document. Multiple exported documents are joined in a single file with the help of the PdfDocumentProcessor class.
View Example: ASP.NET Web Forms
This example shows how to use the DashboardExporter component in a console application to export a dashboard with a custom Funnel item.
See Also