制作我的导出 Excel 方法 public 并从其他 类 C# WinForms 调用它

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

我制作了 button_click 事件,用于将数据从 c# winforms 中的 gridview 导出到 excel 电子表格 如下

        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("");
        }

我在每个视图中都重复了这么多的问题 我如何制作这两个 mmthods public 并调用它们 只有一个变量 myGridView 此致

基本上,您更改方法的可访问性并使任何本地依赖项成为您的方法的参数。

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("");
    }
}

然后你可以这样称呼它:

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

您实际上可以更进一步,将它们变成 DataGridView 的扩展方法,只需更改静态方法的签名,使 DataGridView 参数带有前缀 [=17] =]:

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

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

现在调用更流畅:

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