I have the blow routine
private void btn_ExportExcel(object sender, RoutedEventArgs e)
{
if (dgResults.DataContext == null) return;
var extension = "xls";
var dialog = new Microsoft.Win32.SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{
dgResults.ElementExporting -= ElementExporting;
dgResults.ElementExporting += ElementExporting;
using (var stream = dialog.OpenFile())
{
// dgResults.ExportSettings.ExportOnlyData = true;
// dgResults.ExportSettings.UseItemStyles = true;
dgResults.Export(stream,
new GridViewExportOptions()
{
Format = ExportFormat.Html,
ShowColumnHeaders = true,
ShowColumnFooters = false,
ShowGroupFooters = false,
});
}
MessageBox.Show("Exporting to Excel has completed successfully");
}
}
The routine is executing succesfully, after saving the resultant Excel, when I try to open it, its showing an error messsage as below...
"The file format and extension of 'File1.xls' don't match. The file could be currupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
I tried with different formats like csv, html, ExcelML, the same error pops out. When I click 'Yes', its opening with the perfect data.
I would like to request, any help to fix this issue.
Thanks in advance.