Q&A

  • 엑셀 레코드 카운터 하는 방법좀 알려주세요..??
procedure TForm9.SpeedButton1Click(Sender: TObject);
var
v : Variant ;
iRow, iCol : integer ;
CNT : integer;

begin
try
v:= CreateOleObject('Excel.application');  
except  
  ShowMessage('Excel이 설치되어 있지 않습니다!!!');
  Exit;
end;

if OpenDialog1.Execute then
begin
  v.workbooks.open(OpenDialog1.FileName);
end
else exit;
cnt := strToint(v.ActiveSheet.UsedRange.Rows.Count); // 엑셀 레코드 카운트를
위처럼 하면 제대로 카운트가 안됩니다. 실제 레코드 보더 몇백게씩 더 카운트 되는데..
다른 방법은 없나요?
꼭좀 도와주세요..^^
1  COMMENTS
  • Profile
    Crazy 2003.11.02 00:30
    var
         ExcelApp, ExcelSheet, ExcelFile : Variant;
         iRow, iCol, iXLRows, iXLCols : Integer;
         ExcelValue : Variant;
    begin
         SpeedButton_Save.Enabled := False;
         prc_DisMsg( Panel_Msg, clBlack, 'Excel Data Loading...' ) ;

         try
              ExcelApp := CreateOLEObject( 'Excel.Application' );
         except
              MessageDlg( 'Excel 이 설치되지 않았습니다!', mtInformation, [mbOk], 0 );
              Exit;
         end;

         try
              ExcelApp.Visible       := False;
              ExcelApp.DisplayAlerts := False;

              OpenDialog1.InitialDir := ExtractFilePath( Application.ExeName );
              if OpenDialog1.Execute then
              begin
                   ExcelFile  := ExcelApp.WorkBooks.Open( OpenDialog1.FileName );
                   ExcelFile  := ExcelApp.WorkBooks.Item[1];
                   ExcelSheet := ExcelFile.Worksheets.Item[1];

                   iXLCols := ExcelSheet.UsedRange.Columns.Count;
                   iXLRows := ExcelSheet.UsedRange.Rows.Count;

                   ProgressBar1.Max := iXLCols * iXLRows ;
                   ProgressBar1.Position := 0 ;

                   with dxMemData_Main do
                   begin
                        Close;
                        Open;

                        for iRow := 2 to iXLRows do
                        begin
                             Append;
                             for iCol := 1 to iXLCols  do
                             begin
                                 dxMemData_Main.Fields[iCol].AsString := EXcelSheet.Cells[iRow, iCol];
                                 ProgressBar1.Position := ProgressBar1.Position + 1;
                            end;
                        end;
                   end;

                   ProgressBar1.Position := 0 ;
                   ExcelApp.WorkBooks.Close;
                   ExcelApp.Quit;
                   Unassigned;
              end;

              prc_DisMsg( Panel_Msg, clBlack, 'Excel Loading Complete!' ) ;
              SpeedButton_Save.Enabled := True;
         except
              on err:exception do
              begin

                   ProgressBar1.Position := 0 ;
                   ExcelApp.WorkBooks.Close;
                   ExcelApp.Quit;
                   ExcelApp := Unassigned;
                   MessageDlg( '작업이 취소되었습니다. Data확인요망-'+err.message, mtInformation, [mbOk], 0 );
              end;
         end;