Q&A

  • EXCEL -> StringGrid (속도문제때문)
procedure TForm.ButtonClick(Sender: TObject);
var
        ExcelApp, ExcelBook, ExcelSheet : Variant;
        I, J, s : Integer; // 순환변수
begin
        try                                                                     //엑셀이 설치되었있을 경우만 가능
           ExcelApp := CreateOLEObject('Excel.Application');                    //엑셀을 실행
        except
           ShowMessage('Excel이 설치되어 있지 않습니다!!!');
           Exit;
        end;

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

           if OpenDialog1.Execute then begin                                    //엑셀 통합문서 열기
              ExcelBook := ExcelApp.WorkBooks.Open(OpenDialog1.FileName);
              ExcelBook := ExcelApp.WorkBooks.item[1];                          //워크 쉬트 설정
              ExcelSheet := ExcelBook.Worksheets.Item[1];                       //일단 개별문서는 sheet1으로 고정

              SG.RowCount := ExcelSheet.UsedRange.Rows.Count + 1;               //StringGrid 초기화 (Title은 고려하지 않았습니다.)
              SG.ColCount := ExcelSheet.UsedRange.Columns.Count + 1;

              S := 0;
              for I := 1 to EXCELSheet.UsedRange.Rows.count do begin
                  S := i - 2;
                  for J := 0 to ExcelSheet.UsedRange.Columns.count do begin
                      if j = 0 then begin
                         if s > 0 then
                            SG.Cells[J,I] := intTostr(i);                       //스트링그리드에 뿌리기
                      end
                      else
                         SG.Cells[J,I] := VarToStr(ExcelSheet.Cells[I,J]);      //스트링그리드에 뿌리기
                      end;
                  end;
                  ExcelApp.WorkBooks.Close;
                  ExcelApp.quit;
                  ExcelApp := unassigned;
              end;
        except
           on err : exception do begin
              ExcelApp.WorkBooks.Close;
              ExcelApp.quit;
              ExcelApp := unassigned;
              ShowMessage('작업이 취소되었습니다. Data확인요망-'+err.message);
           end;
        end;
           ShowMessage(IntToStr(ExcelSheet.UsedRange.Rows.Count) + '건의 자료를 변환하였습니다');
end;


위의 코딩으론 도저히 개발했다고 할수 없네요...

속도 문제도 그렇고 다운되는 현상도 그렇고...

스트림을 쓰면 될것같은데...

아시는분 리플 좀 부탁드립니다.
2  COMMENTS
  • Profile
    박성훈 2003.01.21 08:57

    안녕하세요?
    전에 최용일 님께서 올려주셨던 것을 퍼왔습니다. Ole없이 엑셀파일 만드는 예제입니다. 참고하세요.

    const
    CXlsBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0);
    CXlsEof: array[0..1] of Word = ($0A, 00);
    CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
    CXlsNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
    CXlsRk: array[0..4] of Word = ($27E, 10, 0, 0, 0);

    procedure XlsBeginStream(XlsStream: TStream; const BuildNumber: Word);
    begin
    CXlsBof[4] := BuildNumber;
    XlsStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
    end;

    procedure XlsEndStream(XlsStream: TStream);
    begin
    XlsStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
    end;

    procedure XlsWriteCellRk(XlsStream: TStream; const ACol, ARow: Word; const AValue: Integer);
    var
    V: Integer;
    begin
    CXlsRk[2] := ARow;
    CXlsRk[3] := ACol;
    XlsStream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
    V := (AValue shl 2) or 2;
    XlsStream.WriteBuffer(V, 4);
    end;

    procedure XlsWriteCellNumber(XlsStream: TStream; const ACol, ARow: Word; const AValue: Double);
    begin
    CXlsNumber[2] := ARow;
    CXlsNumber[3] := ACol;
    XlsStream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
    XlsStream.WriteBuffer(AValue, 8);
    end;

    procedure XlsWriteCellLabel(XlsStream: TStream; const ACol, ARow: Word; const AValue: string);
    var
    L: Word;
    begin
    L := Length(AValue);
    CXlsLabel[1] := 8 + L;
    CXlsLabel[2] := ARow;
    CXlsLabel[3] := ACol;
    CXlsLabel[5] := L;
    XlsStream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
    XlsStream.WriteBuffer(Pointer(AValue)^, L);
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
    FStream: TFileStream;
    I, J: Integer;
    begin
    FStream := TFileStream.Create('J:e.xls', fmCreate);
    try
    XlsBeginStream(FStream, 0);
    for I := 0 to 99 do
    for J := 0 to 99 do
    begin
    XlsWriteCellNumber(FStream, I, J, 34.34);
    // XlsWriteCellRk(FStream, I, J, 3434);
    // XlsWriteCellLabel(FStream, I, J, Format('Cell: %d,%d', [I, J]));
    end;
    XlsEndStream(FStream);
    finally
    FStream.Free;
    end;
    end;

    출처 by : Borland CodeCentral, Azret Botash

  • Profile
    Sean 2003.01.21 10:33
    아고~~
    설명 좀 넣어 주세요...
    조금 어렵네요