WPF DataGrid如何分页、导出Excle、打印(急)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF DataGrid如何分页、导出Excle、打印(急)相关的知识,希望对你有一定的参考价值。

最近新学WPF技术,公司要实现以上技术,如果不能实现就要走人,哪位好心人帮一下忙,跪谢!注(是WPF里的DataGrid,另外到处可以选中行导出和全部导出)
分页是实现前一页,后一页,最前页,最后页,当前页号/总页号 ;可以获取数据源,可是数据源始对象列表,并且可以获取行号,请问该怎么导出到Excle

我用的是sql分页,用静态变量保存当前页数,然后在相应的事件里处理。你能获取你选中的行吧!数据源都获取到了,所以导出Excle跟平时的导入导出没什么分别。 参考技术A Excel的导出一般采取的方式是获取数据源的数据,然后对数据进行处理,形成CAS字符串的形式导出.具体代码(来自网站codeproject,具体可以搜一下,适应与silverlight对wpf大同小异,请适当修改一下!):
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

public static class DataGridExtensions

public static void Export(this DataGrid dg)

ExportDataGrid(dg);


public static void ExportDataGrid(DataGrid dGrid)

SaveFileDialog objSFD = new SaveFileDialog() DefaultExt = "csv", Filter = "CSV Files (*.csv)|*.csv|Excel XML (*.xml)|*.xml|All files (*.*)|*.*", FilterIndex = 1 ;
if (objSFD.ShowDialog() == true)

string strFormat = objSFD.SafeFileName.Substring(objSFD.SafeFileName.IndexOf('.') + 1).ToUpper();
StringBuilder strBuilder = new StringBuilder();
if (dGrid.ItemsSource == null) return;
List<string> lstFields = new List<string>();
if (dGrid.HeadersVisibility == DataGridHeadersVisibility.Column || dGrid.HeadersVisibility == DataGridHeadersVisibility.All)

foreach (DataGridColumn dgcol in dGrid.Columns)
lstFields.Add(FormatField(dgcol.Header.ToString(), strFormat));
BuildStringOfRow(strBuilder, lstFields, strFormat);

foreach (object data in dGrid.ItemsSource)

lstFields.Clear();
foreach (DataGridColumn col in dGrid.Columns)

string strValue = "";
Binding objBinding = null;
if (col is DataGridBoundColumn)
objBinding = (col as DataGridBoundColumn).Binding;
if (col is DataGridTemplateColumn)

//This is a template column... let us see the underlying dependency object
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 != "")

PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
if (pi != null) strValue = pi.GetValue(data, null).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();


lstFields.Add(FormatField(strValue,strFormat));

BuildStringOfRow(strBuilder, lstFields, strFormat);

StreamWriter sw = new StreamWriter(objSFD.OpenFile());
if (strFormat == "XML")

//Let us write the headers for the Excel XML
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">");
sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine("<Author>Arasu Elango</Author>");
sw.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>");
sw.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>");
sw.WriteLine("<Company>Atom8 IT Solutions (P) Ltd.,</Company>");
sw.WriteLine("<Version>12.00</Version>");
sw.WriteLine("</DocumentProperties>");
sw.WriteLine("<Worksheet ss:Name=\"Silverlight Export\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
sw.WriteLine("<Table>");

sw.Write(strBuilder.ToString());
if (strFormat == "XML")

sw.WriteLine("</Table>");
sw.WriteLine("</Worksheet>");
sw.WriteLine("</Workbook>");

sw.Close();


private static void BuildStringOfRow(StringBuilder strBuilder, List<string> lstFields, string strFormat)

switch (strFormat)

case "XML":
strBuilder.AppendLine("<Row>");
strBuilder.AppendLine(String.Join("\r\n", lstFields.ToArray()));
strBuilder.AppendLine("</Row>");
break;
case "CSV":
strBuilder.AppendLine(String.Join(",", lstFields.ToArray()));
break;


private static string FormatField(string data, string format)

switch (format)

case "XML":
return String.Format("<Cell><Data ss:Type=\"String\">0</Data></Cell>", data);
case "CSV":
return String.Format("\"0\"", data.Replace("\"", "\"\"\"").Replace("\n", "").Replace("\r", ""));

return data;

本回答被提问者采纳

以上是关于WPF DataGrid如何分页、导出Excle、打印(急)的主要内容,如果未能解决你的问题,请参考以下文章

WPF 实现 DataGrid/ListView 分页控件

WPF 实现 DataGrid/ListView 分页控件(转)

WPF 实现 DataGrid/ListView 分页控件(转)

WPF 中的DataGrid怎么分页啊

WPF中如何实现分页打印! 主要是打印DataGrid控件中的数据,若实现打印数据库中查询出的数据也可以。

Jquery datatables导出Excle表的问题