Excelauswertung mit Typbibliothek excel.h & excel.cpp



  • Hallo!

    Ich moechte eine Exceldatei mit mehreren Reitern in VC++ einlesen und sie an ein Dialog uebergeben. Leider kann ich auf die Zelleninhalte nicht zugreifen und sie einer Textbox zuweisen.

    Was mache ich falsch?

    Excelklassen wurden ueber den MFC-Assistenten -> "Automatisierung" -> "Aus einer Typbibliothek" dem Projekt hinzugefuegt!

    void CTestView::Excelauswertung() 
    {
    	// TODO: Code für die Behandlungsroutine der Steuerelement-Benachrichtigung hier einfügen
    	COleVariant
    	covTrue((short)TRUE),
    	covFalse((short)FALSE),
    	covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
    	// Ausgabevariable ist ein Union
    	VARIANT output;
    
    	// excel-deklaration
    	_Application app;
    	Workbooks pBooks;
    	_Workbook pBook;
    	Worksheets pSheets;
    	_Worksheet pSheet;
    
    	Range range;
    	Font font;
    	Range cols;
    
    	// Start Excel and get Application object
    	if(!app.CreateDispatch("Excel.Application"))
    	{
    		AfxMessageBox("Excel kann nicht gestartet werden und kann nicht auf das Application object zugreifen!");
    		return;
    	}
    
    	app.SetVisible(FALSE);	// EXCEL wird nicht geoeffnet!
    
    	// Get a new Workbook
    	pBooks = app.GetWorkbooks();
    	pBook = pBooks.Open("Y:\\test.xls", covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);	
    
    	// Get the first sheet
    	pSheets = pBook.GetSheets();
    
    	// Zugriff auf das 1. Tabellenblatt "Allgemein"
    	pSheet = pSheets.GetItem(COleVariant("Allgemein"));
    
    	// Zugriff auf Zelle B13
    	range = pSheet.GetRange(COleVariant("B13"), COleVariant("B13"));
    
    	// Rückgabe von GetValue als VARIANT-Struktur in output
    	output = range.GetValue(COleVariant("B13"));
    
    	//AfxMessageBox(output);
             // HIER SOLLTE DER ZELLENINHALT WEITER VERARBEITET WERDEN, NUR
             // LEIDER BEKOMME ICH NICHTS ZURUECK! -???
    
    	// Quit Excel and release the IDispatch pointer we used to automate
    	pSheet.ReleaseDispatch();
    	pSheets.ReleaseDispatch();
    	pBook.ReleaseDispatch();
    	pBooks.ReleaseDispatch();
    	app.ReleaseDispatch();
    	//app.Quit();
    
    	//ShowFields();
    }
    


  • So, jetzt habe ich meinen Fehler gefunden: 👍

    // Get the first sheet 
        pSheets = pBook.GetSheets();
    

    RICHTIG sollte es so sein:

    // Get the first sheet 
        pSheets = pBook.GetWorksheets();
    

    Jetzt habe ich zumindestens den Zellenwert aus Excel schon einmal im Debugger eingefangen. 🙂

    So, jetzt muss der Wert halt noch von einen VARIANT (COleVariant) in einen String (CString) konvertiert werden...

    ABER wie funktioniert das, damit ich den Wert meiner Membervariablen uebergeben kann? 😕



  • Hallo!

    Meine Anwendung funktioniert bisher ganz gut, hat aber leider beim beenden eine kleine Macke, weil bei jedem Programmstart ein neuer Excel.exe-Prozess gestartet wird, aber leider nicht mehr beendet, obwohl folgedes im Code drinsteht:

    app.ReleaseDispatch(); 
    app.Quit();
    

    Was mache ich hinsichtlich bei meinem Programm beenden falsch?



  • sorry! doppelpost



  • sorry! doppelpost


Anmelden zum Antworten