VBA语言,懂得都懂,大概贴一贴代码,这样方便日后查看
%利用MATLAB生成Word文档
filespec_user = [pwd '\test2.0.docx'];% 设定测试Word文件名和路径
% 判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
% filespec_user = [pwd '\高空风场+浅风层.docx'];% 设定测试Word文件名和路径
% 判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
try
% 若Word服务器已经打开,返回其句柄Word
Word = actxGetRunningServer('Word.Application');
catch
% 否则,创建一个Microsoft Word服务器,返回句柄Word
Word = actxserver('Word.Application');
end;
Word.Visible = 1; % 或set(Word, 'Visible', 1);
% 若测试文件存在,打开该测试文件,否则,新建一个文件,并保存,文件名为测试.doc
if exist(filespec_user,'file');
Document = Word.Documents.Open(filespec_user);
% Document = invoke(Word.Documents,'Open',filespec_user);
else
Document = Word.Documents.Add;
% Document = invoke(Word.Documents, 'Add');
Document.SaveAs2(filespec_user);%在这句话没有注释的时候要保证word页面打开没有弹窗而且得是注册过的
end
% 设定光标位置从头开始
Content = Document.Content;
Selection = Word.Selection;
Paragraphformat = Selection.ParagraphFormat;
% 设定页面大小
Document.PageSetup.TopMargin = 70.9; % 单位像素
Document.PageSetup.BottomMargin = 70.9;
Document.PageSetup.LeftMargin = 62.35;
Document.PageSetup.RightMargin = 62.35;
%后面的这一段主要是将页面进行旋转所需要的操作
% Document.PageSetup.TopMargin = 62.35; % 单位像素
% Document.PageSetup.BottomMargin = 62.35;
% Document.PageSetup.LeftMargin = 70.9;
% Document.PageSetup.RightMargin = 70.9;
% Document.PageSetup.Orientation = 'wdOrientLandscape';%横向页面
% Document.PageSetup.Orientation = 'wdOrientPortrait';%竖向页面
% Content.InsertParagraphAfter;% 插入一段
% Content.Collapse=0; % 0为不覆盖
Content.Start = 0;%定义标题开始位置,如果要接着下一句就是将0替换成Selection.end
title = [];
Content.Text = title;
Content.Font.Size = 18 ;
Content.Font.Bold = 0 ;
Content.Font.Name="方正小标宋简体";
Content.ParagraphFormat.LineSpacing = 1*Content.Font.Size;
Content.Paragraphs.Alignment = 'wdAlignParagraphCenter';% 设定段落格式
Selection.Start = Content.end;% 定义开始的位置
Selection.TypeParagraph;
% 插入内容并定义字体字号
% 对报告的页码进行规范性编写
xueqi = [];
Selection.Text = xueqi;
Paragraphformat.Alignment = 'wdAlignParagraphCenter';
Selection.Font.Size = 14;
Selection.Font.Name="仿宋";
Selection.Font.Name="Times New Roman";
Selection.Font.Bold = 0;
Selection.MoveDown;
Selection.TypeParagraph;
Selection.Font.Size = 12;
Tables = Document.Tables.Add(Selection.Range,7,12);
DTI = Document.Tables.Item(1); % 或DTI = Tables;
DTI.Borders.OutsideLineStyle = 'wdLineStyleSingle';% 设置外边框的线型,Dash,DashDot,DashDotDot,DashSmallGap,DashLargeGap,Dot,Double,Triple等%没有就是none然后把线宽啥的都要注释
DTI.Borders.OutsideLineWidth = 'wdLineWidth050pt';% 设置线宽,有025,050,075,100,150,225,300,450,600pt等
DTI.Borders.InsideLineStyle = 'wdLineStyleSingle';%设置内边框的线型
DTI.Borders.InsideLineWidth = 'wdLineWidth050pt';
DTI.Rows.Alignment = 'wdAlignRowCenter';%设置行对齐方式
column_width = [42.3,60.3,60.3,60.3,60.3,60.3,60.3,60.3,60.3,60.3,60.3,60.3];% 设置列宽,单位为磅
row_height = [56.7,45.4,45.4,45.4,45.4,45.4,56.7]; % 设置行高
for i = 1:12
DTI.Columns.Item(i).Width = column_width(i);
end
for i = 1:7
DTI.Rows.Item(i).Height = row_height(i);
end
for i = 1:7
for j = 1:12
DTI.Cell(i,j).VerticalAlignment = 'wdCellAlignVerticalCenter';% 设置单元格竖直对齐方式,有Bottome,Center,Top
end
end
DTI.Cell(7, 2).Merge(DTI.Cell(7, 12));
% 指定各单元格内容
%生成表头的时间序列
DTI.Cell(1,1).Range.Text = '时间';
DTI.Cell(2,1).Range.Text = ['风速',32,'m/s'];
DTI.Cell(3,1).Range.Text = ['风向' char(13,10)' '°'];
DTI.Cell(4,1).Range.Text = '雾';
DTI.Cell(5,1).Range.Text = ['降水',32,'mm/h'];
DTI.Cell(6,1).Range.Text = ['天气',32,'状况'];
DTI.Cell(7,1).Range.Text = ['窗口',32,'研判'];
for i = 1:length(time_convert)
DTI.Cell(1,i+1).Range.Text =time_convert{i};
end
%DTI.Cell(3, 1).Merge(DTI.Cell(3,2));%这一句是合并单元格
zft = figure('units','normalized','visible','off'); % 新建图形窗口,设为不可见
set(gca); % 设置坐标系的位置和大小
plot();
% 将图形复制到粘贴板
hgexport(zft, '-clipboard');
DTI.Cell(1,1).Range.Paragraphs.Item(1).Range.Paste; % 设置图片为嵌入式,与图片为浮于文字上方相互转化为ConvertToInlineShape,ConvertToShape
delete(zft); % 删除图形句柄
DTI.Cell(7,2).Range.ParagraphFormat.Alignment = 'wdAlignParagraphLeft';
DTI.Cell(7,2).VerticalAlignment = 'wdCellAlignVerticalCenter';% 设定单元格对齐方式
DTI.Cell(7,2).Range.Text = str_ckyp;
Selection.Start = Content.end; % 设定光标位置为最后
Selection.Text = [str_zhu1 ybdd_1 str_zhu2 WDsjy str_zhu3];
Selection.Font.Name="Times New Roman";
Selection.ParagraphFormat.LineSpacing = 23;
Paragraphformat.Alignment = 'wdAlignParagraphLeft';
% Document.ActiveWindow.ActivePane.View.Type = 'wdPrintView'; % 设置视图方式为页面
% Document.Save; % 保存文档
% Document.Close; % 关闭文档
% Word.Quit; % 退出word服务器