Dim xlApp as Object
dim xlWb as Object
Set xlApp=CreateObject("Excel.Application")
Set xlWb=xlApp.Workbooks.Add
xlWb.SaveAs "bla"
xlApp.Quit
Gruss EP
Siegfried Brandt schrieb:
Hallo Siegried,
schau 'mal in Google unter Oleautomation oder COM. Für Delphi von
Borland gibt es darüber eine eigene newsgroup.
Gruss
Frank-R.
Here's the source code to create a new workbook:
** Automate Excel to Create and Format a New Workbook **
This example automates Microsoft Excel to create a new workbook and transfer
data to a worksheet in the new workbook using arrays. The number of rows in
the resulting data is determined at run time.
1. Start a new dialog-based MFC AppWizard EXE project named "ExcelArrays" .
2. Using ClassWizard, add the wrapper classes for the Excel type library.
3. Add a button to the dialog resource IDD_EXCELARRAYS_DIALOG and add the
following code to the button's handler in ExcelDataDlg.cpp:
#define NUMROWS 20
void CExcelArraysDlg::OnRun()
{
// 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);
}
4. Add the following includes to ExcelArraysDlg.cpp:
#include "Excel8.h" // or "Excel9.h" for Excel 2000
5. Modify CExcelArraysApp::InitInstance() in ExcelArrays.cpp to start COM
services:
if(!AfxOleInit())
{
AfxMessageBox("Cannot initialize COM services.");
return FALSE;
}
6. Build and run the application. Click the button you added to the dialog
box. When the Automation code finishes running, you see a new workbook in
Microsoft Excel containing data.
The sample above is taken from the MSDN article
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dno2kta/htm
l/offaut.asp
- or -
http://msdn.microsoft.com/library/en-us/dno2kta/html/offaut.asp?frame=true
If you want to create the Excel file directly, look here:
- How to Create a BIFF5 File
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q150447
- Records Needed to Make a BIFF5 File Microsoft Excel Can Use
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q147732
- Support for the Microsoft Office Binary File Formats
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q239653
- Determine the Version of a Microsoft Excel Workbook
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178605
Hope this helps
Philipp
Siegfried Brandt schrieb in Nachricht
<6a8ceb3d.02091...@posting.google.com>...