请问这个程序导出的XLS文件,为什么不能被Excls打开? 提示:"不能读取文件!" ( 积分: 100 )

  • 主题发起人 主题发起人 yzykjh
  • 开始时间 开始时间
Y

yzykjh

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure XlsWriteCellLabel(XlsStream: TStream; const ACol, ARow: Word; const AValue: string);
var
L: Word;
const
{$J+}
CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
{$J-}
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;

function SaveAsExcelFile(AGrid: TDBGridEh; AFileName: string): Boolean;
const
{$J+} CXlsBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0); {$J-}
CXlsEof: array[0..1] of Word = ($0A, 00);
var
FStream: TFileStream;
I, J: Integer;
begin
Result := False;
FStream := TFileStream.Create(PChar(AFileName), fmCreate or fmOpenWrite);
try
CXlsBof[4] := 0;
FStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
for i := 0 to AGrid.DataSource.DataSet.RecordCount -1 do
for j := 0 to AGrid.Columns.Count -1 do
XlsWriteCellLabel(FStream, I, J, AGrid.DataSource.DataSet.Fields[j].AsString);
FStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
Result := True;
finally
FStream.Free;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveAsExcelFile(DBGridEh1, 'c:/MyExcelFile.xls') then
ShowMessage('DBGridEh saved!');
end;
 
procedure XlsWriteCellLabel(XlsStream: TStream; const ACol, ARow: Word; const AValue: string);
var
L: Word;
const
{$J+}
CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
{$J-}
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;

function SaveAsExcelFile(AGrid: TDBGridEh; AFileName: string): Boolean;
const
{$J+} CXlsBof: array[0..5] of Word = ($809, 8, 00, $10, 0, 0); {$J-}
CXlsEof: array[0..1] of Word = ($0A, 00);
var
FStream: TFileStream;
I, J: Integer;
begin
Result := False;
FStream := TFileStream.Create(PChar(AFileName), fmCreate or fmOpenWrite);
try
CXlsBof[4] := 0;
FStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
for i := 0 to AGrid.DataSource.DataSet.RecordCount -1 do
for j := 0 to AGrid.Columns.Count -1 do
XlsWriteCellLabel(FStream, I, J, AGrid.DataSource.DataSet.Fields[j].AsString);
FStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
Result := True;
finally
FStream.Free;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveAsExcelFile(DBGridEh1, 'c:/MyExcelFile.xls') then
ShowMessage('DBGridEh saved!');
end;
 
Stream已经读取,在打开时不能获得句柄
 
请帮忙解决!
 
自己搞定!
 
后退
顶部