1.我是从excel中导入数据到stringgrid里的,一共400多条,也得等上10秒钟左右,以下是我用的代码,不过是参考社区某篇文章的,具体记不清是哪篇了,在此致以衷心感谢。 2.另外,我想加进度条,可是max值不太好设,取完了才知道有多少条记录,请诸位大虾帮忙解决,多谢! 3.初来乍到,不知道这里的规矩,也不太清楚怎样散分,也顾不得看社区帮助了,请包涵。 begin sfilename:=opendialog1.FileName; SetCurrentDirectory(PChar(ExtractFilePath(Application.ExeName))); Try ExcelApplication1.Connect; Except MessageDlg('aaaaaaaaaaaaaaa',mtError, [mbOk], 0); Abort; End; excelapplication1.Visible[0]:=true; ExcelApplication1.Caption:='Excel Application'; try excelapplication1.Workbooks.Open(sfilename, null,null,null,null,null,null,null,null,null,null,null,null,0); except begin ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit; showmessage('出现异常情况...关闭'); exit; end; end; ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); //取字段名:EXCEL第一行 for j:=1 to 256 do if trim(excelworksheet1.cells.item[1,j])<>'' then begin StringGrid1.ColCount := j; c := j; SetLength(fields,j); fields[j-1] := ExcelWorksheet1.Cells.Item[1,j]; end; //开始从EXCEL中取数,放到stringgrid1中,取完数后关闭EXCEL for i:=1 to 65536 do//最大取值 for j:=0 to c-1 do if trim(excelworksheet1.cells.item[i,1])<>'' then begin stringgrid1.rowCount := i+1; Str := ExcelWorksheet1.Cells.Item[i,j+1]; stringgrid1.Cells[j,i] := Str; end else begin label1.caption := '共有记录:' + IntToStr(i-2) + '条'; Gauge1.Maxvalue:=i-2; ExcelApplication1.Disconnect; ExcelApplication1.Quit; ExcelApplication1.Free; ExcelWorksheet1.Free; ExcelWorkbook1.Free; exit; end; end;
经过测试 procedure TForm1.Button1Click(Sender: TObject); var I:Integer; T :LongInt; begin with ADOQuery1 do begin Close; SQL.Text:='Select * from Temp'; Open; T := GetTickCount; with StringGrid1 do begin RowCount := RecordCount +1; ColCount := Fields.Count +1; for I:=0 to Fields.Count -1 do Cols[I].BeginUpdate; // 加上这句 ,节省一半时间 while not Eof do begin Cells[0, RecNo] := IntToStr(RecNo); for I:=0 to Fields.Count -1 do Cells[I+1, RecNo] := Fields[I].AsString; Next; end; for I:=0 to Fields.Count -1 do Cols[I].EndUpdate;// 加上这句 ,节省一半时间 Close; Label1.Caption := IntToStr( GetTickCount -T); end; end; end;
就在查询是限定一下一次捞出记录的量
推荐的是100~1000
你的不是STRINGGRID慢而是数据库里找数据慢,原因有好多,可能就是你的数据查询时的问题
2.另外,我想加进度条,可是max值不太好设,取完了才知道有多少条记录,请诸位大虾帮忙解决,多谢!
3.初来乍到,不知道这里的规矩,也不太清楚怎样散分,也顾不得看社区帮助了,请包涵。
begin
sfilename:=opendialog1.FileName;
SetCurrentDirectory(PChar(ExtractFilePath(Application.ExeName)));
Try
ExcelApplication1.Connect;
Except
MessageDlg('aaaaaaaaaaaaaaa',mtError, [mbOk], 0);
Abort;
End; excelapplication1.Visible[0]:=true;
ExcelApplication1.Caption:='Excel Application';
try excelapplication1.Workbooks.Open(sfilename,
null,null,null,null,null,null,null,null,null,null,null,null,0);
except
begin
ExcelApplication1.Disconnect;//出现异常情况时关闭
ExcelApplication1.Quit;
showmessage('出现异常情况...关闭');
exit;
end;
end; ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); //取字段名:EXCEL第一行
for j:=1 to 256 do
if trim(excelworksheet1.cells.item[1,j])<>'' then
begin
StringGrid1.ColCount := j;
c := j;
SetLength(fields,j);
fields[j-1] := ExcelWorksheet1.Cells.Item[1,j];
end;
//开始从EXCEL中取数,放到stringgrid1中,取完数后关闭EXCEL
for i:=1 to 65536 do//最大取值
for j:=0 to c-1 do
if trim(excelworksheet1.cells.item[i,1])<>'' then
begin
stringgrid1.rowCount := i+1;
Str := ExcelWorksheet1.Cells.Item[i,j+1];
stringgrid1.Cells[j,i] := Str;
end
else
begin
label1.caption := '共有记录:' + IntToStr(i-2) + '条';
Gauge1.Maxvalue:=i-2;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
exit;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I:Integer;
T :LongInt;
begin
with ADOQuery1 do
begin
Close;
SQL.Text:='Select * from Temp';
Open;
T := GetTickCount;
with StringGrid1 do
begin
RowCount := RecordCount +1;
ColCount := Fields.Count +1;
for I:=0 to Fields.Count -1 do Cols[I].BeginUpdate; // 加上这句 ,节省一半时间
while not Eof do
begin
Cells[0, RecNo] := IntToStr(RecNo);
for I:=0 to Fields.Count -1 do
Cells[I+1, RecNo] := Fields[I].AsString;
Next;
end;
for I:=0 to Fields.Count -1 do Cols[I].EndUpdate;// 加上这句 ,节省一半时间
Close;
Label1.Caption := IntToStr( GetTickCount -T);
end; end;
end;
我把数据先读到内存,然后往stringgrid里写,还是很慢啊
我的数据从ini文件里去出来
大概700多条记录