Back to Devexpress

MapControl.ExportToXls(String) Method

wpf-devexpress-dot-xpf-dot-map-dot-mapcontrol-dot-exporttoxls-x28-system-dot-string-x29.md

latest10.1 KB
Original Source

MapControl.ExportToXls(String) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Exports the map data to the specified XLS file.

Namespace : DevExpress.Xpf.Map

Assembly : DevExpress.Xpf.Map.v25.2.dll

NuGet Package : DevExpress.Wpf.Map

Declaration

csharp
public void ExportToXls(
    string filePath
)
vb
Public Sub ExportToXls(
    filePath As String
)

Parameters

NameTypeDescription
filePathString

A string value, specifying the file path.

|

Example

To export a map image to a file, use one of the following methods.

csharp
using System;
using System.Windows;
using DevExpress.XtraPrinting;

namespace Exporting {
    public partial class MainWindow : Window {
        const string filename = "exportedMap";

        public MainWindow() {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            ExportFormat format = (ExportFormat)cbExportFormat.SelectedItem;
            string filepath;
            if (format == ExportFormat.Image)
                filepath = String.Format("{0}.jpg", filename);
            else
                filepath = String.Format("{0}.{1}", filename, format);
            bool isExported = true;
            switch (format) { 
                case (ExportFormat.Htm):
                    mapControl.ExportToHtml(filepath);
                    break;
                case (ExportFormat.Image):
                    mapControl.ExportToImage(filepath);
                    break;
                case (ExportFormat.Mht): 
                    mapControl.ExportToMht(filepath);
                    break;
                case (ExportFormat.Pdf): 
                    mapControl.ExportToPdf(filepath);
                    break;
                case (ExportFormat.Rtf): 
                    mapControl.ExportToRtf(filepath);
                    break;
                case (ExportFormat.Xls): 
                    mapControl.ExportToXls(filepath);
                    break;
                case (ExportFormat.Xlsx): 
                    mapControl.ExportToXlsx(filepath);
                    break;
                case (ExportFormat.Xps): 
                    mapControl.ExportToXps(filepath);
                    break;
                default: 
                    isExported = false;
                    break;
            }
            if (isExported) 
                MessageBox.Show(String.Format("Map exported successfully."));
            else
                MessageBox.Show(String.Format("Map exporting does not support the {0} file format.", format)); 
        }
    }
}
xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" 
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
        xmlns:dxp="clr-namespace:DevExpress.XtraPrinting;assembly=DevExpress.Printing.v14.2.Core"
        x:Class="Exporting.MainWindow"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <ObjectDataProvider x:Key="ExportFormatValues" 
                            MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="dxp:ExportFormat"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="MapPrintSizeModeValues" 
                            MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="dxm:MapPrintSizeMode"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <Grid>
        <dxlc:LayoutControl Orientation="Vertical">
            <dxlc:LayoutItem VerticalAlignment="Stretch">
                <dxm:MapControl x:Name="mapControl">
                    <dxm:MapControl.PrintOptions>
                        <dxm:MapPrintOptions SizeMode="{Binding SelectedItem, ElementName=cbPrintSizeMode}"/>
                    </dxm:MapControl.PrintOptions>
                    <dxm:ImageTilesLayer>
                        <dxm:ImageTilesLayer.DataProvider>
                            <dxm:BingMapDataProvider BingKey="Your Bing Key"/>
                        </dxm:ImageTilesLayer.DataProvider>
                    </dxm:ImageTilesLayer>
                </dxm:MapControl>
            </dxlc:LayoutItem>
            <dxlc:LayoutGroup>
                <dxlc:LayoutItem Label="Print Size Mode:">
                    <ComboBox x:Name="cbPrintSizeMode"
                              ItemsSource="{Binding Source={StaticResource MapPrintSizeModeValues}}"
                              SelectedIndex="0"/>
                </dxlc:LayoutItem>
                <dxlc:LayoutItem Label="Export to" AddColonToLabel="True">
                    <ComboBox x:Name="cbExportFormat"  
                        ItemsSource="{Binding Source={StaticResource ExportFormatValues}}"
                        SelectedIndex="0"/>
                </dxlc:LayoutItem>
                <dxlc:LayoutItem Width="100">
                    <Button Content="Export" Click="Button_Click"/>
                </dxlc:LayoutItem>
            </dxlc:LayoutGroup>
        </dxlc:LayoutControl>
    </Grid>
</Window>
vb
Imports System
Imports System.Windows
Imports DevExpress.XtraPrinting

Namespace Exporting
    Partial Public Class MainWindow
        Inherits Window

        Private Const filename As String = "exportedMap"

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim format As ExportFormat = CType(cbExportFormat.SelectedItem, ExportFormat)
            Dim filepath As String
            If format = ExportFormat.Image Then
                filepath = String.Format("{0}.jpg", filename)
            Else
                filepath = String.Format("{0}.{1}", filename, format)
            End If
            Dim isExported As Boolean = True
            Select Case format
                Case (ExportFormat.Htm)
                    mapControl.ExportToHtml(filepath)
                Case (ExportFormat.Image)
                    mapControl.ExportToImage(filepath)
                Case (ExportFormat.Mht)
                    mapControl.ExportToMht(filepath)
                Case (ExportFormat.Pdf)
                    mapControl.ExportToPdf(filepath)
                Case (ExportFormat.Rtf)
                    mapControl.ExportToRtf(filepath)
                Case (ExportFormat.Xls)
                    mapControl.ExportToXls(filepath)
                Case (ExportFormat.Xlsx)
                    mapControl.ExportToXlsx(filepath)
                Case (ExportFormat.Xps)
                    mapControl.ExportToXps(filepath)
                Case Else
                    isExported = False
            End Select
            If isExported Then
                MessageBox.Show(String.Format("Map exported successfully."))
            Else
                MessageBox.Show(String.Format("Map exporting does not support the {0} file format.", format))
            End If
        End Sub
    End Class
End Namespace
vb
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows

Namespace Exporting
    ''' <summary>
    ''' Interaction logic for App.xaml
    ''' </summary>
    Partial Public Class App
        Inherits Application

    End Class
End Namespace
xaml
<Application x:Class="Exporting.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

See Also

MapControl Class

MapControl Members

DevExpress.Xpf.Map Namespace