I am trying to write my column names to an Excel worksheet using:
for (int i = 0; i < intColumnCount; i )
{
strColumnName = dataPHLIP251.Columns[i].ToString();
excelWorksheet.Cells[2, i].Value = strColumnName;
}
But I keep getting
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
I have tried it with and without the Value on the Cells. How do I get around this problem?
CodePudding user response:
I think the problem is that excel interop is not handeling cells like arrays in terms of access. 0 = out of range. Try:
excelWorksheet.Cells[2, i 1].Value = strColumnName;
If that is not the case, please provide the code where you generate the excelworksheet instance and in which line exactly the Exception is thrown.
CodePudding user response:
I think reason is still not clear with this code line. You should check the inner exception for this error which may have further explanation for causing this error.
Piece of code you shared has nothing to do with this. For better explanation please share more code.