make my export Excel method public and call it from other classes C# WinForms

Viewed 36

I make button_click event for export data from gridview in c# winforms to excel spreadsheet as following

        private void copyAlltoClipboard()
        {
            myGridView.SelectAll();
            DataObject dataObj = myGridView.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            copyAlltoClipboard();
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            Clipboard.SetDataObject("");
        }

the problem that i repeat them so much in every view how i can make this two mmthods public and just call them and there is only one variable which is myGridView Regards

1 Answers

Basically you change the accessibility of the method and make any local dependencies become arguments for your method.

Making the method static can help to isolate any local dependencies

public static class GridHelper
{
    public static void CopyToClipboard(DataGridView gridView)
    {
        gridView.SelectAll();
        DataObject dataObj = gridView.GetClipboardContent();
        if (dataObj != null)
            Clipboard.SetDataObject(dataObj);
    }

    public static void ExportToExcel(DataGridView gridView)
    {
        CopyToClipboard(gridView);
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Microsoft.Office.Interop.Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
        Clipboard.SetDataObject("");
    }
}

Then you could call that like this:

private void btnExportExcel_Click(object sender, EventArgs e)
{
    GridHelper.ExportToExcel(myGridView);
}

You can actually go one step further and turn these into extension methods of the DataGridView, just by changing the signature of the static methods so that the DataGridView parameter is prefixed with this :

public static void CopyToClipboard(this DataGridView gridView)
{
    ...
}

and

public static void ExportToExcel(this DataGridView gridView)
{
    ...
}

Now the invokation is more fluent:

private void btnExportExcel_Click(object sender, EventArgs e)
{
    myGridView.ExportToExcel();
}
Related