gpt4 book ai didi

excel - 快速加载 Excel 电子表格的方法

转载 作者:行者123 更新时间:2023-12-03 14:50:54 26 4
gpt4 key购买 nike

我正在使用此代码加载仅包含数字的 Excel 电子表格。但将整个文件加载到 stringgrid 需要太长时间。 ,有人知道更快的方法吗?

procedure sh1(SheetIndex:integer);
Var
Xlapp1, Sheet:Variant ;
MaxRow, MaxCol,X, Y:integer ;
str:string;
begin
Str:=trim(form2.OpenDialog1.FileName);

XLApp1 := createoleobject('excel.application');
XLApp1.Workbooks.open(Str) ;

Sheet := XLApp1.WorkSheets[SheetIndex] ;

MaxRow := Sheet.Usedrange.EntireRow.count ;
MaxCol := sheet.Usedrange.EntireColumn.count;

form2.stringgrid1.RowCount:=maxRow+1;
form2.StringGrid1.ColCount:=maxCol+1;
for x:=1 to maxCol do
for y:=1 to maxRow do
form2.stringgrid1.Cells[x,y]:=sheet.cells.item[y,x].value;

XLApp1.Workbooks.close;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
if opendialog1.Execute then begin
stringgrid1.Visible:=true;
sh1(1);
end;
end;

最佳答案

您可以尝试将整个范围复制到变体数组中。类似的东西

procedure sh1(SheetIndex:integer);
Var
Xlapp1, Sheet:Variant ;
MaxRow, MaxCol,X, Y:integer ;
str:string;
arrData:Variant;
begin
Str:=trim(form1.OpenDialog1.FileName);

XLApp1 := createoleobject('excel.application');
XLApp1.Workbooks.open(Str) ;

Sheet := XLApp1.WorkSheets[SheetIndex] ;

MaxRow := Sheet.Usedrange.EntireRow.count ;
MaxCol := sheet.Usedrange.EntireColumn.count;

//read the used range to a variant array
arrData:= Sheet.UsedRange.Value;

form1.stringgrid1.RowCount:=maxRow+1;
form1.StringGrid1.ColCount:=maxCol+1;

for x:=1 to maxCol do
for y:=1 to maxRow do
//copy data to grid
form1.stringgrid1.Cells[x,y]:=arrData[y, x]; // do note that the indices are reversed (y, x)

XLApp1.Workbooks.close;
end;

关于excel - 快速加载 Excel 电子表格的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14831463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com