XIAOHUA_123 2013-07-18 08:33 采纳率: 0%
浏览 1311

delphi excel导入stringgrid后时间格式转换有越界

![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
请各位大仙帮助解决为谢!

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀