在C#中为excel添加插件 [英] Adding addin for excel in C#

查看:254
本文介绍了在C#中为excel添加插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个C#Excel插件,以便自动化某些单元格值的计算。插件应该做的是当选中一个框时,它应该将单元格的值乘以其他单元格并粘贴结果我得到的错误是无法将null转换为int,因为它不是一个不可为空的值类型。当Excel打开时我输入了数字,在我选中该框之后我得到了这个错误。从我的理解这行代码从Excel获取null:



I'm trying to make an C# Excel addin in order to automate the calculus of some cells value.What the addin should do is when a box is checked it should multiply the value of a cell with other cells and paste the result in different cells.The error that I get is "Cannot convert null to int because it is not a non nullable value type".When the Excel opens I put in numbers and after I check the box I get this error.From what I understand this line of code gets a null from Excel:

int cellValue = sheet.Cells[1, 12].Value2;





我尝试在我的代码的第一个变体中打开一个特定的Excel工作簿但它没有用。它打开一个新的Excel空文件。消除



I tried in the first variant of my code to open a specific Excel workbook but it didn't work.It opens a new Excel empty file.Eliminating

var excelApp = new Excel.Application() as Excel._Application;



也会出现一个新错误:


also brings up a new error:

Quote:

非静态字段方法或属性需要对象引用。

An object reference is required for the non-static field method or property.





我尝试过:



我尝试了这2个变种。我得到了同样的错误。







What I have tried:

I Tried this 2 variants.I get the same error.


public void fc(string[] args)
        {
           

            string dataFilePath = new FileInfo(args[0]).FullName;
          
            string excelTemplateFilePath = Path.Combine(
               Path.GetDirectoryName(dataFilePath),
               "informations.xlsx");
            var excelApp = new Excel.Application() as Excel._Application;
            Excel.Workbook workbook = excelApp.Workbooks.Open(excelTemplateFilePath);
            Excel.Worksheet worksheet = workbook.Sheets[1];
            var dataRange = worksheet.get_Range("A20");
            dataRange.Value2 = 0.5;
            string baseFileName = Path.Combine(
              Path.GetDirectoryName(dataFilePath),
              Path.GetFileNameWithoutExtension(dataFilePath));
            string excelFilePath = baseFileName + ".xlsx";
            for (int i = 0; i <= worksheet.Cells.Count; i++)
            {
                for (int j = 0; j <= worksheet.Cells.Count; j++)
                {
                    while (worksheet.Cells[i+1, 7] != null)
                    {
                        (worksheet.Cells[i+1, 8] as Excel.Range).Value2 = worksheet.Cells[i+1, 7] * dataRange.Value2;
                    }
                }
            }
            System.Diagnostics.Process.Start(excelFilePath);
        }
















if (((RibbonCheckBox)sender).Checked)
        {
            try
            {
                Excel.Application app = new Excel.Application();
                app.Workbooks.Add();
                Excel.Worksheet sheet = app.ActiveSheet;
                int cellValue = sheet.Cells[1, 12].Value2;
                for (int i = 0; i <= sheet.Cells.Count; i++)
                {
                    for (int j = 0; j <= sheet.Cells.Count; j++)
                    {
                        while (sheet.Cells[i + 1, 7] != null)
                        {
                            sheet.Cells[i + 1, 8].Value2 = sheet.Cells[i + 1, 7] * cellValue;
                        }
                    }
                }
            }
            catch(Exception ex)
            {

                MessageBox.Show("Exceptie:" + ex);

            }

推荐答案

解决问题的最佳方法是使用debugger [ ^ ]。



至于第一条错误消息:

The best way to resolve your issue is to use debugger[^].

As to the first error message:
Quote:

无法将null转换为int,因为它是不是非可空值类型

Cannot convert null to int because it is not a non nullable value type



是的,这可能意味着 sheet.Cells [1,12] .Value2 持有可空值。



关于第二条错误消息:您不需要创建另一个Excel应用程序实例。您应该处理Excel应用程序的当前实例,因为它存储当前打开的文件。


Yes, this might means that sheet.Cells[1, 12].Value2 holds nullable value.

As to the second error message: you don't need to create another instance of Excel application. You should work on current instance of Excel application as it stores currently opened files.


这篇关于在C#中为excel添加插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆