//方法一调用DBGRID的函数
#include "DBGridEhImpExp.hpp"
void __fastcall TfrmViewReport::cmdSaveAsClick(TObject *Sender)
{
/**************************************************************
函数名:cmdSaveAsClick(TObject *Sender)
用途: 保存报表到本地文件中
修改者:
修改日期:
**************************************************************/
TDBGridEhExportClass ExpClass;
/*
TDBGridEhExportAsText * ExpText=new TDBGridEhExportAsText;
TDBGridEhExportAsCSV * ExpCsv=new TDBGridEhExportAsCSV;
TDBGridEhExportAsHTML * ExpHtml=new TDBGridEhExportAsHTML;
TDBGridEhExportAsRTF * ExpRtf=new TDBGridEhExportAsRTF;
TDBGridEhExportAsXLS * ExpXls=new TDBGridEhExportAsXLS;
*/
String Ext;
SaveDialog1->FileName="ExFile";
if (SaveDialog1->Execute())
{
switch(SaveDialog1->FilterIndex)
{
case 1:
ExpClass=__classid(TDBGridEhExportAsText);
Ext="txt";
break;
case 2:
ExpClass=__classid(TDBGridEhExportAsCSV);
Ext="csv";
break;
case 3:
ExpClass=__classid(TDBGridEhExportAsHTML);
Ext= "htm";
break;
case 4:
ExpClass=__classid(TDBGridEhExportAsRTF);
Ext= "rtf";
break;
case 5:
ExpClass=__classid(TDBGridEhExportAsXLS);
Ext="xls";
break;
Default:
ExpClass=NULL;
Ext="";
}
if(ExpClass!=NULL)
{
// if(UpperCase(Copy(SaveDialog1->FileName,SaveDialog1->FileName.Length()-2,3))!=
if(UpperCase(SaveDialog1->FileName)!=UpperCase(Ext))
{
SaveDialog1->FileName=SaveDialog1->FileName + "." + Ext;
grdDBGridEh1->Selection->SelectAll();//选择所有
// SaveDBGridEhToExportFile(__classid(TDBGridEhExportAsXLS),DBGridEh1,SaveDialog1->FileName,False);
SaveDBGridEhToExportFile(ExpClass,grdDBGridEh1,SaveDialog1->FileName,False);
grdDBGridEh1->Selection->Clear();
String sMsg="文件导出成功!/r/n/r/n"+SaveDialog1->FileName;
Application->MessageBox(sMsg.c_str(),"导出文件",MB_OK+64);
}
}
}
}
//==============================================================================
//方法二,运用OLE
//將DS輸出到Execl
int txDSToExecl(TDataSet *ds)
{
Variant ex,newxls,sh;
try{
ex=CreateOleObject("Excel.Application");//啟動Excel
ex.OlePropertySet("Visible",(Variant)true); //使Excel啟動后可見
newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add");//新建一新工作薄
sh=newxls.OlePropertyGet("ActiveSheet");
}catch(...)
{
ShowMessage("启动Excel出错,可能由于Excel没有正确安装");
return -1;
}
if(ds->IsEmpty())
{
return 1;
}
ds->First();
sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropert ySet("Size",11);
for(int i=0;i<ds->FieldCount;i++)
{
sh.OlePropertyGet("Cells",1,i+1).OlePropertySet("Value",(TVa riant)(ds->Fields->Fields [I]->FieldName));
sh.OlePropertyGet("Cells",1,i+1).OlePropertyGet("Font").OleP ropertySet("Bold",(TVariant)true);
}
int RecCount=0;;
while(!ds->Eof)
{
for(int i=0;i<ds->FieldCount;i++)
{
sh.OlePropertyGet("Cells",RecCount+2,i+1).OlePropertySet("Value",
(TVariant)ds->Fields->Fields[I]->AsString);
}
ds->Next();
RecCount++;
}
return 0;
}
int txDSToExecl(TStringGrid *grid)
{
Variant ex,newxls,sh;
try{
ex=CreateOleObject("Excel.Application");//啟動Excel
ex.OlePropertySet("Visible",(Variant)true); //使Excel啟動后可見
newxls=(ex.OlePropertyGet("Workbooks")).OleFunction("Add");//新建一新工作薄
sh=newxls.OlePropertyGet("ActiveSheet");
}catch(...)
{
ShowMessage("启动Excel出错,可能由于Excel没有正确安装");
return -1;
}
if(grid->RowCount<10 )
{
return 1;
}
//ds->First();
sh.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropert ySet("Size",11);
for(int i=1;i<grid->ColCount;i++)
{
sh.OlePropertyGet("Cells",1,i+1).OlePropertySet("Value",(TVa riant)(grid->Cells[I][0]));// ds->Fields->Fields[I]->FieldName));
sh.OlePropertyGet("Cells",1,i+1).OlePropertyGet("Font").OleP ropertySet("Bold",(TVariant)true);
}
int RecCount=0;;
//while(!ds->Eof)
for (int k=1;k<grid->RowCount;k++)
{
for(int i=1;i<grid->ColCount;i++)
{
sh.OlePropertyGet("Cells",RecCount+2,i+1).OlePropertySet("Value",
(TVariant)grid->Cells[I][k]);//ds->Fields->Fields[I]->AsString);
}
//ds->Next();
RecCount++;
}
return 0;
}
//將DS保存到Execl文件
void txDSToExeclFile(TDataSet *ds,AnsiString FileName)
{
TStringList *sl;
AnsiString sline="";
try{
sl=new TStringList();
ds->First();
for(int i=0;i<ds->FieldCount;i++)
{
sline+=ds->Fields->Fields[I]->FieldName;
if(i!=ds->FieldCount-1)
sline+=",";
}
sl->Add(sline);
sline="";
while(!ds->Eof)
{
for(int i=0;i<ds->FieldCount;i++)
{
if(ds->Fields->Fields[I]->AsString!="")
sline+=ds->Fields->Fields[I]->AsString;
else
sline+="' ";
if(i!=ds->FieldCount-1)
sline+=",";
}
sl->Add(sline);
sline="";
ds->Next();
}
sl->SaveToFile(FileName);
}
__finally{
delete sl;
}