第二种方法用第三方控件:Ehlib 的DBGridEh,它有个方法直接输出EXcel文件如下例,还有DevelopExpress的dxDBGrid控件也有个方法:SaveToXLS
int ExcuteCode;
TDBGridEhExportAsXLS *ExportAsXLS;
TMetaClass *test;
Variant my_excel,all_workbooks,my_workbook,my_worksheet;
Variant my_range;
//Variant filename;
// AnsiString filename="d:\\book1.xls";
AnsiString columname,residue,cellname,rangename;
int i,j=1;
int count;
SaveDialog1->Filter="*.xls";
SaveDialog1->Title = "Save File As Excel";
if(SaveDialog1->Execute())
{
if (!SaveDialog1->FileName.IsEmpty())
{
if(FileExists(SaveDialog1->FileName))
{
ExcuteCode=MessageDlg("文件名已存在,复盖吗?",mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel,0);
switch(ExcuteCode)
{
case mrYes:
ExportAsXLS=new TDBGridEhExportAsXLS();
test=ExportAsXLS->ClassType();
SaveDBGridEhToExportFile(test,DBGridEh1,SaveDialog1->FileName,true);
delete ExportAsXLS;
break;
case mrNo:
break;
case mrCancel:
break;
}
}
else
{
ExportAsXLS=new TDBGridEhExportAsXLS();
test=ExportAsXLS->ClassType();
SaveDBGridEhToExportFile(test,DBGridEh1,SaveDialog1->FileName,true);
delete ExportAsXLS;
}
int ExcuteCode;
TDBGridEhExportAsXLS *ExportAsXLS;
TMetaClass *test;
Variant my_excel,all_workbooks,my_workbook,my_worksheet;
Variant my_range;
//Variant filename;
// AnsiString filename="d:\\book1.xls";
AnsiString columname,residue,cellname,rangename;
int i,j=1;
int count;
SaveDialog1->Filter="*.xls";
SaveDialog1->Title = "Save File As Excel";
if(SaveDialog1->Execute())
{
if (!SaveDialog1->FileName.IsEmpty())
{
if(FileExists(SaveDialog1->FileName))
{
ExcuteCode=MessageDlg("文件名已存在,复盖吗?",mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel,0);
switch(ExcuteCode)
{
case mrYes:
ExportAsXLS=new TDBGridEhExportAsXLS();
test=ExportAsXLS->ClassType();
SaveDBGridEhToExportFile(test,DBGridEh1,SaveDialog1->FileName,true);
delete ExportAsXLS;
break;
case mrNo:
break;
case mrCancel:
break;
}
}
else
{
ExportAsXLS=new TDBGridEhExportAsXLS();
test=ExportAsXLS->ClassType();
SaveDBGridEhToExportFile(test,DBGridEh1,SaveDialog1->FileName,true);
delete ExportAsXLS;
}