Q&A

  • 엑셀 파일을 직접생성할때 ADO로 읽을 수있도록 만들순 없을까요(외부테이블 형식 오류라 하는군요)
procedure WriteExlRec(ExStream: TStream; const Row_, Col_ : Word; const Value_: Integer);
const
  ExcelRec: array[0..4] of Word = ($27E, 10, 0, 0, 0);
var
  V: Integer;
begin
  ExcelRec[2] := Row_;
  ExcelRec[3] := Col_;
  ExStream.WriteBuffer(ExcelRec, SizeOf(ExcelRec));
  V := (Value_ shl 2) or 2;
  ExStream.WriteBuffer(V, 4);
end;

procedure WriteExlCel(ExStream: TStream; const Row_, Col_ : Word; const Value_: Double);
const
  ExcelNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
begin
  ExcelNumber[2] := Row_;
  ExcelNumber[3] := Col_;
  ExStream.WriteBuffer(ExcelNumber, SizeOf(ExcelNumber));
  ExStream.WriteBuffer(Value_, 8);
end;

procedure WriteExlCell(ExStream : TStream; Const Row_, Col_ : Word; Const Value_ : String);
const
  ExcelLabel : array[0..5] of word = ($204, 0, 0, 0, 0, 0);
var
  L : Word;
begin
  L := Length(Value_);
  ExcelLabel[1] := 8 + L;
  ExcelLabel[2] := Row_;
  ExcelLabel[3] := Col_;
  ExcelLabel[5] := L;
  ExStream.WriteBuffer(ExcelLabel, SizeOf(ExcelLabel));
  ExStream.WriteBuffer(Pointer(Value_)^, L);
end;

procedure WriteExlBegin(ExStream : TStream);
const
  ExcelBegin : array[0..5] of word = ($809, 8, 00, $10, 0, 0);
var
  L : Word;
begin
  L := 0;
  ExcelBegin[4] := L;
  ExStream.WriteBuffer(ExcelBegin, SizeOf(ExcelBegin));
end;

procedure WriteExlEnd(ExStream : TStream);
const
  ExcelEnd   : array[0..1] of word = ($0A, 00);
begin
  ExStream.WriteBuffer(ExcelEnd, SizeOf(ExcelEnd));
end;

Procedure TForm1.XLS_Save;
var
  Row_, Col_, ii : Integer;
  F_Stream : TFileStream;
begin
  F_Stream := TFileStream.Create('c:'+Edit1.Text+'.xls', fmCreate);
  try
    // 시작
    WriteExlBegin(F_Stream);
    Query1.Active := False;
    Query1.DatabaseName := 'SICHUNG';
    Query1.Close;
    Query1.SQL.Clear;
    Query1.SQL.Add('SELECT * FROM '+Edit1.Text);
    Query1.open;
    Query1.first;
    Row_ := -1;
    Col_ := 0;
    if query1.eof then exit;
    while not query1.eof do
    begin
      Row_ := Row_ + 1;
      for ii := 0 to query1.fieldcount -1 do
      begin
         Col_ := ii;
//         WriteExlCel(F_Stream, Row_, Col_, 34.34);
//         WriteExlRec(F_Stream, Row_, Col_, 3434);
         WriteExlCell(F_Stream, Row_, Col_, Query1.Fields[Col_].AsString);
      end;
      Query1.Next;
    end;
    Query1.Close;
    WriteExlEnd(F_Stream);
  finally
    F_Stream.Free;
  end;
end;

위와 같이 직접 엑셀 파일을 생성 했습니다... 그런데
ADO Query 로 읽으려고 하니까 외부데이타 형식 오류라고 하는군요.
테이블 형식으로 엑셀 파일을 직접 생성할순 없을 까요.
도와 주세요..
1  COMMENTS
  • Profile
    이규재 2003.08.07 02:36
    엑셀구조보고 어케 해볼라했는데 실력이 안되서..
    구조설명이 영어로 된데다가.. 텍스트가 열나게기네요
    excel.txt 보시면 끝부분에 보시면 예제 코드도 들어 있습니다.
    암튼 여러모로 도움되길 바랍니다..
    ㅠ.ㅠ..