블로그 이미지
훅크선장

카테고리

분류 전체보기 (362)
사진이야기 (23)
펭귄컴퓨팅 (121)
컴퓨터보안 (84)
절름발이 프로그래머 (59)
하드웨어개조 (23)
멀알려줄까 (35)
홈베이킹&홈쿠킹 (2)
잡다한것들 (15)
Total
Today
Yesterday

달력

« » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

공지사항

태그목록

최근에 올라온 글

http://www.delmadang.com/
델마당의 “강좌, 팁, 정보” 게시판에서 가져온 글입니다.

대웅
(klol)
2009-01-12 오후 11:39:47

이 쓰신 글입니다.
----------------------------------------------------------------------------------

DBGrid자료를 엑셀로 보내는 함수입니다.

uses
      dbgrids, ComObj, Excel2000, Clipbrd;
.....

procedure ToExcle(vDBGrid: TDBGrid);
var
  XL: Variant;
  i,k: integer;
  sData: string;
begin
  try
    XL := CreateOLEObject('Excel.Application');
  except on E: Exception do
  begin
    ShowMessage('Excel OLE object를 오픈할 수 없습니다.'+E.Message);
    Exit;
  end; end;
  try
    XL.WorkBooks.Add; //새로운 페이지 생성
    XL.Visible := False;
    XL.Workbooks[XL.Workbooks.Count].WorkSheets[1].Name := 'Sheet1';
    sData := '';
    for i := 0 to vDBGrid.Columns.Count - 1 do
    begin
      sData := sData+vDBGrid.Columns[i].Title.Caption+#9;
      if vDBGrid.Columns[i].Field.DataType in [ftString,ftMemo,ftWideString] then
      begin
        XL.Workbooks[XL.Workbooks.Count].WorkSheets['Sheet1'].Columns[i+1].Select;
        XL.Selection.NumberFormatLocal := '@';
      end;
    end;
    sData := sData+#$D#$A;

    with vDBGrid do
    begin
      k := 0;
      DataSource.DataSet.First;
      while not DataSource.DataSet.Eof do
      begin
        inc(k);
        for i := 0 to Columns.Count - 1 do
          sData := sData+Columns[i].Field.AsString+#9;
        sData := sData+#$D#$A;
        DataSource.DataSet.Next;
      end;
    end;

    Clipboard.SetTextBuf(PChar(sData));
    XL.Cells[1,1].Pastespecial;
    XL.Range['A1', Chr(64+vDBGrid.Columns.Count)+IntToStr(k)].select;
    XL.Selection.Columns.AutoFit;
    XL.Range['A1', 'A1'].select;
    XL.Visible := True;
  except on E: Exception do
  begin
    ShowMessage('Excel로 자료를 보내는 중 오류가 발생했습니다.'+E.Message);
    XL.Visible := True;
    Exit;
  end; end;
end;

테스트결과 아주 잘 되는군요.

Posted by 훅크선장
, |