利用MATLAB生成Word文档
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
fun ctio n ceshi_Word
%利用MATLAB生成Word文档
% ceshi_Word
%
% Copyright 2009 - 2010 xiezhh.
% $Revisio n: 1.0.0.0 $ $Date: 2009/12/14 12:37:00 $
%设定测试Word文件名和路径
测试.doc'];
%判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开try
%若Word服务器已经打开,返回其句柄Word
Word = actxGetR unnin gServer('Word.Applicati on');
catch
%创建一个Microsoft Word服务器,返回句柄Word
Word = actxserver('Word.Applicatio n');
en d;
%设置Word属性为可见
Word.Visible = 1; % 或set(Word, 'Visible', 1);
%若测试文件存在,打开该测试文件,否则,新建一个文件,并保存,文件名为测试
if exist(filespec_user,'file');
Docume nt = Word.Docume nts.Ope n(filespec_user);
% Docume nt = in voke(Word.Docume nts,'Ope n',filespec_user);
else
Docume nt = Word.Docume nts.Add;
% Docume nt = in voke(Word.Docume nts, 'Add');
Docume nt.SaveAs(filespec_user);
end
Content = Document.Content; % 返回Content 接口句柄
Selection = Word.Selection; % 返回Selection 接口句柄
Paragraphformat = Selection.ParagraphFormat; % 返回ParagraphFormat 接口句柄
%页面设置
Docume nt.PageSetup.TopMarg in = 60;
Docume nt.PageSetup.BottomMarg in = 45;
Docume nt.PageSetup.LeftMarg in = 45;
Docume nt.PageSetup.RightMarg in = 45;
%设定文档内容的起始位置和标题
Con te nt.Start = 0; %设置文档内容的起始位置Word .doc
%上边距60磅%下边距45磅%左边距45磅%右边距45磅
title ='试卷分
析';
Conten t.Text = title;
%输入文字内容 Conten t.F on t.Size =
16 ; %设置字号为16 Co nten t.Fo nt.Bold =
4 ; %字体加粗 Con te nt.P aragraphs.Alig nment = 'wdAlig nParagraphCe nter :Select ion. TypeParagraph; xueqi = ' ( 2009
— 2010 学年第一学期)’;
Selection.Text = xueqi; % 在当前位置输入文字内容 Selectio n.Fo nt.Size = 12; % 设置字号为 12 Selectio n.Fo nt.Bold = 0; % 字体不加粗
Tables = Docume nt.Tables.Add(Selectio n.Ra nge,12,9); %返回第1个表格的句柄
DTI = Document.Tables.ltem(1); % 或 DTI = Tables; %设置表格边框
DTI.Borders.OutsideLi neStyle = 'wdLi neStyleSi ngle'; DTI.Borders.OutsideLi neWidth = 'wdL in eWidth150pt'; DTI.Borders.I nsideLi neStyle = 'wdLi neStyleSi ngle'; DTI.Borders.I nsideLi neWidth = 'wdL in eWidth150pt'; DTI.Rows.Alig nment = 'wdAlig nRowCe nter';
DTI.Rows.ltem(8).Borders.ltem(1).Li neStyle = 'wdL in eStyleNo ne'; DTI.Rows.ltem(8).Borders.lte m( 3).Li neStyle = 'wdL in eStyleNo ne';
DTI.Rows.Item(11).Borders.Item(1).Li neStyle = 'wdLi neStyleNo ne'; DTI.Rows.ltem(11).Borders.ltem(3).Li neStyle = 'wdLi neStyleNo ne'; %设置表格列宽和行高
column_width = [53.7736,85.1434,53.7736,35.0094,...
35.0094,76.6981,55.1887,52.9245,54.9057];
% 定义列宽向量
row_height = [28.5849,28.5849,28.5849,28.5849,25.4717,25.4717,...
32.8302,312.1698,17.8302,49.2453,14.1509,18.6792]; % 定义行高向量
%通过循环设置表格每列的列宽 for i = 1:9
DTI.Colu mn s.Item(i).Width = colu mn _width(i);
%居中对齐
Selectio n.Start = Conten t.e nd; %设定下面内容的起始位置
%回车,另起一段 Selectio n.MoveDow n;
%光标下移(取消选中)
paragraphformat.Alig nment = 'wdAlig nParagraphCe nter : Select ion. TypeParagraph; %回车,另起一段 Select ion. TypeParagraph;
%回车,另起一段
Selection.Font.Size = 10.5; % 设置字号为 10.5
%居中对齐
%在光标所在位置插入一个
12行9列的表格