wpf-devexpress-dot-xpf-dot-grid-dot-tableview-dot-exporttocsv-x28-system-dot-string-x29.md
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 a grid to the specified file path in CSV format.
Namespace : DevExpress.Xpf.Grid
Assembly : DevExpress.Xpf.Grid.v25.2.dll
NuGet Package : DevExpress.Wpf.Grid.Core
public override void ExportToCsv(
string filePath
)
Public Overrides Sub ExportToCsv(
filePath As String
)
| Name | Type | Description |
|---|---|---|
| filePath | String |
A String which specifies the file name (including the full path) for the created CSV file.
|
To display the Print Preview of the grid, use the DataViewBase.ShowPrintPreview and DataViewBase.ShowPrintPreviewDialog methods. To print the grid, use the DataViewBase.Print or DataViewBase.PrintDirect method.
To learn more, see Printing.
Note
The grid can be previewed, printed and exported only if the DXPrinting Library is available. You should manually add the reference to the DevExpress.Xpf.Printing.v25.2 assembly.
This example shows how to preview, print, and export the GridControl‘s content.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView x:Name="view" AutoWidth="True"/>
</dxg:GridControl.View>
</dxg:GridControl>
<WrapPanel Grid.Row="1" Orientation="Horizontal">
<Button Click="PreviewGrid" Content="Show Print Preview"/>
<Button Click="ExportToXls" Content="Export to Excel"/>
<Button Click="ExportToCsv" Content="Export to CSV"/>
<Button Click="ExportToPng" Content="Export to an image"/>
</WrapPanel>
</Grid>
void PreviewGrid(object sender, RoutedEventArgs e) {
view.ShowPrintPreviewDialog(this);
}
void ExportToXls(object sender, RoutedEventArgs e) {
view.ExportToXlsx(@"d:\grid_export.xlsx");
}
void ExportToCsv(object sender, RoutedEventArgs e) {
view.ExportToCsv(@"d:\grid_export.csv");
}
void ExportToPng(object sender, RoutedEventArgs e) {
view.ExportToImage(@"d:\grid_export.png");
}
Private Sub PreviewGrid(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.view.ShowPrintPreviewDialog(Me)
End Sub
Private Sub ExportToXls(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.view.ExportToXlsx("d:\grid_export.xlsx")
End Sub
Private Sub ExportToCsv(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.view.ExportToCsv("d:\grid_export.csv")
End Sub
Private Sub ExportToPng(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.view.ExportToImage("d:\grid_export.png")
End Sub
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ExportToCsv(String) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
wpf-data-grid-print-and-export-data/CS/Window1.xaml.cs#L16
void ExportToCsv(object sender, RoutedEventArgs e) {
view.ExportToCsv(@"d:\grid_export.csv");
}
wpf-data-grid-print-and-export-data/VB/Window1.xaml.vb#L22
Private Sub ExportToCsv(ByVal sender As Object, ByVal e As RoutedEventArgs)
Me.view.ExportToCsv("d:\grid_export.csv")
End Sub
See Also