本文主要是介绍WPF及Silverlight中将DataGrid数据导出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这段源码是我在项目中实际应用的源码,没有经过删减及处理。 如果你认为有用可以摘去作为自己的导出类中的一个小工具使用。 /// summary /// 数据源导出辅助类 /// /summary /// remarks /// Author:sucsy /// Createdate:2011-6-17 /// Description:提供基于数据源(公共枚举器类型)的数据导出辅助功能 /// /remarks publi
这段源码是我在项目中实际应用的源码,没有经过删减及处理。
如果你认为有用可以摘去作为自己的导出类中的一个小工具使用。
/// <summary>
/// 数据源导出辅助类
/// </summary>
/// <remarks>
/// Author: sucsy
/// Create date: 2011-6-17
/// Description: 提供基于数据源(公共枚举器类型)的数据导出辅助功能
/// </remarks>
public class DataSourceExportHelper
{
/// <summary>
/// 导出到文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="splitString"> 分隔符 </param>
/// <param name="columns"> 导出的列(为null时表示全部导出) </param>
/// <returns> 文本数据 </returns>
public static string Export2Text(IEnumerable dataSource, string splitString,DataGridColumn[] columns, bool isVisiableColumn)
{
// 定义结果存放对象
StringBuilder sbResult = new StringBuilder();
string[] columnHeaders = GetSelectedColumns(columns);
// 处理DataTable作为数据源的数据导出操作
if (dataSource is System.Data.DataView)
{
System.Data.DataView dataView = (System.Data.DataView)dataSource;
// 加载数据标题
foreach (DataColumn objHeaderpi in dataView.Table.Columns)
{
if (columns == null || columnHeaders.Contains(objHeaderpi.ColumnName))
sbResult.Append(objHeaderpi.ColumnName + splitString);
}
sbResult.AppendLine();
foreach (System.Data.DataRowView dr in dataView)
{
foreach (DataColumn objHeaderpi in dataView.Table.Columns)
{
if (columns == null || columnHeaders.Contains(objHeaderpi.ColumnName))
sbResult.Append(dr[objHeaderpi.ColumnName].ToString() + splitString);
}
sbResult.AppendLine();
}
}
else
{
foreach (DataGridColumn dgcol in columns)
sbResult.Append(dgcol.Header.ToString() + splitString);
sbResult.AppendLine();
foreach ( object data in dataSource)
{
foreach (DataGridColumn col in columns)
{
string strValue = "";
Binding objBinding = null;
if (col is DataGridBoundColumn)
objBinding = (Binding)(col as DataGridBoundColumn).Binding;
if (col is DataGridTemplateColumn)
{
DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
FrameworkElement oFE = (FrameworkElement)objDO;
FieldInfo oFI = oFE.GetType().GetField( " TextProperty ");
if (oFI != null)
{
if (oFI.GetValue( null) != null)
{
if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue( null)) != null)
objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue( null)).ParentBinding;
}
}
}
if (objBinding != null)
{
if (objBinding.Path.Path != "")
{
string[] dataTree = objBinding.Path.Path.Split( " . ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
object parentData = data;
for ( int treeIndex = 0; treeIndex < dataTree.Length; treeIndex++)
{
PropertyInfo pData = parentData.GetType().GetProperty(dataTree[treeIndex]);
parentData = pData.GetValue(parentData, null);
}
if (parentData != null) strValue = parentData.ToString();
}
if (objBinding.Converter != null)
{
if (strValue != "")
strValue = objBinding.Converter.Convert(strValue, typeof( string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
else
strValue = objBinding.Converter.Convert(data, typeof( string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
}
}
sbResult.Append(strValue.Replace( ' , ', ' , ') + splitString);
}
sbResult.AppendLine();
}
}
return sbResult.ToString();
}
public static string[] GetSelectedColumns(DataGridColumn[] columns)
{
List< string> columnsHeader = new List< string>();
foreach (DataGridColumn column in columns)
{
if (column.Visibility == System.Windows.Visibility.Visible)
columnsHeader.Add(column.Header.ToString());
}
return columnsHeader.ToArray();
}
/// <summary>
/// 导出为平面文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="columns"> 选择要导出的列 </param>
/// <returns> 平面文本 </returns>
public static string Export2FlotText(IEnumerable dataSource,DataGridColumn[] columns, bool isVisiableColumn)
{
return Export2Text(dataSource, " \t ", columns, isVisiableColumn);
}
/// <summary>
/// 导出为平面文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="columns"> 选择要导出的列 </param>
/// <returns> 平面文本 </returns>
public static string Export2FlotCSV(IEnumerable dataSource,DataGridColumn[] columns, bool isVisiableColumn)
{
return Export2Text(dataSource, " , ", columns, isVisiableColumn);
}
}
/// 数据源导出辅助类
/// </summary>
/// <remarks>
/// Author: sucsy
/// Create date: 2011-6-17
/// Description: 提供基于数据源(公共枚举器类型)的数据导出辅助功能
/// </remarks>
public class DataSourceExportHelper
{
/// <summary>
/// 导出到文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="splitString"> 分隔符 </param>
/// <param name="columns"> 导出的列(为null时表示全部导出) </param>
/// <returns> 文本数据 </returns>
public static string Export2Text(IEnumerable dataSource, string splitString,DataGridColumn[] columns, bool isVisiableColumn)
{
// 定义结果存放对象
StringBuilder sbResult = new StringBuilder();
string[] columnHeaders = GetSelectedColumns(columns);
// 处理DataTable作为数据源的数据导出操作
if (dataSource is System.Data.DataView)
{
System.Data.DataView dataView = (System.Data.DataView)dataSource;
// 加载数据标题
foreach (DataColumn objHeaderpi in dataView.Table.Columns)
{
if (columns == null || columnHeaders.Contains(objHeaderpi.ColumnName))
sbResult.Append(objHeaderpi.ColumnName + splitString);
}
sbResult.AppendLine();
foreach (System.Data.DataRowView dr in dataView)
{
foreach (DataColumn objHeaderpi in dataView.Table.Columns)
{
if (columns == null || columnHeaders.Contains(objHeaderpi.ColumnName))
sbResult.Append(dr[objHeaderpi.ColumnName].ToString() + splitString);
}
sbResult.AppendLine();
}
}
else
{
foreach (DataGridColumn dgcol in columns)
sbResult.Append(dgcol.Header.ToString() + splitString);
sbResult.AppendLine();
foreach ( object data in dataSource)
{
foreach (DataGridColumn col in columns)
{
string strValue = "";
Binding objBinding = null;
if (col is DataGridBoundColumn)
objBinding = (Binding)(col as DataGridBoundColumn).Binding;
if (col is DataGridTemplateColumn)
{
DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
FrameworkElement oFE = (FrameworkElement)objDO;
FieldInfo oFI = oFE.GetType().GetField( " TextProperty ");
if (oFI != null)
{
if (oFI.GetValue( null) != null)
{
if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue( null)) != null)
objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue( null)).ParentBinding;
}
}
}
if (objBinding != null)
{
if (objBinding.Path.Path != "")
{
string[] dataTree = objBinding.Path.Path.Split( " . ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
object parentData = data;
for ( int treeIndex = 0; treeIndex < dataTree.Length; treeIndex++)
{
PropertyInfo pData = parentData.GetType().GetProperty(dataTree[treeIndex]);
parentData = pData.GetValue(parentData, null);
}
if (parentData != null) strValue = parentData.ToString();
}
if (objBinding.Converter != null)
{
if (strValue != "")
strValue = objBinding.Converter.Convert(strValue, typeof( string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
else
strValue = objBinding.Converter.Convert(data, typeof( string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
}
}
sbResult.Append(strValue.Replace( ' , ', ' , ') + splitString);
}
sbResult.AppendLine();
}
}
return sbResult.ToString();
}
public static string[] GetSelectedColumns(DataGridColumn[] columns)
{
List< string> columnsHeader = new List< string>();
foreach (DataGridColumn column in columns)
{
if (column.Visibility == System.Windows.Visibility.Visible)
columnsHeader.Add(column.Header.ToString());
}
return columnsHeader.ToArray();
}
/// <summary>
/// 导出为平面文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="columns"> 选择要导出的列 </param>
/// <returns> 平面文本 </returns>
public static string Export2FlotText(IEnumerable dataSource,DataGridColumn[] columns, bool isVisiableColumn)
{
return Export2Text(dataSource, " \t ", columns, isVisiableColumn);
}
/// <summary>
/// 导出为平面文本
/// </summary>
/// <param name="dataSource"> 数据源 </param>
/// <param name="columns"> 选择要导出的列 </param>
/// <returns> 平面文本 </returns>
public static string Export2FlotCSV(IEnumerable dataSource,DataGridColumn[] columns, bool isVisiableColumn)
{
return Export2Text(dataSource, " , ", columns, isVisiableColumn);
}
}
本文来自旧木的博客,原文地址:http://www.cnblogs.com/sucsy/archive/2011/09/23/2186242.html
这篇关于WPF及Silverlight中将DataGrid数据导出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!