Excel-Vorlage öffnen
-
Hallo Leute
Ich möchte aus einem MFC Dialog eine Excel-Vorlage öffnen.
Bisher habe ich mit folgenden Code versucht und es funktioniert
Es wird halt nur ein neues Workbook geöffnet.CString szTemp,str; CWorkbooks oBooks; CWorksheets oSheets; CWorksheet oSheet; CRange oRange; COleVariant covOptional(DISP_E_PARAMNOTFOUND,VT_ERROR); // If you have not created Excel, create a new instance. if (oExcel.m_lpDispatch == NULL) { oExcel.CreateDispatch("Excel.Application"); } // Show Excel to the user. oExcel.put_Visible(TRUE); oExcel.put_UserControl(TRUE); // Add a new workbook and get the first worksheet in that book. oBooks = oExcel.get_Workbooks(); // <<== oBook = oBooks.Add(covOptional); oSheets = oBook.get_Worksheets(); oSheet = oSheets.get_Item(COleVariant((short)1)); // Get a range of data. oRange = oSheet.get_Range(COleVariant("A1"),covOptional); oRange = oRange.get_Resize(COleVariant((short)m_MaxRow),COleVariant((short)m_MaxCol)); COleSafeArray saRet; DWORD numElements[2]; numElements[0] = m_MaxRow; numElements[1] = m_MaxCol; long index[2]; // Create a BSTR or double safe array. saRet.Create(VT_BSTR,2,numElements); // Fill the array with data. for (int iRow = 1; iRow <= m_MaxRow; iRow++) { for (int iCol = 1; iCol <= m_MaxCol; iCol++) { index[0]=iRow-1; index[1]=iCol-1; if(iRow!=1) szTemp="TEst"; else szTemp="TEST"; BSTR bstr = szTemp.AllocSysString(); saRet.PutElement(index,bstr); SysFreeString(bstr); } } // Send the array to Excel. oRange.put_Value(covOptional,COleVariant(saRet));Wie kann ich nun eine bestehende Datei öffnen ??
danke und Gruß
Linus
-
Einfach mit CoGetObject!
http://msdn.microsoft.com/en-us/library/ms678805(VS.85).aspx
-
Danke für die schnelle Antwort
Habe es aber mit Book._open() hinbekommen.
Jetzt Habe ich nur ein anderes Problem:
Ich möchte in einem anderen Dialog wieder auf Excel zugreifen.
Wenn ich jetzte Application.h etc einbinde sagt er mir dass ich
diese Datei schon eingebunden habe. Aber zugreifen kann ich auf die
Variablen und Funktionen nicht ???

Gruß Linus