怎么把cxGrid数据导出到EXCEL文档?
各位大虾,本人初次接触BCB,碰到了一个问题,请教各位:我的界面上有五个cxgrid网格,现在我要一次性把他们的数据全部导出到EXCLE里面去,我能导出一个,但是要连续导其他的话,最后EXCLE里面的数据就只有最后一个数据表的数据了。怎么办啊?很急得要,HELPING。。。。。。。。。。。。。。。。。
[解决办法]
// 你参考一下,这是一个数据表导至 Excel 的函数
void __fastcall ADOQuery2Excel (TADOQuery* ADOQuery, String elxFilename, bool CloseExcel)
{
Variantmy_excel;
try
{
my_excel = Variant::GetActiveObject( "excel.application ");
}
catch (...)
{
try
{
my_excel = Variant::CreateObject( "excel.application ");
}
catch (...)
{
ShowMessage( "GetExcel failed. ");
return;
}
}
my_excel.OlePropertySet( "Visible ",(Variant)true);
// ----------------------------
Variantall_workbooks;
Variantmy_workbook1;
//-- Get workbooks collection
all_workbooks= my_excel.OlePropertyGet( "Workbooks ");
//-- Set number of worksheets to 1
my_excel.OlePropertySet( "SheetsInNewWorkbook ",(Variant)1);
//-- Create a new workbook
my_workbook1= all_workbooks.OleFunction( "Add ");
// ---------------------------
Variantmy_workbook;
Variantmy_worksheet;
Variantmy_range;
PropertyGet Range( "Range ");
PropertySet SetValue( "Value ");
PropertySet SetFormula( "Formula ");
PropertyGet GetValue( "Value ");
PropertyGet GetFormula( "Formula ");
my_workbook= my_excel.OlePropertyGet( "ActiveWorkbook ");
my_worksheet= my_workbook.OlePropertyGet( "ActiveSheet ");
// ********************* 表头设置 **********************
ADOQuery-> Open();
int FieldCount = ADOQuery-> FieldCount;
String Column = " ";
String X1 = "A ";
String Y1 = "1 ";
for ( int i = 0; i < FieldCount; i ++ )
{
Column = X1 + Y1;
Range.ClearArgs();
SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range < < Column);
my_range.Exec(SetValue < < ADOQuery-> Fields-> Fields[i]-> DisplayLabel);
my_excel.OlePropertyGet( "Columns ", i+1).OlePropertySet( "ColumnWidth ", 15);
X1[1] = X1[1] + 1;
}
String Item = " ";
String X2 = "A ";
String Y2 = "2 ";
Y2 = "2 ";
ADOQuery-> First();
while ( !ADOQuery-> Eof )
{
X2 = "A ";
Item = X2 + Y2;
for ( int i = 0; i < FieldCount; i ++ )
{
Item = X2 + Y2;
Range.ClearArgs();
SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range < < Item);
my_range.Exec(SetValue < < ADOQuery-> Fields-> Fields[i]-> Value);
X2[1] = X2[1] + 1;
}
ADOQuery-> Next();
Y2 = IntToStr ( StrToInt ( Y2 ) + 1 );
}
ADOQuery-> Close();
Procedure SaveAs( "SaveAs ");
my_workbook = my_excel.OlePropertyGet( "ActiveWorkbook ");
my_workbook.Exec(SaveAs < < elxFilename);
my_workbook.OlePropertyGet( "Close ");
if ( CloseExcel == true)
my_excel.OleFunction( "Quit ");
}
//---------------------------------------