Dev Express WPF GridControl 数据导出到Excel
Dev Express WPF 给控件提供了公共的导出方法:
- Export to PDF
- Export to HTML
- Export to MHT
- Export to Text
- Export to CSV
- Export to XLS
- Export to XLSX
- Export to RTF
- Export to DOCX
- Export to Image
以导出数据到Excel表格为例:
导出模式分为两种类型: Data-Aware Export和 WYSIWYG Export
其中 WYSIWYG Export 模式支持导出结果保持GridControl设置的 PrintStyle,但在Excel中显示有写问题,同时性能也稍差一些,可能这是Dev 升级导出引擎的部分原因吧。
Data-Aware Export 模型导出数据则不支持PrintStyle,如果需要设置表格数据的展示样式,可以实现 CustomizeCell 事件;
测试代码:

<Window x:Class="GridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:local="clr-namespace:GridExample"
Width="600" Height="350"
> <Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/GridExample;component/Themes/PrintCellStylesWPF.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources> <Grid VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> <dxg:GridControl Name="grid" AutoGenerateColumns="AddNew">
<dxg:GridControl.Columns> <dxg:GridColumn FieldName="PlainText"/> <dxg:GridColumn FieldName="MemoText"
PrintCellStyle="{StaticResource MemoColumnPrintingStyle}"
>
<dxg:GridColumn.EditSettings>
<dxe:MemoEditSettings/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn> <dxg:GridColumn FieldName="BooleanMember"
PrintCellStyle="{StaticResource CheckEditColumnPrintingStyle}"
>
<dxg:GridColumn.EditSettings>
<dxe:CheckEditSettings/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn> <dxg:GridColumn FieldName="Image"
PrintCellStyle="{StaticResource ImageColumnPrintingStyle}"
>
<dxg:GridColumn.EditSettings>
<dxe:PopupImageEditSettings/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View>
<dxg:TableView Name="view" PrintColumnHeaderStyle="{StaticResource HeaderStyle}"/>
</dxg:GridControl.View>
</dxg:GridControl> <Button Grid.Row="1" Width="150" Name="PrintButton" Click="PrintButton_Click" Content="Show print preview"/>
</Grid>
</Window>

// Developer Express Code Central Example:
// How to use the PrintCellStyle property to customize cell's printing appearance
//
// This example shows how to create custom PrintCellStyle for grid columns to bring
// a custom printing appearance for PopupImageEdit, CheckBoxEdit and MemoEdit.
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3227 using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Windows.Resources;
using System.IO;
using System.Drawing;
using System.Windows.Data;
using DevExpress.XtraPrinting;
using DevExpress.Export; namespace GridExample {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded);
} void MainWindow_Loaded(object sender, RoutedEventArgs e) {
grid.ItemsSource = new List<TestData>() {
new TestData() { PlainText = "LMA AG", MemoText = "Mercedes-Benz SLK \n 2004 \n Silver", BooleanMember = true, Image = GetImage("/Images/1.png") },
new TestData() { PlainText = "Western Motors", MemoText ="Rolls-Royce Corniche \n 1975 \n Snowy whight", BooleanMember = false, Image = GetImage("/Images/2.png") },
new TestData() { PlainText = "Sun car rent", MemoText = "Ford Ranger FX-4\n 1999 \n Red rock", BooleanMember = true, Image = GetImage("/Images/3.png") }
};
} ImageSource GetImage(string path) {
return new BitmapImage(new Uri(path, UriKind.Relative));
} private void PrintButton_Click(object sender, RoutedEventArgs e) {
//view.ExportToCsv(@"C:\Users\lenovo\Desktop\testExport\aa.csv");
view.ExportToXlsx(@"C:\Users\lenovo\Desktop\testExport\aa.xlsx", new XlsxExportOptionsEx
{ ExportType = ExportType.WYSIWYG
});
XlsxExportOptionsEx options = new XlsxExportOptionsEx();
options.CustomizeCell += Options_CustomizeCell;
view.ExportToXlsx(@"C:\Users\lenovo\Desktop\testExport\aa11.xlsx", options);
view.ShowPrintPreview(this);
} private void Options_CustomizeCell(CustomizeCellEventArgs e)
{
if(e.AreaType == SheetAreaType.Header)
{
e.Formatting.Font = new DevExpress.Export.XlCellFont { Name= "微软雅黑", Bold = true, Size = 20 };
e.Formatting.BackColor = System.Drawing.Color.Pink;
e.Handled = true;
}
}
}
}
PrintStyle 设置

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing"
xmlns:dxgt="clr-namespace:DevExpress.Xpf.Grid.Themes;assembly=DevExpress.Xpf.Grid.v20.1"
>
<Style x:Key="HeaderStyle"
TargetType="{x:Type dxe:TextEdit}"
BasedOn="{StaticResource {dxgt:TableViewThemeKey ResourceKey=DefaultPrintCellStyle}}">
<Setter Property="dxp:ExportSettings.TargetType" Value="Text" />
<Setter Property="FontSize" Value="11pt" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="Background" Value="#177477" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontWeight" Value="Bold" />
</Style> <Style x:Key="ImageColumnPrintingStyle"
TargetType="{x:Type dxe:PopupImageEdit}"
BasedOn="{StaticResource {dxgt:TableViewThemeKey ResourceKey=DefaultPrintCellStyle}}">
<Setter Property="dxp:ExportSettings.TargetType" Value="Panel" />
<Setter Property="DisplayTemplate">
<Setter.Value>
<ControlTemplate TargetType="dxe:PopupImageEdit"> <dxe:ImageEdit Source="{Binding Path=Value}"
IsPrintingMode="True"
Margin="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/> </ControlTemplate>
</Setter.Value>
</Setter>
</Style> <dx:BoolToObjectConverter x:Key="BoolToTextConverter" TrueValue="Avaliable" FalseValue="NotAvaliable" /> <Style x:Key="CheckEditColumnPrintingStyle"
TargetType="dxe:CheckEdit"
BasedOn="{StaticResource {dxgt:TableViewThemeKey ResourceKey=DefaultPrintCellStyle}}">
<Style.Setters>
<Setter Property="dxp:ExportSettings.TargetType" Value="Panel" />
<Setter Property="DisplayTemplate">
<Setter.Value>
<ControlTemplate TargetType="dxe:CheckEdit">
<dxe:TextEdit Text="{Binding Path=Value, Converter={StaticResource BoolToTextConverter}}" HorizontalAlignment="Center" Margin="4"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style> <Style x:Key="MemoColumnPrintingStyle"
TargetType="dxe:MemoEdit"
BasedOn="{StaticResource {dxgt:TableViewThemeKey ResourceKey=DefaultPrintCellStyle}}">
<Style.Setters>
<Setter Property="dxp:ExportSettings.TargetType" Value="Panel" />
<Setter Property="DisplayTemplate">
<Setter.Value>
<ControlTemplate TargetType="dxe:MemoEdit">
<dxe:TextEdit Text="{Binding Value}"
TextWrapping="Wrap"
IsPrintingMode="True"
Margin="4"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ResourceDictionary>

public class TestData {
public string PlainText { get; set; }
public string MemoText { get; set; }
public bool BooleanMember { get; set; }
public ImageSource Image { get; set; }
}
Dev Express WPF GridControl 数据导出到Excel的更多相关文章
- WinForm------如何将GridControl数据导出到Excel
转载: http://www.cnblogs.com/xiaofengfeng/archive/2011/11/22/2258906.html 代码: SaveFileDialog saveFileD ...
- Dev GridControl数据导出格式问题
环境:DevExpress9.3,Vs2008 DevExpress的GridControl提供方便的数据导出到Excel功能,导出中用户可以根据GridControl的格式进行导出(ExportTo ...
- WPF-将DataGrid控件中的数据导出到Excel
原文:WPF-将DataGrid控件中的数据导出到Excel 导出至Excel是非常常见,我们可以用很多类库,例如Aspose.NOPI.Interop,在这里我们使用微软自家的工具.我的WPF绑定的 ...
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
- 将C1Chart数据导出到Excel
大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...
- vb.net-三种将datagridview数据导出为excel文件的函数
第一种方法较慢,但是数据格式都比较好,需要引用excel的 Microsoft.Office.Interop.Excel.dll office.dll #Region "导出excel函数 ...
- 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- 数据导出到Excel中
自己修改后的一个数据导出到Excel的方法,粘出来与大家共享. 只需要将 System.Web.HttpContext.Current.Response.Charset = ...
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
随机推荐
- 关于ConditionalOnClass注解
1. pom文件<optional>标签 在Java开发中,大家肯定在pom文件中添加过依赖(现在没有,以后也肯定会有的),不知道大家对<optional>标签的了解有多少,或 ...
- uniapp h5 和 小程序互相传值
小程序端 <template> <div> <web-view :webview-styles="webviewStyles" :src=" ...
- 如何给自己的网站添加live2D女仆
<script type="text/javascript" charset="utf-8" src="https://files.cnblo ...
- 如何实现LLM的通用function-calling能力?
众所周知,LLM的函数function-calling能力很强悍,解决了大模型与实际业务系统的交互问题.其本质就是函数调用. 从openai官网摘图: 简而言之: LLM起到决策的作用,告知业务系统应 ...
- 硬件开发笔记(三十二):TPS54331电源设计(五):原理图BOM表导出、元器件封装核对
前言 一个12V转5V.3.3V和4V的电源电路设计好了,下一步导出BOM表,二次核对元器件型号封装,这是可以生产前的最后一步了. 导出BOM表 步骤一:打开原理图 打开项目,双击点开原理 ...
- UML之图框架标题类型之谬
在UML中,我们可以用一个被称为"框架"的边界框围绕着UML图形,当然在很多情况下,框架可以省略,也就是不将它描画出来.但是对于某些图形类型而言,框架具有语义意义,在这些图形类型中 ...
- Datawhale AI 夏令营-天池Better Synth多模态大模型数据合成挑战赛-task2探索与进阶(更新中)
在大数据.大模型时代,随着大模型发展,互联网数据渐尽且需大量处理标注,为新模型训练高效合成优质数据成为新兴问题."天池 Better Synth - 多模态大模型数据合成挑战赛"应 ...
- 特斯拉CEO埃隆马.斯克的五步工作法,怎么提高工程效率加速产品开发?
简介 在<埃隆·马斯克传>这本书中,有两个章节写到了特斯拉 CEO 埃隆马斯克为了在一段时间内,提升特斯拉汽车 model 3 的产能到每个月 5000 辆这个数量级,在书中叫 " ...
- spring事务配置的5种方式
事务配置其实把思路理清,还是很容易的. spring的配置文件中关于事务配置总是由三个部分组成:分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一 ...
- 一款基于 .NET8 + Vue 开源、免费、跨平台的企业级在线考试系统
前言 今天大姚给大家分享一款基于 .NET8 + Vue 开源.免费(AGPL-3.0开源协议).跨平台的企业级在线考试系统:XBLMS. 项目介绍 XBLMS是一款基于 .NET8 + Vue 开源 ...