I have a datagrid which populates a datatable, I want to export the DataTable to excel on click of a button. I am using MVVM for this application. So is it ok to implement the export feature in my view?

Secondly, as i am using xaml and desktop application, how do i capture the grid and its details.

Can any one suggest me some pointers? I am new to this.

Thanks, Sagar

Here's an extension method to output to any DataTable to a csv (which excel can open)

<!-- language: lang-vb -->
Imports System.Text
Imports System.IO
Imports System.Runtime.CompilerServices

Module ExtensionMethods

	<Extension()> _
	Public Sub OutputAsCSV(ByVal dt As DataTable, ByVal filePath As String)
		Dim sb As New StringBuilder()
				
				'write column names to string builder
		Dim columnNames As String() = (From col As DataColumn In dt.Columns Select col.ColumnName).ToArray
		sb.AppendLine(String.Join(",", columnNames))
				
				'write cell value in each row to string builder
		For Each row As DataRow In dt.Rows
			Dim fields As String() = (From cell In row.ItemArray Select CStr(cell)).ToArray
			sb.AppendLine(String.Join(",", fields))
		Next
				
				'write string builder to file
		File.WriteAllText(filePath, sb.ToString())
	End Sub

End Module