![CSDN移动问答][1]excel导入stringgrid过程:
procedure TForm1.btn1Click(Sender: TObject);
var
ExcelApp,sheet:Variant;
ExcelRowCount,ExcelcolCount,i,j,m,n,k:Integer;
str,str1:string;
wbk:OleVariant;
EclApp, WorkBook : Variant;
begin
if dlgOpen1.Execute then
begin
try
EclApp := CreateOleObject ('Excel.Application');
WorkBook := CreateOleObject ('Excel.Sheet');
except
ShowMessage ('您的计算机上没有 Microsoft Excel!');
Exit;
end;
ExcelApp:=CreateOleObject ('Excel.Application');
//建立Excel对象
WBK := ExcelApp.WorkBooks.Open(dlgopen1.FileName); //打开指定文件
sheet := ExcelApp.WorkSheets[1];
ExcelApp.Visible := False; //不显示Excel
ExcelRowCount := ExcelApp.WorkSheets[1].UsedRange.Rows.Count; //行数
ExcelColCount := ExcelApp.WorkSheets[1].UsedRange.Columns.Count; //列数
// showmessage(inttostr(ExcelColCount));
strngrd1.RowCount:= ExcelRowCount+1;
strngrd1.ColCount:=ExcelcolCount+1;
with strngrd1 do
begin
for n:=0 to ExcelcolCount -1 do
begin
if n<=ExcelcolCount then
begin
Cells[n,0]:=IntToStr(n+1);
end;
end;
for k:=0 to ExcelRowCount do
begin
if k<=ExcelRowCount then
begin
Cells[0,k]:=IntToStr(k+1);
end;
end;
end;
pb1.Visible:=True;
pb1.Max:=ExcelRowCount* ExcelcolCount;
m:=0;
for i := 1 to ExcelRowCount do
begin
for j := 1 to ExcelColCount do
begin
Application.ProcessMessages;
str := sheet.cells[i,j];
if LeftStr(Trim(sheet.cells[i,j]),1)='.' then
begin
str1:='0'+str;
strngrd1.Cells[j,i] :=FormatDateTime('hh:mm:ss',FloatToDateTime(StrToFloat(Trim(str1))));;//这里做了转换
;
;
end else
begin
strngrd1.Cells[j,i] := str;
end;
inc(m);
end;
pb1.Position:=m;
end;
WBK.Close(SaveChanges := True);
ShowMessage('表格导入完成');
导入excel表,时间栏结果:
23:45:23
9.8923611111111E-02
请各位大仙帮助解决为谢!