Delphi修改word页面方向

项目中需要把数据通过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;

转向参考.
建表参考.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值