我实现过用Excel做Delphi报表输出工具;贴上主要的程序:
procedure TPrintSet.btPrnExcelClick(Sender: TObject);
var RangeE: Excel2000.Range;0D
j,k,lastPrintField,I, Row: integer;
sCell: String;
begin
j := 0;
k := 0;
lastPrintField := 0;
SetFieldlist;
while (j < nFieldCount) do0D
begin
if (WantPrintedFieldlist[j] = 1) then
begin
k := k + 1;
lastPrintField := j;
end;
j := j + 1;
end;
pBar.Position := 0;
ExcelApplication.Connect;
pBar.Position := 20;
ExcelApplication.Workbooks.Add(null,0);
ExcelApplication.Caption :3D edTitle.Text;
pBar.Position := 30; //fill the first row with field titles
sCell := char(65 + k -1);
RangeE := ExcelApplication.Range['A1',sCell+'1'];
RangeE.MergeCells := true;
RangeE.HorizontalAlignment := xlCenter;
RangeE.VerticalAlignment :3D xlCenter;
RangeE.Borders.LineStyle :3D xlContinuous;
RangeE.Font.Bold := true;
RangeE.WrapText := true;
RangeE.Font.Size := 20;
RangeE.Value := edTitle.Text; RangeE := ExcelApplication.Range ['A2', 'A2'];
for I := 0 to (nFieldCount - 1) do
0A begin
RangeE.HorizontalAlignment := xlCenter;
RangeE.VerticalAlignment := xlCenter;
RangeE.Borders.LineStyle := xlContinuous;
RangeE.Font.Bold := true;
RangeE.WrapText := true;
if (WantPrintedFieldlist[I] = 1) then
begin
RangeE.Value := FDBGrid.DataSource.DataSet.Fields[I].DisplayLabel;
if (I <> lastPrintField) then RangeE := RangeE.Next;
end;
end;
pBar.Position := 40; //add field data in folling rows
FDBGrid.DataSource.DataSet.First;
Row := 3;
while not FDBGrid.DataSource.DataSet.Eof do
begin
RangeE := ExcelApplication.Range ['A' 2B IntToStr (Row), 'A' + IntToStr (Row)];
for I := 0 to (nFieldCount - 1) do
0A begin
RangeE.WrapText := true;
RangeE.HorizontalAlignment := xlLeft;
RangeE.VerticalAlignment := xlCenter;
RangeE.Borders.LineStyle := xlContinuous;
if (WantPrintedFieldlist[I] = 1) then
begin
RangeE.Value := FDBGrid.DataSource.DataSet.Fields[I].AsString;
if (I <> lastPrintField) then RangeE := RangeE.Next;
end;
pBar.Position := pBar.Position + Round((pBar.Max - pBar.Position)/(FDBGrid.DataSource.DataSet.FieldCount));
end;
FDBGrid.DataSource.DataSet.Next;
Inc (Row);
end;
pBar.Position := 100;
ExcelApplication.Visible[0] := true;
end;
—————————————————————————————————
MaximStr := '宠辱不惊,看庭前花开花落,去留无意;
毁誉由人,望天上云卷云舒,聚散任风。';
if Not Assigned(I) then
I := TI.Create(Nil);
I.Maxim := MaximStr;
I.Explain := '假如上述代码中出现“OA”等字样,删除它们';
I.Desire := '加不加分随你';
—————————————————————————————————
procedure TPrintSet.btPrnExcelClick(Sender: TObject);
var RangeE: Excel2000.Range;0D
j,k,lastPrintField,I, Row: integer;
sCell: String;
begin
j := 0;
k := 0;
lastPrintField := 0;
SetFieldlist;
while (j < nFieldCount) do0D
begin
if (WantPrintedFieldlist[j] = 1) then
begin
k := k + 1;
lastPrintField := j;
end;
j := j + 1;
end;
pBar.Position := 0;
ExcelApplication.Connect;
pBar.Position := 20;
ExcelApplication.Workbooks.Add(null,0);
ExcelApplication.Caption :3D edTitle.Text;
pBar.Position := 30; //fill the first row with field titles
sCell := char(65 + k -1);
RangeE := ExcelApplication.Range['A1',sCell+'1'];
RangeE.MergeCells := true;
RangeE.HorizontalAlignment := xlCenter;
RangeE.VerticalAlignment :3D xlCenter;
RangeE.Borders.LineStyle :3D xlContinuous;
RangeE.Font.Bold := true;
RangeE.WrapText := true;
RangeE.Font.Size := 20;
RangeE.Value := edTitle.Text; RangeE := ExcelApplication.Range ['A2', 'A2'];
for I := 0 to (nFieldCount - 1) do
0A begin
RangeE.HorizontalAlignment := xlCenter;
RangeE.VerticalAlignment := xlCenter;
RangeE.Borders.LineStyle := xlContinuous;
RangeE.Font.Bold := true;
RangeE.WrapText := true;
if (WantPrintedFieldlist[I] = 1) then
begin
RangeE.Value := FDBGrid.DataSource.DataSet.Fields[I].DisplayLabel;
if (I <> lastPrintField) then RangeE := RangeE.Next;
end;
end;
pBar.Position := 40; //add field data in folling rows
FDBGrid.DataSource.DataSet.First;
Row := 3;
while not FDBGrid.DataSource.DataSet.Eof do
begin
RangeE := ExcelApplication.Range ['A' 2B IntToStr (Row), 'A' + IntToStr (Row)];
for I := 0 to (nFieldCount - 1) do
0A begin
RangeE.WrapText := true;
RangeE.HorizontalAlignment := xlLeft;
RangeE.VerticalAlignment := xlCenter;
RangeE.Borders.LineStyle := xlContinuous;
if (WantPrintedFieldlist[I] = 1) then
begin
RangeE.Value := FDBGrid.DataSource.DataSet.Fields[I].AsString;
if (I <> lastPrintField) then RangeE := RangeE.Next;
end;
pBar.Position := pBar.Position + Round((pBar.Max - pBar.Position)/(FDBGrid.DataSource.DataSet.FieldCount));
end;
FDBGrid.DataSource.DataSet.Next;
Inc (Row);
end;
pBar.Position := 100;
ExcelApplication.Visible[0] := true;
end;
—————————————————————————————————
MaximStr := '宠辱不惊,看庭前花开花落,去留无意;
毁誉由人,望天上云卷云舒,聚散任风。';
if Not Assigned(I) then
I := TI.Create(Nil);
I.Maxim := MaximStr;
I.Explain := '假如上述代码中出现“OA”等字样,删除它们';
I.Desire := '加不加分随你';
—————————————————————————————————
http://www.ksdev.com/vcl/index.html
http://vcl.vclxx.org/DELPHI/D32FREE/DATA2WRD.ZIP最近接触了一个用户的案例,用delphi控制word做一个合同管理程序。办公人员先根据业务需要,写好合同的文字,但在用户名称、产品名称等变化的位置填写指定的标记字符串,然后通过delphi把数据库中的实际数据替换掉word中的文字,最后让word打印出合同。 delphi自带了一个简单的word例题,但功能太简单。通过查找vba的说明,再对照delphi的vcl,编写了如下代码,实现了基本的公文管理功能。 启动word时用如下代码:
begin
try
wordapplication.connect;
except
messagedlg('word may not be installed', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;
wordapplication.caption := 'delphi automation';
end; 关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end; 让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:
var
itemindex :olevariant;
filename, confirmconversions, readonly, addtorecentfiles,
passworddocument, passwordtemplate, revert,
writepassworddocument, writepasswordtemplate, format: olevariant;
begin
if not dlgopen.execute then
exit; {open document}
filename := dlgopen.filename;
confirmconversions := false;
readonly := false;
addtorecentfiles := false;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument; wordapplication.documents.open( filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format ); {assign worddocument component}
itemindex := 1;
worddocument.connectto(wordapplication.documents.item(itemindex)); {turn spell checking of because it takes a long time if enabled and slows down winword}
wordapplication.options.checkspellingasyoutype := false;
wordapplication.options.checkgrammarasyoutype := false;
end; 让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>:
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace: olevariant;
begin
findtext := '<#name>';
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := 'delphi';
replace := true; worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace ); end; 上面这4段代码完成了公文管理的基本功能,再把它和数据库结合起来,就可以开发一个与lotus notes类似的产品了。
http://www.ksdev.com/vcl/index.html
http://vcl.vclxx.org/DELPHI/D32FREE/DATA2WRD.ZIP最近接触了一个用户的案例,用delphi控制word做一个合同管理程序。办公人员先根据业务需要,写好合同的文字,但在用户名称、产品名称等变化的位置填写指定的标记字符串,然后通过delphi把数据库中的实际数据替换掉word中的文字,最后让word打印出合同。 delphi自带了一个简单的word例题,但功能太简单。通过查找vba的说明,再对照delphi的vcl,编写了如下代码,实现了基本的公文管理功能。 启动word时用如下代码:
begin
try
wordapplication.connect;
except
messagedlg('word may not be installed', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;
wordapplication.caption := 'delphi automation';
end; 关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end; 让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:
var
itemindex :olevariant;
filename, confirmconversions, readonly, addtorecentfiles,
passworddocument, passwordtemplate, revert,
writepassworddocument, writepasswordtemplate, format: olevariant;
begin
if not dlgopen.execute then
exit; {open document}
filename := dlgopen.filename;
confirmconversions := false;
readonly := false;
addtorecentfiles := false;
passworddocument := '';
passwordtemplate := '';
revert := true;
writepassworddocument := '';
writepasswordtemplate := '';
format := wdopenformatdocument; wordapplication.documents.open( filename, confirmconversions,
readonly, addtorecentfiles, passworddocument, passwordtemplate,
revert, writepassworddocument, writepasswordtemplate, format ); {assign worddocument component}
itemindex := 1;
worddocument.connectto(wordapplication.documents.item(itemindex)); {turn spell checking of because it takes a long time if enabled and slows down winword}
wordapplication.options.checkspellingasyoutype := false;
wordapplication.options.checkgrammarasyoutype := false;
end; 让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>:
var
findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
matchallwordforms, forward, wrap, format, replacewith, replace: olevariant;
begin
findtext := '<#name>';
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replacewith := 'delphi';
replace := true; worddocument.range.find.execute( findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace ); end; 上面这4段代码完成了公文管理的基本功能,再把它和数据库结合起来,就可以开发一个与lotus notes类似的产品了。