将 excel 宏转换为 C#

converting excel macro to C#

我正在 excel 中创建 VSTO,为此我将现有的宏转换为 c#,其中必须找到该列的最小值,我已经转换到如下所示:

using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.Excel;

namespace CpCpk
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            var excelApp = Globals.ThisAddIn.Application.ActiveSheet;
            //excelApp.Workbooks.Add();
            excelApp.Range["P2"].Select();
            excelApp.ActiveCell.FormulaR1C1 = "=MIN(C[-14])";
            excelApp.Range["Q2"].Select();
            excelApp.Visible = true;
        } 
    }
}

现在我没有遇到任何语法错误,但在执行过程中我遇到了以下错误:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'ActiveCell''

在行中:

excelApp.ActiveCell.FormulaR1C1 = "=MIN(C[-14])";

谁能帮我解决这个问题..

您可以像这样将电子表格转换为数据表 (通过 nuget):

public static DataTable GetDataFromExcel(string path, dynamic worksheet)
    {
        //Save the uploaded Excel file.


        DataTable dt = new DataTable();
        //Open the Excel file using ClosedXML.
        using (XLWorkbook workBook = new XLWorkbook(path))
        {
            //Read the first Sheet from Excel file.
            IXLWorksheet workSheet = workBook.Worksheet(worksheet);

            //Create a new DataTable.

            //Loop through the Worksheet rows.
            bool firstRow = true;
            foreach (IXLRow row in workSheet.Rows())
            {
                //Use the first row to add columns to DataTable.
                if (firstRow)
                {
                    foreach (IXLCell cell in row.Cells())
                    {
                        if (!string.IsNullOrEmpty(cell.Value.ToString()))
                        {
                            dt.Columns.Add(cell.Value.ToString());
                        }
                        else
                        {
                            break;
                        }
                    }
                    firstRow = false;
                }
                else
                {
                    int i = 0;
                    DataRow toInsert = dt.NewRow();
                    foreach (IXLCell cell in row.Cells(1, dt.Columns.Count))
                    {
                        try
                        {
                            toInsert[i] = cell.Value.ToString();
                        }
                        catch (Exception ex)
                        {

                        }
                        i++;
                    }
                    dt.Rows.Add(toInsert);
                }
            }
            return dt;
        }

然后通过linq获取最小值:

var min = dt.AsEnumerable()
        .Min(r  => r.Field<Decimal>(col));

提醒,您需要 using System.Linq; 并使用 ClosedXML.Excel;

您需要直接从特定单元格

设置FormulaR1C1
excelApp.Range["P2"].FormulaR1C1 = "=MIN(C[-14])";