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