//최용일님의 [팁] 을 이용하여 Excel을 만들었는데....
//[팁]
안녕하세요. 최용일입니다.
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
================================================
=======저의 풀그램========
ADO외에 다이렉트로 다시 읽는 방법은 없을런지?
================================================
procedure TfrmK9Main.btnM2ExcelClick(Sender: TObject);
var FStream: TFileStream;
i, j: Integer;
XValue, YValue, PValue: Double;
buf, FName: string;
begin
CurrentDir := '..\Data';
buf := 'Test';
//buf := FormatDateTime('yyyymmddhhnn',Now);
FName := CurrentDir + '\T' + buf + '.xls';
ConversionFileDataToTList;
FStream := TFileStream.Create(FName, fmCreate);
try
XlsBeginStream(FStream, 0);
//Title......
//XlsWriteCellLabel(FStream, 0, 0, 'TargetID');
//for i := 1 to 8 do XlsWriteCellLabel(FStream, (i * 3) - 2, 0, 'ID' + IntToStr(i));
XlsWriteCellLabel(FStream, 0, 0, 'XCount');
for i := 1 to 8 do begin
//XlsWriteCellLabel(FStream, (i * 3) - 2, 0, 'X_Value');
//XlsWriteCellLabel(FStream, (i * 3) - 1, 0, 'Y_Value');
//XlsWriteCellLabel(FStream, (i * 3), 0, 'P_Value');
XlsWriteCellLabel(FStream, (i * 3) - 2, 0, 'X' + IntToStr(i) + '_Value');
XlsWriteCellLabel(FStream, (i * 3) - 1, 0, 'Y' + IntToStr(i) + '_Value');
XlsWriteCellLabel(FStream, (i * 3), 0, 'P' + IntToStr(i) + '_Value');
end;
for i := 0 to MaxDataCount - 1 do begin
XlsWriteCellNumber(FStream, 0, i + 1, i);
for j := 1 to 8 do begin
try
XValue := FUList[j].Items[i].XValue;
XValue := Trunc(XValue * 1000) / 1000;
except XValue := 0; end;
XlsWriteCellNumber(FStream, (j * 3) - 2, i + 1, XValue);
try
YValue := FUList[j].Items[i].YValue;
YValue := Trunc(YValue * 1000) / 1000;
except YValue := 0; end;
XlsWriteCellNumber(FStream, (j * 3) - 1, i + 1, YValue);
try
PValue := FUList[j].Items[i].PValue;
PValue := Trunc(PValue * 1000) / 1000;
except PValue := 0; end;
XlsWriteCellNumber(FStream, (j * 3), i + 1, PValue);
end;
end;
XlsEndStream(FStream);
finally
FStream.Free;
//ShowMessage('End..CnvExcel');
end;
ShellExecute(Handle, 'open', 'Excel', PChar(FName), '', SW_SHOWNORMAL);
end;
로 하여 엑셀 황일을 만들고 다시 ............불러오기를 하였읍니다
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
with ADOQuery1 do
begin
close;
ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
OpenDialog1.FileName + ';Extended Properties=Excel 8.0;Persist Security Info=False';
SQL.Clear;
SQL.Text := 'SELECT * FROM [TTest$]'; // 엑셀 문서의 sheet값이 Sheet1
open;
end;
end;
근데 ComObj를 이용한 XSL화일은 읽어지는데 자꾸만 에러메세지가.....
'외부 데이터 형식이 잘못되었읍니다' 라는 메세지가 저를.........
제가 무엇을 잘못 했는지요....
직접읽어드릴수 있는 다른 방법은 없을까요???
고수님들의 도움을 구합니다....