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 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题