【问题标题】:converting excel macro to C#将excel宏转换为C#
【发布时间】:2021-04-21 18:19:58
【问题描述】:

我在 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])";

请有人帮我解决这个问题..

【问题讨论】:

  • 试试 excelApp.Range["P2"].FormulaR1C1 = "=MIN(C[-14])";
  • 还有@matmahnke,请将您的评论更改为答案,以便我可以标记相同
  • 对不起,我对excel一无所知

标签: c# excel vba visual-studio vsto


【解决方案1】:

您需要直接从特定单元格设置FormulaR1C1

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

【讨论】:

    【解决方案2】:

    您可以像这样将电子表格转换为数据表 via closed xml(通过 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;

    【讨论】:

    • 非常感谢您的回答,但评论中的上述解决方案对我有用..
    猜你喜欢
    • 2010-09-15
    • 2010-09-10
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多