Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;


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.


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);

            }
Posted
Updated 28-Dec-18 2:01am

1 solution

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

As to the first error message:
Quote:
Cannot convert null to int because it is not a non nullable value type

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900