项目中需要把数据通过word表格形式导出,建好表后发现表头太长,纵向不能全部显示,需要改为横向显示。网上没找到可以直接使用的例子,最后参考几篇文章,写了一个简单的测试例子。
procedure TForm1.WordOutput();
var
WordApp, WordDoc,table: OleVariant;
fileName,docFileName : string;
i,j:integer;
Rows:integer;
begin
try
{创建OLE对象}
WordApp := CreateOleObject('Word.Application');
WordDoc := WordApp.Documents.Add;
Except
Application.MessageBox('未安装安装Microsoft Word',
'使用Microsoft Word',MB_OK+MB_ICONWarning);
Exit;
End;
//DateTime:=Time();
//FileName := 'Demo'+DateTimeToTimeStamp(now).time .ToString+'.doc';
FileName := 'Demo.doc';
docFileName:=ExtractFilePath(paramstr(0))+ FileName;
showmessage(docFileName);
try
WordDoc.PageSetup.LeftMargin := 0.39*72; // 1 英寸 = 72 磅
WordDoc.PageSetup.RightMargin := 0.39*72;
WordDoc.PageSetup.TopMargin := 1*72;
WordDoc.PageSetup.BottomMargin := 1*72;
WordDoc.PageSetup.PaperSize := wdPaperA4; //A4纸
WordDoc.PageSetup.Orientation := wdOrientLandscape; //纸张横向
WordApp.Selection.Font.Name := '黑体';
WordApp.Selection.Font.Size := 22; //二号字体 单位:磅
WordApp.Selection.Font.Bold := True; //字体加粗
WordApp.Selection.Font.Color := wdColorBlack; //字体颜色
WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter; //段落中文本居中
WordApp.Selection.ParagraphFormat.LineSpacingRule := wdLineSpaceSingle; //单倍行距
WordApp.Selection.TypeText('测试表格');
WordApp.Selection.TypeParagraph; //回车
WordApp.Selection.TypeParagraph;
WordApp.Selection.Font.Name := '宋体';
WordApp.Selection.Font.Size := 9;
WordApp.Selection.Font.Bold := False;
WordApp.Selection.Font.Color := wdColorBlack;
//往第三段增加一表格(2行11列)
table := WordApp.ActiveDocument.Tables.Add(WordApp.ActiveDocument.Paragraphs.item(3).Range,2,11);
//合并单元格
{
table.Cell(1, 1).Merge(table.Cell(2, 1));
}
table.cell(1,1).VerticalAlignment := wdCellAlignVerticalCenter; //垂直居中
WordApp.Selection.TypeText('序号');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('AA');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('BB');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('CC');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('DD');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('EE');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('FF');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('GG');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('HH');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('II');
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText('JJ');
//数据
rows := 10;
for I := 1 to rows do //行数
begin
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText(inttostr(i)); //序号
for j := 1 to 10 do //列数
begin
WordApp.Selection.MoveRight(wdCell,1);
WordApp.Selection.TypeText(inttostr(j*i)); //内容
end;
end;
table.Rows.Alignment := wdAlignRowCenter;//表格居中
table.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
table.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
table.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
table.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;
table.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;
table.Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleSingle;
WordDoc.saveas(docFileName);
{
If MessageDlg(docFileName+'是否保存?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
WordDoc.saveas(docFileName)
Else
WordDoc.Saved:=True; //放弃保存
}
showmessage('ok');
finally
WordDoc.Saved := True;
WordDoc.Close;
WordApp.Quit;
end;
end;