Conversión de C# Excel

I have an existing project that creates an excel spreadsheet using the Microsoft.Office.Interop.Excel.WorkbookClass and I am trying to convert the workbook object to the Microsoft.Office.Tools.Excel.Workbook, but I am getting an exception thrown stating:

"Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorkbookClass' to class type 'Microsoft.Office.Tools.Excel.Workbook'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."

Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Tools.Excel.Workbook xlWorkbook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Tools.Excel.Worksheet xlNewSheet = null;

xlApp = new Interop.Application();
xlApp.Visible = true;
xlWorkbook = (Microsoft.Office.Tools.Excel.Workbook)xlApp.Workbooks.Add(Interop.XlWBATemplate.xlWBATWorksheet);
xlSheets = xlWorkbook.Sheets as Interop.Sheets;
xlNewSheet = (Microsoft.Office.Tools.Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlNewSheet.Name = "SheetName1";

Is this possible if not, what other options can I take that since the excel sheet is already created using the interop class and

preguntado el 09 de septiembre de 13 a las 21:09

Hello. Did you get an answer to your question? I'm facing the exact same problem. A standalone application that creates a new Excel sheet and I want to access the controls to add events. Were you able to convert the Interop sheet to a Tools sheet? -

2 Respuestas

If possible try to avoid using the Microsoft.Office.Tools assembly which is internal to Visual Studio Tools For Office.

I've amended your code as below:

    Microsoft.Office.Interop.Excel.Application xlApp = null;
    Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
    Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
    Microsoft.Office.Interop.Excel.Worksheet xlNewSheet = null;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlApp.Visible = true;
    xlWorkbook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
    xlSheets = xlWorkbook.Sheets as Microsoft.Office.Interop.Excel.Sheets;
    xlNewSheet = xlSheets.Add(xlSheets[1], System.Type.Missing, System.Type.Missing, System.Type.Missing);
    xlNewSheet.Name = "SheetName1";

Respondido el 10 de Septiembre de 13 a las 08:09

ok, but how do I use the the getvstoobject using this code, by the way there is an error in the change on xlnewsheet = slsheets.add - AC25

It looks like to "convert" an interop workbook to a vsto workbook you just do este:

In application-level projects, you can create Microsoft.Office.Tools.Excel.Workbook objects programmatically by using the GetVstoObject method.

Microsoft.Office.Interop.Excel.Workbook nativeWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook;
if (nativeWorkbook != null)
{
    Microsoft.Office.Tools.Excel.Workbook vstoWorkbook = 
        Globals.Factory.GetVstoObject(nativeWorkbook);
}

Respondido el 09 de Septiembre de 13 a las 21:09

I am using a console application to write an excel sheet and to use the getvsto object and hasvstoobject, it dosent seem to pick up even after the excel she is visible with a value in the cell, is it correct to use,Excel.Extensions.WorksheetExtensions.HasVstoObject(newSheet) Iwith newsheet being Microsoft.Office.Interop.Excel.Worksheet I keep getting false as the return value - AC25

@AC25: Do you have to use Microsoft.Office.Tools.Excel.Workbook? I think that this namespace is generally for VSTO AddIns. You should be able to create and manipulate workbooks and worksheets with just the interop. If there something you're trying to do that can't be done with the interop? - seth moore

I would like to use Microsoft.Office.Tools.Excel for workbook or worksheet since I would like to be able to use the controls in it. I tried playing with interop and asked for help in terms of binding an event to one of the controls, there was no answer to that or little documentation, the getvstoobject seems to be tailored to this sort of thing. - AC25

Does it create an workbook or does it interact with that current workbook, bec at the end of the day I am trying to add controls to that workbook vstoWorkbook->worksheet.Controls - AC25

Are you trying to do a document level addin? If so, that's the type of project you should use, not a console app. - seth moore

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.