Resolved Problem with creat Excel worksheet using C#

Rabi

Member
Joined
Mar 22, 2022
Messages
17
Programming Experience
Beginner
I am trying to creat code for export data from DGV to Excel . And i faced a problem in creating worksheet.

C#:
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
 
 Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true

 worksheet = workbook.Sheets["Sheet1"]; 
 worksheet = workbook.ActiveSheet;

It showed error at the last two codes :
C#:
worksheet = workbook.Sheets["Sheet1"];
 worksheet = workbook.ActiveSheet;

Error is :
cannot implicitly convert type 'object' to Microsoft.Office.Interop.Excel._Worksheet. An explicit conversion exists (are you missing a cast)
 
Last edited by a moderator:
Solution
You will have to perform a cast because both the ActiveSheet property and the Sheets indexer return object types instead of your expected Microsoft.Office.Interop.Excel._Worksheet.
You will have to perform a cast because both the ActiveSheet property and the Sheets indexer return object types instead of your expected Microsoft.Office.Interop.Excel._Worksheet.
 
Solution
Back
Top Bottom