H
huangli_nj
Unregistered / Unconfirmed
GUEST, unregistred user!
我用ado连接一个excel表 然后对数字类型进行动态求和,代码如下:
procedure TForm1.suiButton3Click(Sender: TObject);
var
fieldList,tableList:TStringList;
i,j,k:integer;
begin
if(OpenDialog1.Execute) then
begin
path:='';
tableList:=TStringList.Create;
fieldList:=TStringList.Create;
Edit1.Text:=OpenDialog1.FileName;
path:=OpenDialog1.FileName;
form1.ADOConnection1.Close;
form1.ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+OpenDialog1.FileName
+';Extended Properties=Excel 8.0 ;Persist Security Info=False';
form1.ADOConnection1.Open;
form1.ADOConnection1.GetTableNames(tableList,true);
form1.ADOConnection1.GetFieldNames(tableList.Strings[0],fieldList);
cxGrid1DBTableView1.ClearItems;
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Clear;
for i:=0 to fieldList.Count-1 do
begin
cxGrid1DBTableView1.CreateColumn;
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Add;
end;
for j:=0 to cxGrid1DBTableView1.ColumnCount-1 do
begin
cxGrid1DBTableView1.Columns[j].Caption:=fieldList.Strings[j];
cxGrid1DBTableView1.Columns[j].DataBinding.FieldName:=fieldList.Strings[j];
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Items[j].ItemLink:=cxGrid1DBTableView1.Columns[j];
cxGrid1DBTableView1.Columns[j].Width:=80;
end;
for k:=0 to cxGrid1DBTableView1.ColumnCount-1 do
begin
//问题在这个地方
if(cxGrid1DBTableView1.Columns[k].DataBinding.Field.DataType=ftfloat) then
begin
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems[k].Kind:=sksum;
end
else
begin
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems[k].Kind:=skCount;
end;
end;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select * from ['+tableList.Strings[0]+']');
open;
end;
fieldList.Free;
tableList.Free;
end;
end;
procedure TForm1.suiButton3Click(Sender: TObject);
var
fieldList,tableList:TStringList;
i,j,k:integer;
begin
if(OpenDialog1.Execute) then
begin
path:='';
tableList:=TStringList.Create;
fieldList:=TStringList.Create;
Edit1.Text:=OpenDialog1.FileName;
path:=OpenDialog1.FileName;
form1.ADOConnection1.Close;
form1.ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+OpenDialog1.FileName
+';Extended Properties=Excel 8.0 ;Persist Security Info=False';
form1.ADOConnection1.Open;
form1.ADOConnection1.GetTableNames(tableList,true);
form1.ADOConnection1.GetFieldNames(tableList.Strings[0],fieldList);
cxGrid1DBTableView1.ClearItems;
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Clear;
for i:=0 to fieldList.Count-1 do
begin
cxGrid1DBTableView1.CreateColumn;
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Add;
end;
for j:=0 to cxGrid1DBTableView1.ColumnCount-1 do
begin
cxGrid1DBTableView1.Columns[j].Caption:=fieldList.Strings[j];
cxGrid1DBTableView1.Columns[j].DataBinding.FieldName:=fieldList.Strings[j];
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems.Items[j].ItemLink:=cxGrid1DBTableView1.Columns[j];
cxGrid1DBTableView1.Columns[j].Width:=80;
end;
for k:=0 to cxGrid1DBTableView1.ColumnCount-1 do
begin
//问题在这个地方
if(cxGrid1DBTableView1.Columns[k].DataBinding.Field.DataType=ftfloat) then
begin
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems[k].Kind:=sksum;
end
else
begin
cxGrid1DBTableView1.DataController.Summary.FooterSummaryItems[k].Kind:=skCount;
end;
end;
with adoquery1 do
begin
close;
sql.Clear;
sql.Add('select * from ['+tableList.Strings[0]+']');
open;
end;
fieldList.Free;
tableList.Free;
end;
end;