Word2000制作报表(100分)

  • 主题发起人 主题发起人 asljl
  • 开始时间 开始时间
A

asljl

Unregistered / Unconfirmed
GUEST, unregistred user!
请问有谁知道怎么样在Word2000中绘制表格,并向里面加入数据??
 
网上有个用word作报表的控件,你下载下来看看人家是怎么做的.
 
先在word中制报表,然后向其中写数据(用查找与替换)。
 
参考一下,就是输出一个表格。
procedure TFormbaobiao.BitBtn1Click(Sender: TObject);
// Modified By Pan - 2002-12-07
// 关于 Word VBA 常量等参见 {$Delphi}/Ocx/Servers
// 这里使用的是 Word2000
var
Template, NewTemplate, ItemIndex: OleVariant;
ovDocumentType, ovVisible: OleVariant;
ovTableBehavior, ovAutoFitBehavior: OleVariant;
Start, Stop: OleVariant;
myRange: Range;
WordTable: Variant;
ColIndex, RowIndex: integer;
I, j: integer;
SQLstr, sqlstr1: string;
begin
if ListView2.Items.Count = 0 then
begin
Application.MessageBox('尚未选择打印客户范围,请先选择。', '提示',
MB_ICONINFORMATION + mb_Ok);
Exit;
end;
if ListView4.Items.Count = 0 then
begin
Application.MessageBox('尚未选择客户打印内容,请先选择。', '提示',
MB_ICONINFORMATION + mb_Ok);
Exit;
end;
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
WordApplication.Connect;
except
//MessageDlg('Word may not be installed', mtError, [mbOk], 0);
MessageDlg('可能没有安装 Word', mtError, [mbOk], 0);
Abort;
end;
try
WordApplication.Visible := True;
WordApplication.Caption := '报表打印';
{Create new do
cument}
Template := EmptyParam;
NewTemplate := False;
//WordApplication.Documents.Add(Template, NewTemplate);
ovDocumentType := Null;
ovVisible := True;
WordApplication.Documents.Add(Template, NewTemplate, ovDocumentType,
ovVisible);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
WordDocument.PageSetup.Orientation := wdOrientLandscape;
//设置纸张横向输出打印
// 设置便距
WordDocument.PageSetup.LeftMargin := WordApplication.InchesToPoints(0.75);
WordDocument.PageSetup.rightMargin := WordApplication.InchesToPoints(0.75);
WordDocument.PageSetup.topMargin := WordApplication.InchesToPoints(0.90);
WordDocument.PageSetup.bottomMargin :=
WordApplication.InchesToPoints(0.75);
WordDocument.Range.InsertAfter('客户资料' + #13#10);
//'报表'
Start := 0;
Stop := 4;
myRange := WordDocument.Range(Start, Stop);
// 设置标题字体加粗、居中显示
with myRangedo
begin
Bold := 1;
ParagraphFormat.Alignment := wdAlignParagraphCenter;
end;

//-- 指定插入表的起始处 --------
Start := 4;
Stop := Start;
myRange := WordDocument.Range(Start, Stop);
//WordTable := WordDocument.Tables.Add(myRange, ListView2.Items.Count + 1,
// ListView4.Items.Count);
// 表格样式
ovTableBehavior := wdWord9TableBehavior;
ovAutoFitBehavior := wdAutoFitWindow;
//wdAutoFitContent;
WordTable := WordDocument.Tables.Add(myRange, ListView2.Items.Count + 1,
ListView4.Items.Count, ovTableBehavior, ovAutoFitBehavior);

// 行数 // 列数
//--------------------------------------------------------
//-- 以上为华表个部分
//-- 打印列数,和打印行数已知,为listview2,listview4的行数
//--------------------------------------------------------
{
Formkhzldy.listview2.Items.Item[k].SubItems.strings[0];
}
{打印表头 }
ColIndex := 1;
for j := 0 to ListView4.Items.Count - 1do
begin
WordTable.Cell(1,
ColIndex).Range.InsertAfter(ListView4.Items.Item[j].Caption);
ColIndex := ColIndex + 1;
end;
// 准备数据
SQLstr := 'select ';
for I := 0 to ListView4.Items.Count - 1do
begin
if I = 0 then
SQLstr := SQLstr + ListView4.Items.Item.SubItems.Strings[0]
else
SQLstr := SQLstr + ',' + ListView4.Items.Item.SubItems.Strings[0] +
'';
end;
RowIndex := 2;
ColIndex := 1;
for I := 0 to ListView2.Items.Count - 1do
begin
if TabControl1.ActiveTab = main.cPersonNo then
sqlstr1 := SQLstr + ' from grkhzl where khdm=''' +
ListView2.Items.Item.Caption + ''''
else
sqlstr1 := SQLstr + ' from jgkhzl where khdm=''' +
ListView2.Items.Item.Caption + '''';
with AQuery1do
begin
Close;
SQL.Clear;
SQL.Append(sqlstr1);
Open;
end;
for j := 0 to DBGrid1.Columns.Count - 1do
begin
if Trim(WordTable.Cell(1, ColIndex).Range.Text) = '客户状态' then
begin
case AQuery1.FieldByName(DBGrid1.Columns[j].fieldname).AsInteger of
0: WordTable.Cell(RowIndex,
ColIndex).Range.InsertAfter('潜在客户');
1: WordTable.Cell(RowIndex,
ColIndex).Range.InsertAfter('现有客户');
2: WordTable.Cell(RowIndex,
ColIndex).Range.InsertAfter('历史客户');
end;
end
else
WordTable.Cell(RowIndex, ColIndex).Range.InsertAfter
(AQuery1.FieldByName(DBGrid1.Columns[j].fieldname).AsString);
ColIndex := ColIndex + 1;
end;
RowIndex := RowIndex + 1;
ColIndex := 1;
end;
finally
WordApplication.Activate;
// 最后确保释放对 Word 的连接,以免第二次点击按钮时如果用户已经关闭 word
// 会出错,而且没有必要保持对 word 的连接
WordDocument.Disconnect;
WordApplication.Disconnect;
end;
//end try-finally
end;
 
后退
顶部