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 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据