如题

解决方案 »

  1.   

    var
    OpenDialog1:TOpenDialog;
    ExcelApp:Variant;
    expath,str:String;
    ExCountRow,ExCountCol:integer; 
    begin
    OpenDialog1:=TOpenDialog.Create(Application);
      with OpenDialog1 do
      begin
        Title:='xls選擇';
        Filter:='Excel File(*.xls)|*.xls';
        DefaultExt:='*.xls';
      end;
    if OpenDialog1.Execute then
      begin
        expath:=opendialog1.FileName;
        opendialog1.Free;
        opendialog1:=nil;
        application.ProcessMessages;
        try
          Screen.Cursor:=crhourglass;
          ExcelApp:=CreateOleObject('excel.application');
          ExcelApp.visible:=false;
          ExcelApp.workbooks.open(expath);
          ExCountRow:=0;
          while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do
            begin
              Inc(ExCountRow);
            end;      ExCountCol:=0;
          while not(length(ExcelApp.cells[1,ExCountCol+1])=0) do
            begin
              Inc(ExCountCol);
            end;
        strngrd1.RowCount:=ExCountRow+1;
          for i:=1 to strngrd1.RowCount do
          begin
            for j:=1 to strngrd1.ColCount do
            begin
              strngrd1.Cells[j,i]:=ExcelApp.cells[i+1,j].value;
             end;
          end;     ExcelApp.workbooks.close;
        Excelapp.quit;
        screen.Cursor:=crdefault;
      finally
        ExcelApp.workbooks.close;
        Excelapp.quit; 
        screen.Cursor:=crdefault; 
      end;