It is not possible to implicitly convert the type "object" to "Microsoft.Office.Interop.Excel.Worksheet". An explicit conversion exists (do you miss the role?)

This is where I start to read and write fine on a sheet. I am changing a Windows application to an asp site and I see this error. I have added all the links and libraries. I don’t know what I am missing here.

Getting the error as below. Please help me.

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 } 
+6
source share
2 answers

you need to create a new worksheet with an array of Sheets by specifying a WorkSheet Name. and also, please drop out the newly created WorkSheet .

Replace this:

 Excel.Worksheet sh = wb.Sheets.Add(); 

with the following

  Excel.Worksheet sh = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets["Sheet1"]; 
+9
source

To fix the second error,

// Error in .Value2

  1. Go to project properties. (Click "Project" on the menu, click "Properties")
  2. Install Target Platform as .NET Framework 4
  3. This should fix your .Value2 error.
0
source

Source: https://habr.com/ru/post/957767/


All Articles