T
Mhm dabei ist der Zugriff auf Exel mittels COM doch sehr einfach, wenn man gute Beispiele hat.
Ich habe ganze Packte mit Beispielen für VB/C++/MFC von der MSDN-HP und die Links sind in der FAQ:
Word/Excel über ActiveX fernsteuern
// For optional arguments
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Instantiate Excel
_Application oApp;
oApp.CreateDispatch("Excel.Application");
if (!oApp)
{
AfxMessageBox("Cannot start Excel.");
return;
}
//Get the Workbooks collection so that you can add a new
//workbook
Workbooks oBooks = oApp.GetWorkbooks();
_Workbook oBook = oBooks.Add(vOpt);
//Get the Worksheets collection of the new Workbook so that
//you can get the IDispatch for the first WorkSheet
Worksheets oSheets = oBook.GetWorksheets();
_Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
//*** ADD DATA TO THE WORKSHEET
//Add Headers to Row 1 of the worksheet
Range oRange;
oRange = oSheet.GetRange(COleVariant("A1"), vOpt);
oRange.SetValue(COleVariant("Date"));
oRange = oSheet.GetRange(COleVariant("B1"), vOpt);
oRange.SetValue(COleVariant("Order #"));
oRange = oSheet.GetRange(COleVariant("C1"), vOpt);
oRange.SetValue(COleVariant("Amount"));
oRange = oSheet.GetRange(COleVariant("D1"), vOpt);
oRange.SetValue(COleVariant("Tax"));
//Create a safe array that is NUMROWS x 3 --
//column 1 will contain dates column 2 will contain strings
//and column 2 will contain numbers
COleSafeArray sa;
DWORD dwElements[2];
dwElements[0]= NUMROWS; //Number of rows
dwElements[1]= 3; //Number of columns
sa.Create(VT_VARIANT, 2, dwElements);
//Populate the safe array with the data
long index[2];
long lRow;
COleVariant vTemp;
COleDateTime vDateTime;
CString s;
for(lRow=0;lRow<=NUMROWS-1;lRow++)
{
index[0] = lRow;
//Fill the first column with dates
index[1] = 0;
vDateTime.SetDate(1999, rand()%12, rand()%28);
sa.PutElement(index, (COleVariant)vDateTime);
//Fill the second column with strings
index[1] = 1;
s.Format("ORDR%d", lRow+1000);
vTemp = s;
sa.PutElement(index, vTemp);
//Fill the third column with numbers
index[1] = 2;
vTemp = (long)rand();
sa.PutElement(index, vTemp);
}
//Fill a range, starting at A2 with the data in
//the safe array
oRange = oSheet.GetRange(COleVariant("A2"), vOpt);
oRange = oRange.GetResize(COleVariant((short)NUMROWS),
COleVariant((short)3));
oRange.SetValue(sa);
sa.Detach();
//*** ADD FORMULAS TO THE WORKSHEET
//Fill the fourth column with a formula to compute the
//sales tax. Note that the formula uses a "relative"
//cell reference so that it fills properly.
oRange = oSheet.GetRange(COleVariant("D2"), vOpt);
oRange = oRange.GetResize(COleVariant((long)NUMROWS),
COleVariant((long)1));
oRange.SetFormula(COleVariant("=C2*0.07"));
//*** FORMAT THE WORKSHEET
oRange = oSheet.GetRange(COleVariant("A1"), COleVariant("D1"));
Font oFont = oRange.GetFont();
oFont.SetBold(COleVariant((short)TRUE));//Apply Bold to Headers
oRange = oRange.GetEntireColumn();
oRange.AutoFit(); //AutoFit the columns 1:4
//Make Excel visible and give the user control
oApp.SetVisible(TRUE);
oApp.SetUserControl(TRUE);