Article View: borland.public.cppbuilder.nativeapi
Article #7526Re: Export data to MS Excel
From: Vladimir Afanasyev
Date: Fri, 28 Jan 2005 13:57
Date: Fri, 28 Jan 2005 13:57
64 lines
2479 bytes
2479 bytes
Hi,
If Excel installed you can use late binding for it.
Type Variant can wrap IDispatch interface.
Some sample you can find in BCB Help (near Variant help).
Also follow sample for BCB5:
Note for BCB6 developers: Before building projects set Registers variables in None in page Advanced Compiler (Project options). This way the problem BCB5-BCB6 incompatibility (type Variant) is solved.
//---------
#include <objbase.h>
//...
//...
CoInitialize(NULL);
Variant covFalse((short)false);
Variant vExcel;
Function WorkbooksP("Workbooks");
Function OpenF("Open");
Variant WorkBooks;
Variant WorkBook;
PropertyGet WS("ActiveSheet");
Function SaveAsF("SaveAs");
Function CloseF("Close");
Function QuitF("Quit");
Function NewWorkBook("Add");
PropertyGet GetActiveSheet("ActiveSheet");
bool FieldCanBeString[10]={1,1,1,1,0,0,0,0,0,0};
vExcel=Variant::CreateObject("Excel.Application");
vExcel.Exec(PropertySet("Visible") << Variant((short)true));
WorkBooks=vExcel.Exec(WorkbooksP);
WorkBook=WorkBooks.Exec(NewWorkBook);
Variant WorkSheet=WorkBook.Exec(GetActiveSheet);
Table1->Active=true;
int RC=Table1->RecordCount;
Table1->First();
for(int i=1; i <= RC; ++i)
{
for(int j=0; j < Table1->Fields->Count; ++j)
{
PropertySet Cells("Cells");
if(!FieldCanBeString[j])continue;
WorkSheet.Exec(Cells << i << j+1 << Table1->Fields->Fields[j]->AsString);
}
Table1->Next();
}
Table1->Active=false;
WorkBook.Exec(SaveAsF << "c:\\1.xls");
WorkBook.Exec(CloseF << covFalse);
vExcel.Exec(QuitF);
CoUninitialize();
Vladimir.
Technical Details
Message-ID:
<41fa44ca$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<41f924bc$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,8198285744adbac6 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Afanasyev" <avv@systema.kiev.ua> Sender: "Vladimir Afanasyev" <avv@systema.kiev.ua> Reply-To: "Vladimir Afanasyev" <avv@systema.kiev.ua> Subject: Re: Export data to MS Excel Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 83.218.237.46 X-Forwarded-For: unknown References: <41f924bc$1@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <41fa44ca$1@newsgroups.borland.com> Date: 28 Jan 2005 05:57:30 -0800 X-Trace: newsgroups.borland.com 1106920650 207.105.83.66 (28 Jan 2005 05:57:30 -0800) Lines: 64 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail