AssignFile(TxtFile,filename); //先将文件变量TxtFile与filename文件名关联 Rewrite(TxtFile); for i := 0 to DBGrid.Columns.Count -1 do //写标题 begin TxtStr := TxtStr + ViewDBGrid.Columns[i].Title.Caption + ' '; end; writeln(TxtFile,TxtStr);DBGrid.DataSource.DataSet.First; Append(TxtFile); while not ViewDBGrid.DataSource.DataSet.Eof do begin for j:= 0 to ViewDBGrid.Columns.Count -1 do TxtStr:=TxtStr+ DBGrid.Fields[j].Text + ' ' writeln(TxtFile,HtmlStr); end;
请教:如果mailto要增加主题,怎么做?
这里有完整的代码,而且支持两种输出格式(SPACE, COMMA), 有点长. 1. procedure exportRecords(const aFileName: String; aQuery: TQuery; const aSeparator: Char);const arrcRETURN: array[0..1] of Char = #13#10; arrcQUOTE: array[0..0] of Char = #34; arrcDOUBLEQUOTE: array[0..1] of Char = #34#34;var i, j: Integer; lsWriteString: String; larrcBuffer: array[0..4000] of Char; larrcSeparator: array[0..1] of Char; lFileBuf: TFileStream; begin lFileBuf := TFileStream.Create(aFileName, fmCreate); try with aQuery, lFileBuf do begin DisableControls;{ faster; disable highlight line from moving through rows } try StrPCopy(larrcSeparator, aSeparator); {Export column names to text file} for i := 0 to FieldCount - 1 do begin StrPCopy(larrcBuffer, Fields[i].DisplayLabel); Write(larrcBuffer, StrLen(larrcBuffer)); Write(larrcSeparator, 1); end; Seek(-1, 1{Current Position});{go back one step from current position; overwrite comma} Write(arrcRETURN, 2);
{Export the records of DBGrid} First; while not Eof do begin for i := 0 to FieldCount - 1 do begin if aSeparator = #32{SPACE} then for j := 0 to Fields[i].DataSize -1 do larrcBuffer[j] := aSeparator; StrPCopy(larrcBuffer, Fields[i].AsString); case Fields[i].DataType of ftMemo, ftString: begin if aSeparator = #32{SPACE} then Write(larrcBuffer, Fields[i].DataSize) else begin if aSeparator = #44{COMMA} then begin Write(arrcQUOTE, 1); lsWriteString := StringReplace(larrcBuffer, arrcQUOTE, arrcDOUBLEQUOTE, [rfReplaceAll]); StrPCopy(larrcBuffer, lsWriteString); Write(larrcBuffer, StrLen(larrcBuffer)); Write(arrcQUOTE, 1); end else Write(larrcBuffer, StrLen(larrcBuffer)); end; end; else begin if aSeparator = #32{SPACE} then Write(larrcBuffer, Fields[i].DataSize) else Write(larrcBuffer, StrLen(larrcBuffer)); end; {else case} end; {case} if (i = FieldCount - 1) then Write(arrcRETURN, 2) else if aSeparator <> #32{SPACE} then Write(larrcSeparator, 1{StrLen(aSeparator)}); end; {for} Next; end; {while} finally EnableControls; end; end; finally lFileBuf.Free; end; end;2. ShellExecute(Application.Handle,nil, 'mailto:[email protected]?cc=&subject=Hello&body=Hello world', nil, nil, SW_SHOW);
uses ShellApi;procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(Handle, nil, 'mailto:[email protected]', nil, nil, SW_SHOW);
end;
2、mailto:[email protected],这句话使用shellexecute之类的执行
http://kingron.myetang.com/zsfunc04.htm
Rewrite(TxtFile);
for i := 0 to DBGrid.Columns.Count -1 do //写标题
begin
TxtStr := TxtStr + ViewDBGrid.Columns[i].Title.Caption + ' ';
end;
writeln(TxtFile,TxtStr);DBGrid.DataSource.DataSet.First;
Append(TxtFile);
while not ViewDBGrid.DataSource.DataSet.Eof do
begin
for j:= 0 to ViewDBGrid.Columns.Count -1 do
TxtStr:=TxtStr+ DBGrid.Fields[j].Text + ' '
writeln(TxtFile,HtmlStr);
end;
1.
procedure exportRecords(const aFileName: String; aQuery: TQuery; const aSeparator: Char);const arrcRETURN: array[0..1] of Char = #13#10;
arrcQUOTE: array[0..0] of Char = #34;
arrcDOUBLEQUOTE: array[0..1] of Char = #34#34;var i, j: Integer;
lsWriteString: String;
larrcBuffer: array[0..4000] of Char;
larrcSeparator: array[0..1] of Char;
lFileBuf: TFileStream;
begin
lFileBuf := TFileStream.Create(aFileName, fmCreate);
try
with aQuery, lFileBuf do begin
DisableControls;{ faster; disable highlight line from moving through rows }
try
StrPCopy(larrcSeparator, aSeparator);
{Export column names to text file}
for i := 0 to FieldCount - 1 do begin
StrPCopy(larrcBuffer, Fields[i].DisplayLabel);
Write(larrcBuffer, StrLen(larrcBuffer));
Write(larrcSeparator, 1);
end;
Seek(-1, 1{Current Position});{go back one step from current position; overwrite comma}
Write(arrcRETURN, 2);
{Export the records of DBGrid}
First;
while not Eof do begin
for i := 0 to FieldCount - 1 do begin if aSeparator = #32{SPACE} then
for j := 0 to Fields[i].DataSize -1 do
larrcBuffer[j] := aSeparator;
StrPCopy(larrcBuffer, Fields[i].AsString); case Fields[i].DataType of
ftMemo, ftString: begin
if aSeparator = #32{SPACE} then
Write(larrcBuffer, Fields[i].DataSize)
else begin
if aSeparator = #44{COMMA} then begin
Write(arrcQUOTE, 1);
lsWriteString := StringReplace(larrcBuffer, arrcQUOTE,
arrcDOUBLEQUOTE, [rfReplaceAll]);
StrPCopy(larrcBuffer, lsWriteString);
Write(larrcBuffer, StrLen(larrcBuffer));
Write(arrcQUOTE, 1);
end
else
Write(larrcBuffer, StrLen(larrcBuffer));
end;
end;
else begin if aSeparator = #32{SPACE} then
Write(larrcBuffer, Fields[i].DataSize)
else
Write(larrcBuffer, StrLen(larrcBuffer)); end; {else case}
end; {case} if (i = FieldCount - 1) then
Write(arrcRETURN, 2)
else
if aSeparator <> #32{SPACE} then
Write(larrcSeparator, 1{StrLen(aSeparator)});
end; {for}
Next;
end; {while}
finally
EnableControls;
end; end;
finally
lFileBuf.Free;
end;
end;2.
ShellExecute(Application.Handle,nil, 'mailto:[email protected]?cc=&subject=Hello&body=Hello world', nil, nil, SW_SHOW);