How can I add a value to an Excel Cell of the active Workbook using C#? I'm new to VSTO C# and can't find a solution that works for me...
This is my code (from this question Write to cell in excel using c#) :
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelSDRAddIn
{
    public partial class UserControlSDR : UserControl
    {
        public UserControlSDR()
        {
            InitializeComponent();
        }
        private void btnTemplate_Click(object sender, EventArgs e)
        {
            Excel.Worksheet ws = (Excel.Worksheet)(sender as Workbook).ActiveSheet;
            ws.Cells[1, 1] = "Value";
        }
    }
}
After running it I get the following exception on this line Excel.Worksheet ws = (Excel.Worksheet)(sender as Workbook).ActiveSheet;:
An exception of type 'System.NullReferenceException' occurred in ExcelSDRAddIn.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Also tried with this:
    dynamic excelType = Type.GetTypeFromProgID("Excel.Application");
    excelType.Visible = true;
    excelType.Workbooks.Add();
    dynamic workSheet = excelType.ActiveSheet;
    workSheet.Cells[1, 1] = "Names";
    workSheet.Cells[1, 2] = "Age";
And returns:
An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code
Additional information: 'System.Reflection.TypeInfo' does not contain a definition for 'Visible'
Another example that fails:
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
...
var Excel = new Excel.Application();
oXL = new Microsoft.Office.Interop.Excel.Application();
oWB = oXL.ActiveWorkbook;
oSheet = oWB.ActiveSheet;
oSheet.Cells[1, 1] = "Value";
 
     
     
    