目前项目使用第三方空间Telerik RadControls,在数据导出方面提供便利
可以直接使用RadGridView,将GridView中数据导出为需要的格式。
//表格数据导出
private void btn_export_Click(object sender, RoutedEventArgs e)
{
string extension = "";
ExportFormat format = ExportFormat.Html;
RadComboBoxItem comboItem = this.rcb_format.SelectedItem as RadComboBoxItem;
string selectedItem = comboItem.Content.ToString();
switch (selectedItem)
{
case "Excel": extension = "xls";
format = ExportFormat.Html;
break;
case "ExcelML": extension = "xml";
format = ExportFormat.ExcelML;
break;
case "Word": extension = "doc";
format = ExportFormat.Html;
break;
case "Csv": extension = "csv";
format = ExportFormat.Csv;
break;
}
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = extension;
dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem);
dialog.FilterIndex = 1;
if (dialog.ShowDialog())
{
using (Stream stream = dialog.OpenFile())
{
GridViewExportOptions exportOptions = new GridViewExportOptions();
exportOptions.Format = format;
exportOptions.ShowColumnFooters = true;
exportOptions.ShowColumnHeaders = true;
exportOptions.ShowGroupFooters = true;
this.radgridview.Export(stream, exportOptions);
}
}
}
* RadGridView到处为Excel是带有HTML元素的xls表格,在用OLEDB打开会 提示“外部表不是预期的格式。”,为以后方便导入,到处为CSV文件最合适。