Delphi7如何在Word插入表格并往表格单元格填写内容?(100分)

  • 主题发起人 主题发起人 hhi179
  • 开始时间 开始时间
H

hhi179

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi7如何在Word插入表格并往表格单元格填写内容?
 
我给你一个表格函数,Word2000=就是WORDAPPLICATION;从我的MIS项目中截取的。


procedure TFrmCertificate_Sample.Word2000Table(var FTable: Table;
TableSize, FontSize: Single; FRowNum, FColNum: Integer;
BorderIndex: Boolean);
begin
FTable:=Word2000.Selection.Tables.Add(Word2000.Selection.Range,FRowNum,FColNum,
EmptyParam,EmptyParam);
FTable.Range.Font.Name:=MyFrame.FontName;
FTable.Range.Font.Size:=FontSize;
FTable.PreferredWidthType:=wdPreferredWidthPercent;
FTable.PreferredWidth:=TableSize;
with FTable do
begin
//-----如果不是,设置为无边框-------
TopPadding:=Word2000.CentimetersToPoints(0);
BottomPadding:=Word2000.CentimetersToPoints(0);
LeftPadding:=Word2000.CentimetersToPoints(0);
RightPadding:=Word2000.CentimetersToPoints(0);
Spacing:=0;
AllowPageBreaks:=True;
AllowAutoFit:=False;
if BorderIndex=False then
begin
Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderRight).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderTop).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderDiagonalDown).LineStyle:=wdLineStyleNone;
Borders.Item(wdBorderDiagonalUp).LineStyle:=wdLineStyleNone;
Borders.Shadow:=False;
end;
//------设置表格对齐方式---------------
FTable.Columns.PreferredWidthType:=wdPreferredWidthAuto;
FTable.Range.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
Range.Cells.VerticalAlignment:=wdCellAlignVerticalCenter;
Rows.Alignment:=wdAlignRowCenter;
end;
end;

Word2000Table(FTable,100,9,FRowCount+1,5,False);
with FTable do
begin
Cell(1,1).Range.Text:='名称';
Cell(1,2).Range.Text:='规格型号';
Cell(1,3).Range.Text:='测量范围';
Cell(1,4).Range.Text:='不确定度/准确度';
Cell(1,5).Range.Text:='证书编号';
//------------------填写数据-------------
FRow:=2;
for I:=1 to StoreGrid.RowCount-1 do
begin
if StoreGrid.Cells[0,I]<>'' then
begin
Cell(FRow,1).Range.Text:=StoreGrid.Cells[1,I];
Cell(FRow,2).Range.Text:=StoreGrid.Cells[2,I];
Cell(FRow,3).Range.Text:=StoreGrid.Cells[3,I];
Cell(FRow,4).Range.Text:=StoreGrid.Cells[4,I];
Cell(FRow,5).Range.Text:=StoreGrid.Cells[5,I];
FRow:=FRow+1;
end;
end;
end;//end tab
 
只要你计算机里有WORD和VB就简单了。你可以在WORD的工具栏里添加宏(比如加个表格什么的),然后结束添加,运行宏,执行单步运行。这样在VB中就能看到WORD的命令行了,只要在DELPHI里试试就行了。用这办法想在WORD中做什么都行。
 
procedure TForm1.Button1Click(Sender: TObject);
var FTable: Table;
VarWord: Variant;// 创建 WORD时所用
FRowNum, FColNum: Integer;
a:olevariant;
begin
a:=OpenDialog2.FileName;//已经存在的文档
FRowNum:=5;
FColNum:=8;
wordapplication1.Documents.Open(a,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
VarWord:=Wordapplication1.activedocument;
Wordapplication1.ActiveDocument.PageSetup.PageWidth:=600;
//Wordapplication1.ActiveDocument.PageSetup.PageHeight:=23;
Wordapplication1.ActiveDocument.PageSetup.topMargin:=55;
Wordapplication1.ActiveDocument.PageSetup.LeftMargin:=45;
Wordapplication1.ActiveDocument.PageSetup.rightMargin:=45;
Wordapplication1.ActiveDocument.PageSetup.bottomMargin:=45;
Wordapplication1.Selection.TypeText(' '+trim(memo1.text));
Wordapplication1.visible := true;
FTable:=WORDAPPLICATION1.Selection.Tables.Add(WORDAPPLICATION1.Selection.Range,FRowNum,FColNum,EmptyParam,EmptyParam);
FTable.Range.Font.Size:=12;
FTable.PreferredWidthType:=wdPreferredWidthPercent;
FTable.PreferredWidth:=100;
with FTable do
begin
//-----如果不是,设置为无边框-------
TopPadding:=WORDAPPLICATION1.CentimetersToPoints(0);
BottomPadding:=WORDAPPLICATION1.CentimetersToPoints(0);
LeftPadding:=WORDAPPLICATION1.CentimetersToPoints(0);
RightPadding:=WORDAPPLICATION1.CentimetersToPoints(0);
Spacing:=0;
AllowPageBreaks:=True;
AllowAutoFit:=False;
Cell(1,1).Range.Text:='名称';
Cell(1,1).Width:=50;//20能放下一个字
Cell(1,2).Range.Text:='规格型号';
Cell(1,3).Range.Text:='测量范围';
Cell(1,4).Range.Text:='不确定度/准确度';
Cell(1,5).Range.Text:='证书编号';
Cell(2,1).Range.Text:='名称';
Cell(2,1).Width:=50;
Cell(2,2).Range.Text:=memo2.text;
Cell(2,3).Range.Text:='测量范围';
Cell(2,4).Range.Text:='不确定度/准确度';
Cell(2,5).Range.Text:='证书编号';
Cell(3,1).Width:=50;
Cell(4,1).Width:=50;
Cell(5,1).Width:=50;
//------设置表格对齐方式---------------
FTable.Columns.PreferredWidthType:=wdPreferredWidthAuto;
FTable.Range.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
Range.Cells.VerticalAlignment:=wdCellAlignVerticalCenter;
Rows.Alignment:=wdAlignRowCenter;
Wordapplication1.Selection.TypeText(' '+trim(memo3.text));
worddocument1.Disconnect;
wordapplication1.Disconnect;
end;
end;
 
分错分了。
 

Similar threads

S
回复
0
查看
860
SUNSTONE的Delphi笔记
S
S
回复
0
查看
785
SUNSTONE的Delphi笔记
S
D
回复
0
查看
961
DelphiTeacher的专栏
D
D
回复
0
查看
894
DelphiTeacher的专栏
D
后退
顶部