【发布时间】:2015-11-21 18:08:50
【问题描述】:
我正在尝试将 WPF 数据网格导出到 Excel 工作表,但我不断收到错误消息。我找到了这个例子,但我不知道如何从 observablecollection 中导出。在这个例子中,似乎有一个列表。所以我认为我必须从列表转换为 observablecollection,但我做错了。我对 C# 和 WPF 中的功能还很陌生,因此非常感谢任何帮助。
http://www.codeproject.com/Articles/120480/Export-to-Excel-Functionality-in-WPF-DataGrid
我的代码
private ObservableCollection<T_P> _observableCollection;
private CollectionViewSource _collectionViewSource;
public ExportExcel()
{
InitializeComponent();
using (var dc = new DataClasses2DataContext())
{
_observableCollection = new ObservableCollection<T_P>(dc.T_Ps);
}
_collectionViewSource = (CollectionViewSource)this.FindResource("_T_PViewSource") as CollectionViewSource;
_collectionViewSource.Source = _observableCollection;
this.T_P = new T_P();
this.DataContext = T_P;
}
private void btnExportExcel_Click(object sender, RoutedEventArgs e)
{
ExportToExcel();
//some code here?
}
// 怎么调用这个类?
public class ExportToExcel<T>
where T : class
{
public List<T> dataToPrint;
// Excel object references.
private Excel.Application _excelApp = null;
private Excel.Workbooks _books = null;
private Excel._Workbook _book = null;
private Excel.Sheets _sheets = null;
private Excel._Worksheet _sheet = null;
private Excel.Range _range = null;
private Excel.Font _font = null;
// Optional argument variable
private object _optionalValue = Missing.Value;
//......
【问题讨论】:
-
显示
ExportToExcel构造函数
标签: c# wpf excel datagrid observablecollection