Q&A

  • 엑셀 화일로 저장하는 방법좀 가르쳐 주세요
쿼리구문을

엑셀 화일로 저장하는 방법좀 가르쳐 주세요
5  COMMENTS
  • Profile
    major 2002.04.13 02:08
    그러시면 CSV파일로 저장을 하시면 될 것 같네여
    여기에서 바로 CSV를 검색하시면 금방 답을 얻으실수가 있을 겁니다.
    그럼 즐프하시고요..


  • Profile
    gasina 2002.04.13 02:12
  • Profile
    김동화 2002.04.13 01:52
    {
    uses절에 ComObj를 삽입해야 함.
    컴포넌트 : Query1, DataSource1, DBGrid1,
              Button1(오픈), Button2(변환), Button3(종료)
    }

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls,
    Forms, Dialogs, Grids, DBGrids, Db, DBTables, StdCtrls,
    ComObj;

    type
    TForm1 = class(TForm)
       Button1: TButton;
       DataSource1: TDataSource;
       Query1: TQuery;
       DBGrid1: TDBGrid;
       Button2: TButton;
       Button3: TButton;
       procedure Button1Click(Sender: TObject);
       procedure Button2Click(Sender: TObject);
       procedure FormShow(Sender: TObject);
       procedure Button3Click(Sender: TObject);
    private
       { Private declarations }
    public
       { Public declarations }
    end;

    var
    Form1: TForm1;
    procedure ExcelConv(AQuery: TQuery);//엑셀변환 프로시져

    implementation

    {$R *.DFM}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      with Query1 do begin
         DatabaseName := 'DBDEMOS';
         Close;
         SQL.Clear;
         SQL.Add('select * from Customer');
         SQL.Add('order by CustNo');
         Open; //엑셀로 변환할 데이타 오픈
      end;
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      DataSource1.Enabled := False;
      ExcelConv(Query1); //엑셀변환 프로시져 호출
      DataSource1.Enabled := True;
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
      Close;
    end;

    procedure TForm1.FormShow(Sender: TObject);
    begin
      DataSource1.DataSet := Query1;
      DBGrid1.DataSource := DataSource1;
    end;

    procedure ExcelConv(AQuery: TQuery);
    var
      XL, XArr, XTitle: Variant;
      i, j, k: Integer;
    begin
      with AQuery do begin
         //타이틀 처리변수
         XTitle := VarArrayCreate([1, FieldCount], VarVariant);
         //데이타 처리변수
         XArr := VarArrayCreate([1, FieldCount], VarVariant);

         try
            //엑셀을 실행
            XL := CreateOLEObject('Excel.Application');
         except
            MessageDlg('Excel이 설치되어 있지 않습니다.',
                        MtWarning, [mbok], 0);
            Exit;
         end;

         XL.WorkBooks.Add; //새로운 페이지 생성
         XL.Visible := True;
         i := 1;
         k := 1;

         while i <= FieldCount do begin
            XTitle[i] := Fields[i-1].FieldName;
            Inc(i);
         end;
         //타이틀처리
         XL.Range['A1', CHR(64 + FieldCount) + '1'].Value := XTitle;

         First;
         while Not EOF do begin
            j := 1;
            while j <= FieldCount do begin
               if Fields[j-1].DataType = ftString then
                    XArr[j] := '''' + Fields[j-1].Value
               else                                    
                    XArr[j] := Fields[j-1].Value;
               Inc(j);
            end;
            //셀에 값을 넣는다.
            XL.Range['A' + IntToStr(k+1),
               CHR(64 + FieldCount) + IntToStr(k+1)].Value := XArr;
            Next;
            Inc(k);
         end;

         //셀 크기 조정
         XL.Range['A1', CHR(64 + FieldCount) + IntToStr(k)].Select;
         XL.Selection.Columns.AutoFit;
         XL.Range['A1', 'A1'].Select;
         First;
      end;
    end;
    end.


    마소에 실렸던 글입니다.

    이상입니다.

    즐코하세요

  • Profile
    gasina 2002.04.13 02:02
    저는 엑셀 오픈없이 엑셀 화일로 저장하는 방법을 알고 싶습니다.


  • Profile
    최석기 2002.04.13 04:49
    엑셀P/G없이 Excel파일 생성하기...  

    참고 하시고 유용하게 사용하세요.



    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);

    var
    Form1: TForm1;

    implementation

    {$R *.DFM}

    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('c: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;