又是一个新的小项目的整理(本文主要是Matlab 调用VBA语言生成Word相关)

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服务器

 

### 回答1: 可以,以下是matlab语言调用anubis画图软件生成图表的代码:figure; axes1 = axes('Parent',figure); anubis_plot(axes1,'x,y'); title('图表标题'); xlabel('x轴标题'); ylabel('y轴标题'); ### 回答2: 当然可以!下面是一段用MATLAB语言调用Anubis画图软件生成图表的代码。 首先,你需要安装好Anubis软件,并且将其路径配置到MATLAB中。这可以通过在MATLAB命令窗口中运行以下命令来完成: ```matlab setenv('ANUBIS_PATH', 'Anubis软件安装路径') ``` 接下来,你可以使用MATLAB的`plot`函数来生成图表的数据。例如,生成一个简单的正弦函数图表: ```matlab x = linspace(0, 2*pi, 100); % 生成0到2π的100个均匀间隔的数据点 y = sin(x); % 计算正弦函数的值 figure; % 打开一个的图形窗口 plot(x, y); % 使用plot函数绘制图表 xlabel('x'); % 添加x轴标签 ylabel('y'); % 添加y轴标签 title('Sin(x)'); % 添加标题 % 调用Anubis软件生成图表 anubis('save', 'path/to/save/image.png') % 将图表保存为PNG格式的图片,保存路径自行指定 ``` 在这个例子中,我们使用`plot`函数生成了正弦函数的图表,并添加了标签和标题。最后,我们使用`anubis`函数调用Anubis软件来保存图表为PNG格式的图片。 请注意,Anubis软件是第三方软件,我们需要将其路径配置到MATLAB中,并使用`anubis`函数来调用它。如果你尚未安装Anubis软件,可以在其官方网站上下载和安装它。 希望这段代码能够帮助到你! ### 回答3: 可以的,下面是一段使用MATLAB调用Anubis画图软件生成图表的示例代码: ```matlab % 在MATLAB调用Anubis画图软件生成图表 % 首先需要确保已经安装了Anubis和MATLAB软件,并添加了Anubis的图形绘制函数库 % 创建一个数据集用于绘图 x = linspace(0, 2*pi, 100); y = sin(x); % 使用Anubis画图软件生成图表 h = figure; % 创建一个的图表窗口 plot(x, y, 'r'); % 使用MATLAB的绘图函数绘制图形 % 将图表传递给Anubis进行处理 anubis('add figure', h); % 将图表窗口传递给Anubis进行处理 % 定义一些图表样式和参数 anubis('figure title', 'Sine Wave'); % 设置图表标题 anubis('xlabel', 'X'); % 设置X轴标签 anubis('ylabel', 'Y'); % 设置Y轴标签 anubis('grid', 'on'); % 显示网格线 % 显示图表 anubis('show figure'); ``` 这段代码用于在MATLAB生成一条正弦曲线,并将生成的图表传递给Anubis进行进一步处理和显示。在使用这段代码之前,请确保已经正确安装了Anubis和MATLAB软件,并在MATLAB中添加了Anubis的图形绘制函数库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值