无法将类型'object'隐式转换为'Microsoft.Office.Interop.Excel.Worksheet'.存在显式转换(您是否错过了演员?)

221*_*21B 8 .net c# excel interop excel-interop

在这里,我打开excel并写入excel表.我正在将我的Windows应用程序更改为asp网站并看到此错误.我添加了所有引用和库.不知道我在这里失踪了什么.

如下所述获得错误.请帮我.

    Excel.Application excel = new Excel.Application();
    excel.Visible = false; // to hide the processing 
    Excel.Workbook wb = excel.Workbooks.Add();
    Excel.Worksheet sh = wb.Sheets.Add(); // Error at wb


    sh.Name = "Links";

    for (int i = 1; i < list.Count; i++)
    {
        sh.Cells[i.ToString(), "A"].Value2 = list[i]; //Error at .Value2

    }
Run Code Online (Sandbox Code Playgroud)

Sud*_*udi 10

你必须Sheets通过提供WorkSheet名称来创建一个带有数组的新工作表.还请铸造新创造的WorkSheet.

替换这个:

Excel.Worksheet sh = wb.Sheets.Add();
Run Code Online (Sandbox Code Playgroud)

以下

 Excel.Worksheet sh  = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets["Sheet1"];
Run Code Online (Sandbox Code Playgroud)