Winforms export datatable to excel without COM+

Simple export of a datatable to excel without using COM+

Excel will ask if you want to open this as ‘An XML List’

SaveFileDialog fileDialog = new SaveFileDialog();

fileDialog.Filter = "Excel files (*.xls)|*.xls";

fileDialog.FileName = "My_File_Name.xls";

fileDialog.ShowDialog();

DataTable dt = GetFilteredDataTable(DisplayedWorkbookView.EmployeesByFocusGroup);

using (FileStream fs = (FileStream)fileDialog.OpenFile())

{

using (StreamWriter sw = new StreamWriter(fs))

{

sw.Write("<?xml version=\"1.0\" standalone=\"yes\"?>");

dt.WriteXml(sw, XmlWriteMode.IgnoreSchema);

sw.Close();

}

fs.Close();

}