5

我正在使用简单的方法,它将我的 DataGridView 保存到 Excel 文档(仅 1 张)中,还添加了 VBA 代码和一个按钮来运行 VBA 代码。

public void SaveFile(string filePath)
    {

        Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        ExcelApp.Application.Workbooks.Add(Type.Missing);

        //Change  Workbook-properties.
        ExcelApp.Columns.ColumnWidth = 20;

        // Storing header part in Excel.
        for (int i = 1; i < gridData.Columns.Count + 1; i++)
        {
            ExcelApp.Cells[1, i] = gridData.Columns[i - 1].HeaderText;
        }

        //Storing Each row and column value to excel sheet
        for (int row = 0; row < gridData.Rows.Count; row++)
        {
            gridData.Rows[row].Cells[0].Value = "Makro";
            for (int column = 0; column < gridData.Columns.Count; column++)
            {
                ExcelApp.Cells[row + 2, column + 1] = gridData.Rows[row].Cells[column].Value.ToString();
            }
        }

        ExcelApp.ActiveWorkbook.SaveCopyAs(filePath);
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
    }

我只实现了 DataGridView 导出。

编辑:感谢乔尔,我可以用适当的语言再次搜索解决方案。我认为这可能会有所帮助。你会纠正我还是给我一两个关于我应该寻找什么的提示。

4

1 回答 1

7

我刚刚写了一个小例子,它向现有工作簿添加了一个新按钮,然后添加了一个宏,该宏将在单击该按钮时被调用

using Excel = Microsoft.Office.Interop.Excel;
using VBIDE = Microsoft.Vbe.Interop;
...

private static void excelAddButtonWithVBA()
{
    Excel.Application xlApp = new Excel.Application();
    Excel.Workbook xlBook = xlApp.Workbooks.Open(@"PATH_TO_EXCEL_FILE");
    Excel.Worksheet wrkSheet = xlBook.Worksheets[1];
    Excel.Range range;

    try
    {
        //set range for insert cell
        range = wrkSheet.get_Range("A1:A1");

        //insert the dropdown into the cell
        Excel.Buttons xlButtons = wrkSheet.Buttons();
        Excel.Button xlButton = xlButtons.Add((double)range.Left, (double)range.Top, (double)range.Width, (double)range.Height);

        //set the name of the new button
        xlButton.Name = "btnDoSomething";
        xlButton.Text = "Click me!";
        xlButton.OnAction = "btnDoSomething_Click";

        buttonMacro(xlButton.Name, xlApp, xlBook, wrkSheet);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
    xlApp.Visible = true;
}

在这里我们得到了buttonMacro(..)方法

private static void buttonMacro(string buttonName, Excel.Application xlApp, Excel.Workbook wrkBook, Excel.Worksheet wrkSheet)
{
    StringBuilder sb;
    VBIDE.VBComponent xlModule;
    VBIDE.VBProject prj;

    prj = wrkBook.VBProject;
    sb = new StringBuilder();

    // build string with module code
    sb.Append("Sub " + buttonName + "_Click()" + "\n");
    sb.Append("\t" + "msgbox \"" + buttonName + "\"\n"); // add your custom vba code here
    sb.Append("End Sub");

    // set an object for the new module to create
    xlModule = wrkBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

    // add the macro to the spreadsheet
    xlModule.CodeModule.AddFromString(sb.ToString());
}

Found this information within an KB article How To Create an Excel Macro by Using Automation from Visual C# .NET

于 2013-02-13T21:30:05.443 回答