主副表关联问题,如何打印,如何导出到EXECL ?(100分)

  • 主题发起人 主题发起人 qxhou
  • 开始时间 开始时间
Q

qxhou

Unregistered / Unconfirmed
GUEST, unregistred user!
表1:
电话 合户
5810000 0
5810001 1
5810002 0
5810003 0
5810004 1
表2:
电话 挂靠
5810000 5810000
5810001 5822625
5810001 5823241
5810002 5810002
5810003 5810003
5810004 5824158
5810004 5826685
表1中,合户字段为1的,在表2中,就至少有两条记录以上。
我现要要做出以下表格,要如何搞
电话 合户 挂靠
5810000 0 5810000
5810001 1 5812625
5813241
5810002 0 5810002
5810003 0 5810003
5810004 1 5824158
5826685
打印出来,要如何打印?导到EXECL 要如何导?
请朋友们速帮我,谢谢。QQ:346628217
 
顶一下贴
 
一直没有人来回答,自己结贴算了。。。今晚有人吗/来接分吧。
 
关联的结果输出到一个结果集里,然后象处理一般数据库那样。论坛里的帖子里,有直接把数据集输出到Excel的代码:我贴一下:
{ 背景:今天要把DataSet导入Excel,查询以前的贴子,一部分用Ole,速度太慢,
一部分用ADO连接到Excel文件,也很慢,一气之下把DBGrigEh的导出部分改了出来,
欢迎大家指教、改进。
功能:将数据集的数据导入Excel;
用法:With TDS2Excel.Create(TDataSet(ADOQuery1))do
Try
Save2File(SaveDialog1.FileName, True);
finally
Free;
end;
作者:Caidao (核心代码来自Ehlib)
时间:2003-04-09
地点:汕头
}

unit PRN2Excel;
interface
Uses
DB, Classes;
var
CXlsBof: array[0..5] of Word = ($809, 8, 0, $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);
CXlsBlank: array[0..4] of Word = ($201, 6, 0, 0, $17);

Type
TDS2Excel = Class(TObject)
Private
FCol: word;
FRow: word;
FDataSet: TDataSet;
Stream: TStream;
FWillWriteHead: boolean;
FBookMark: TBookmark;
procedure IncColRow;
procedure WriteBlankCell;
procedure WriteFloatCell(const AValue:do
uble);
procedure WriteIntegerCell(const AValue: Integer);
procedure WriteStringCell(const AValue: string);
procedure WritePrefix;
procedure WriteSuffix;
procedure WriteTitle;
procedure WriteDataCell;
procedure Save2Stream(aStream: TStream);
Public
procedure Save2File(FileName: string;
WillWriteHead: Boolean);
Constructor Create();
end;

implementation
uses SysUtils;
Constructor TDS2Excel.Create();
begin
inherited Create;
//FDataSet := aDataSet;
end;

procedure TDS2Excel.IncColRow;
begin
if FCol = FDataSet.FieldCount - 1 then
begin
Inc(FRow);
FCol :=0;
end
else
Inc(FCol);
end;

procedure TDS2Excel.WriteBlankCell;
begin
CXlsBlank[2] := FRow;
CXlsBlank[3] := FCol;
Stream.WriteBuffer(CXlsBlank, SizeOf(CXlsBlank));
IncColRow;
end;

procedure TDS2Excel.WriteFloatCell(const AValue:do
uble);
begin
CXlsNumber[2] := FRow;
CXlsNumber[3] := FCol;
Stream.WriteBuffer(CXlsNumber, SizeOf(CXlsNumber));
Stream.WriteBuffer(AValue, 8);
IncColRow;
end;

procedure TDS2Excel.WriteIntegerCell(const AValue: Integer);
var
V: Integer;
begin
CXlsRk[2] := FRow;
CXlsRk[3] := FCol;
Stream.WriteBuffer(CXlsRk, SizeOf(CXlsRk));
V := (AValue shl 2) or 2;
Stream.WriteBuffer(V, 4);
IncColRow;
end;

procedure TDS2Excel.WriteStringCell(const AValue: string);
var
L: Word;
begin
L := Length(AValue);
CXlsLabel[1] := 8 + L;
//CXlsLabel[2] := FRow;
CXlsLabel[2] := 1;
//CXlsLabel[3] := FCol;
CXlsLabel[3] := 1;
CXlsLabel[5] := L;
Stream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel));
Stream.WriteBuffer(Pointer(AValue)^, L);
//IncColRow;
2006年7月22日13时22分33秒
end;

procedure TDS2Excel.WritePrefix;
begin
Stream.WriteBuffer(CXlsBof, SizeOf(CXlsBof));
end;

procedure TDS2Excel.WriteSuffix;
begin
Stream.WriteBuffer(CXlsEof, SizeOf(CXlsEof));
end;

procedure TDS2Excel.WriteTitle;
var
n: word;
begin
for n := 0 to FDataSet.FieldCount - 1do
WriteStringCell(FDataSet.Fields[n].FieldName);
end;
//写成Excel的关键语句……2006年7月22日
procedure TDS2Excel.WriteDataCell;
var
n: word;
begin
WritePrefix;
if FWillWriteHead then
WriteTitle;
WriteStringCell('a test');
WriteSuffix;
end;

procedure TDS2Excel.Save2Stream(aStream: TStream);
begin
FCol := 0;
FRow := 0;
Stream := aStream;
WriteDataCell;
end;

procedure TDS2Excel.Save2File(FileName: string;
WillWriteHead: Boolean);
var
aFileStream: TFileStream;
begin
FWillWriteHead := WillWriteHead;
if FileExists(FileName) then
DeleteFile(FileName);
aFileStream := TFileStream.Create(FileName, fmCreate);
Try
Save2Stream(aFileStream);
Finally
aFileStream.Free;
end;
end;

end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部