Write to cell in excel using c#

三世轮回 提交于 2019-12-06 05:00:55

You probably create a new Excel application, instead of connecting to the active Excel application.

Use the GetActiveObject to get your reference to the Excel application object.

Look, I tried and this code works:

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
    Excel.Worksheet ws = (Excel.Worksheet)(sender as Workbook).ActiveSheet;

    System.Random rnd = new System.Random();

    for (int i = 1; i <= 20; i++)
        ws.Cells[i, 2] = rnd.Next(100);
}

so as you see you can access the Worksheet and use ws.Cells to manipulate cell values.

there are plenty of examples here:

Understanding the Excel Object Model from a .NET Developer's Perspective

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!