Question: 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

Answer: 
This is what i have done and it helped me:

```
private readonly ICommand m_ExportButtonClick;
    private string ExcelFilePath = "";

    public ICommand ExportButtonClick { get { return m_ExportButtonClick; } }

    private void OnRunExport()
    {
        try
        {
            if (queryDatatable == null || queryDatatable.Columns.Count == 0)
                throw new Exception("ExportToExcel: Null or empty input table!\n");

            // load excel, and create a new workbook
            Excell.Application excelApp = new Excell.Application();
            excelApp.Workbooks.Add();

            // single worksheet
            Excell._Worksheet workSheet = excelApp.ActiveSheet;

            // column headings
            for (int i = 0; i < queryDatatable.Columns.Count; i++)
            {
                workSheet.Cells[1, (i + 1)] = queryDatatable.Columns[i].ColumnName;
            }

            // rows
            for (int i = 0; i < queryDatatable.Rows.Count; i++)
            {
                // to do: format datetime values before printing
                for (int j = 0; j < queryDatatable.Columns.Count; j++)
                {
                    workSheet.Cells[(i + 2), (j + 1)] = queryDatatable.Rows[i][j];
                }
            }

            // check fielpath
            if (ExcelFilePath != null && ExcelFilePath != "")
            {
                try
                {                        
                    workSheet.SaveAs(ExcelFilePath);
                    excelApp.Quit();
                    MessageBox.Show("Excel file saved!");
                }
                catch (Exception ex)
                {
                    throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                        + ex.Message);
                }
            }
            else    // no filepath is given, the file opens up and the user can save it accordingly.
            {
                excelApp.Visible = true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("There is no data to export. Please check your query/ Contact administrator." + ex.Message);
        }
    }      
    #endregion

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

```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

```