从 observablecollection C# 导出到 excel
Export to excel from observablecollection C#
我正在尝试将 WPF 数据网格导出到 Excel sheet,但我不断收到错误。我找到了这个例子,但我不知道如何从 observablecollection 中导出。在此示例中,似乎有一个列表。所以我想我必须从列表转换为 observablecollection,但我做对了。我对 C# 和 WPF 中的功能还不熟悉,所以非常感谢任何帮助。
Export WPF DataGrid to Excel
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?
}
// 我怎么称呼这个 class?
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;
//......
您的代码不完整,因此很难判断,但大致如下:
private void btnExportExcel_Click(object sender, RoutedEventArgs e)
{
var exporter = new ExportToExcel<T_P>()
{
dataToPrint = new List<T_P>(_observableCollection);
};
}
我正在尝试将 WPF 数据网格导出到 Excel sheet,但我不断收到错误。我找到了这个例子,但我不知道如何从 observablecollection 中导出。在此示例中,似乎有一个列表。所以我想我必须从列表转换为 observablecollection,但我做对了。我对 C# 和 WPF 中的功能还不熟悉,所以非常感谢任何帮助。
Export WPF DataGrid to Excel
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?
}
// 我怎么称呼这个 class?
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;
//......
您的代码不完整,因此很难判断,但大致如下:
private void btnExportExcel_Click(object sender, RoutedEventArgs e)
{
var exporter = new ExportToExcel<T_P>()
{
dataToPrint = new List<T_P>(_observableCollection);
};
}