利用MATLAB生成Word和Excel文档
利用MATLAB生成Word和Excel文档

二、actxcontrollist函数 查看系统上当前安装的所有COM控件
>> C = actxcontห้องสมุดไป่ตู้ollist % 查看系统上当前安装的所有COM控件
三、actxcontrolselect函数
h = actxcontrolselect [h, info] = actxcontrolselect
二、COM接口 1. IUnknown接口 接口查询 生存期控制
2. IDispatch接口 IDispatch接口接收一个函数名并执行它
第二节 MATLAB中的ActiveX控件接口技术
一、actxcontrol函数
h = actxcontrol('progid') h = actxcontrol('progid','param1',value1,…) h = actxcontrol('progid', position) h = actxcontrol('progid', position, fig_handle) h = actxcontrol('progid',position,fig_handle,event_handler) h = actxcontrol('progid',position,fig_handle,event_handler,'filename')
'interface', 'interfaceName') h = actxserver('progid', machine)
【例2】通过MATLAB控制Windows Media Player 播放歌曲
MATLAB与Office交互使用

MATLAB与其他软件的交互使用杨文霞matlab提供了与microsoft公司的office系列软件的交互使用和便利的数据传输,使得用户能在word环境、Powerpoint中自如的使用matlab进行计算,为用户营造集文字处理、科学计算和工程设计于一体的工作环境。
本章介绍在数学建模和数学实验中最常用的两个软件Word及Excel与matlab的交互使用和数据传输。
1Word中使用Matlab在Word中使用Matlab主要通过matlab的Notebook功能实现的。
Notebook使得用户能够在word环境中使用matlab,使用户能将文字处理与科学计算放在一个工作环境中,方便用户使用。
Matlab制作的m-book文档不仅拥有ms-word的全部功能,而且具备matlab的数学结算能力。
在word中使用matlab的基本步骤为:(1)notebook的安装在command window中,键入:notebook,运行后,会出现如下界面:Welcome to the utility for setting up the MATLAB Notebookfor interfacing MATLAB to Microsoft WordChoose your version of Microsoft Word:[1] Microsoft Word 97[2] Microsoft Word 2000[3] Microsoft Word 2002 (XP)[4] Exit, making no changes选择适合的matlab版本后,Microsoft Word Version: 3Notebook setup is complete.此后,会打开一个word文档:即表示安装结束。
1.notebook的启动(1)从word中启动notebook打开一个word空白文档后,在文档的右边将会出现一个新建文档的选项:在模板选项下,选择:本机上的模板,则会出现如下对话框:选择m-book.dot模板。
MATLAB统计分析与应用:40个案例分析

MATLAB统计分析与应用:40个案例分析ISBN:00849 分类号:C819 /115 出版社:北京航空航天大学出版社【内容简介】本书从实际应用的角度出发,以大量的案例详细介绍了MATLAB环境下的统计分析与应用。
本书主要内容包括:利用MATLAB制作统计报告或报表;从文件中读取数据到MATLAB;从MATLAB中导出数据到文件;数据的平滑处理、标准化变换和极差归一化变换;生成一元和多元分布随机数;蒙特卡洛方法;参数估计与假设检验;Copula理论及应用实例;方差分析;基于回归分析的数据拟合;聚类分析;判别分析;主成分分析;因子分析;图像处理中的统计应用等。
本书可以作为高等院校本科生、研究生的统计学相关课程的教材或教学参考书,也可作为从事数据分析与数据管理的研究人员的参考用书。
【目录】第1章利用MATLAB生成Word和Excel文档组件对象模型(COM)1.1.1 什么是CoMCoM接口MATLAB中的ActiveX控件接口技术actxcontrol函数actxcontrollist函数actxcontrolselect函数actxserver函数利用MATLAB调用COM对象调用actxserver函数创建组件服务器案例1:利用MATLAB生成Word文档调用actxserver函数创建Microsoft Word服务器建立Word文本文档插入表格插入图片保存文档完整代码案例2:利用MATLAB生成Excel文档调用actxserver函数创建Microsoft Excel服务器新建Excel工作簿获取工作表对象句柄插入、复制、删除、移动和重命名工作表页面设置选取工作表区域设置行高和列宽合并单元格边框设置设置单元格对齐方式写入单元格内容插入图片保存工作簿完整代码第2章数据的导入与导出案例3:从TXT文件中读取数据利用数据导入向导导入TXT文件调用高级函数读取数据调用低级函数读取数据案例4:把数据写入TXT文件调用dlmread函数写入数据调用fprintf函数写入数据案例5:从Excel文件中读取数据利用数据导入向导导入Excel文件调用xlsread函数读取数据案例6:把数据写入Excel文件第3章数据的预处理案例7:数据的平滑处理smooth函数smoothts函数medfiltl函数案例8:数据的标准化变换标准化变换公式标准化变换的MATLAB实现案例9:数据的极差归一化变换极差归一化变换公式极差归一化变换的MATLAB实现第4章生成随机数案例10:生成一元分布随机数均匀分布随机数和标准正态分布随机数 RandStream类常见一元分布随机数任意一元分布随机数案例11:生成多元分布随机数案例12:蒙特卡洛方法有趣的蒙提霍尔问题抽球问题的蒙特卡洛模拟用蒙特卡洛方法求圆周率用蒙特卡洛方法求积分街头骗局揭秘第5章参数估计与假设检验案例13:常见分布的参数估计案例14:正态总体参数的检验……第6章 Copula理论及应用实例第7章方差分析第8章数据拟合第9章聚类分析第10章判别分析第11章主成分分析第12章因子分析附录A 图像处理中的统计应用案例附录B MATLAB统计工具箱函数大全参考文献。
利用MATLAB生成Word文档

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服务器已经打开,返回其句柄WordWord = actxGetR unnin gServer('Word.Applicati on');catch%创建一个Microsoft Word服务器,返回句柄WordWord = 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);elseDocume nt = Word.Docume nts.Add;% Docume nt = in voke(Word.Docume nts, 'Add');Docume nt.SaveAs(filespec_user);endContent = 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:9DTI.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列的表格end%通过循环设置表格每行的行高for i = 1:12DTI.Rows.ltem(i).Height = row_height(i);end%通过循环设置每个单元格的垂直对齐方式for i = 1:12for j = 1:9DTI.Cell(i,j).VerticalAlig nment = 'wdCellAlig nV erticalCe nter'; endend%合并单元格DTI.Cell(1,4).Merge(DTI.Cell(1,5));DTI.Cell(2, 4).Merge(DTI.Cell(2, 5));DTI.Cell(3, 4).Merge(DTI.Cell(3, 5));DTI.Cell(4, 4).Merge(DTI.Cell(4, 5));DTI.Cell(5, 2).Merge(DTI.Cell(5, 5));DTI.Cell(5, 3).Merge(DTI.Cell(5, 6));DTI.Cell(6, 2).Merge(DTI.Cell(6, 5));DTI.Cell(6, 3).Merge(DTI.Cell(6, 6));DTI.Cell(5, 1).Merge(DTI.Cell(6, 1));DTI.Cell(7, 1).Merge(DTI.Cell(7, 9));DTI.Cell(8, 1).Merge(DTI.Cell(8, 9));DTI.Cell(9, 1).Merge(DTI.Cell(9, 3));DTI.Cell(9, 2).Merge(DTI.Cell(9, 3));DTI.Cell(9, 3).Merge(DTI.Cell(9, 4));DTI.Cell(9, 4).Merge(DTI.Cell(9, 5));DTI.Cell(10, 1).Merge(DTI.Cell(10, 9));DTI.Cell(11,5).Merge(DTI.Cell(11,9));DTI.Cell(12, 5).Merge(DTI.Cell(12, 9));DTI.Cell(11, 1).Merge(DTI.Cell(12, 4));Selection.Start = Content.end; % 设置光标位置在文档内容的结尾Select ion. TypeParagraph; %回车,另起一段Selection.Text ='主管院长签字:年月日';%输入文字内容Paragraphformat.Alig nment = 'wdAlig nParagraphRight'; % 右对齐Selection.MoveDown; % 光标下移%写入表格内容DTI.Cell(1,1).Range.Text ='课程名称';DTI.Cell(1,3).Range.Text ='课程号';DTI.Cell(1,5).Range.Text ='任课教师学院’;DTI.Cell(1,7).Range.Text ='任课教师 DTI.Cell(2,1).Range.Text ='授课班级 DTI.Cell(2,3).Range.Text ='考试日期 DTI.Cell(2,5).Range.Text ='应考人数 DTI.Cell(2,7).Range.Text ='实考人数 DTI.Cell(3,1).Range.Text ='出卷方式 DTI.Cell(3,3).Range.Text ='阅卷方式'; DTI.Cell(3,5).Range.Text ='选用试卷 A/B : DTI.Cell(3,7).Range.Text ='考试时间'; DTI.Cell(4,1).Range.Text ='考试方式'; DTI.Cell(4,3).Range.Text ='平均分'; DTI.Cell(4,5).Range.Text ='不及格人数 DTI.Cell(4,7).Range.Text ='及格率'; DTI.Cell(5,1).Range.Text ='成绩分布'; DTI.Cell(5,2).Range.Text = '90 分以上 DTI.Cell(5,3).Ra nge.Text = '80---89 分 DTI.Cell(6,2).Ra nge.Text = '70--79 分 DTI.Cell(6,3).Ra nge.Text = '60---69 分DTI.Cell(7,1).Ra nge.Text =['试卷分析(含是否符合教学大纲、难度、知识覆'盖面、班级分数分布分析、学生答题存在的共性问题与知识掌握情况、教学中 '存在的问题及改进措施等内容) '];DTI.Cell(7,1).Ra nge.ParagraphFormat.Alig nment = 'wdAlig nParagraphLeft :DTI.Cell(9,2).Range.Text ='签字:: DTI.Cell(9,4).Range.Text ='年 月 日'; DTI.Cell(10,1).Range.Text ='教研室审阅意见:’;DTI.Cell(10,1).Ra nge.ParagraphFormat.Alig nment = 'wdAlig nParagraphLeft'; DTI.Cell(10,1).VerticalAlig nment = 'wdCellAlig nVerticalTop'; DTI.Cell(11,2).Range.Text ='教研室主任(签字):年 月 日';DTI.Cell(11,2).Ra nge.ParagraphFormat.Alig nment = 'wdAlig nParagraphLeft'; DTI.Cell(8,1).Ra nge.P aragraphFormat.Alig nment = 'wdAlig nParagraphLeft'; DTI.Cell(8,1).VerticalAlig nment = 'wdCellAlig nV erticalTop'; DTI.Cell(9,2).Borders.ltem(2).L in eStyle = 'wdLi neStyleN on e'; DTI.Cell(9,2).Borders.ltem(4).L in eStyle = 'wdLi neStyleN on e'; DTI.Cell(9,3).Borders.Item(4).L in eStyle = 'wdLi neStyleN on e'; DTI.Cell(11,1).Borders.Item(4).Li neStyle = 'wdL in eStyleNo ne'; %如果当前工作文档中有图形存在,通过循环将图形全部删除Shape = Document.Shapes; % 返回 Shapes 接口的句柄 ShapeCount = Shape.Count; % 返回文档中 Shape 对象的个数 if ShapeCou nt ~= 0;for i = 1:ShapeCo unt;Shape.Item(1).Delete; % 删除第 1 个 Shape 对象 en d;en d;%产生标准正态分布随机数,画直方图,并设置图形属性人占%'; 人占 %'; 人占%'; 人占%';zft = figure(' un its',' no rmalized','positi on',...[0.280469 0.553385 0.428906 0.251302],'visible','off); % 新建图形窗口,设为不可见set(gca,'position',[0.1 0.2 0.85 0.75]); % 设置坐标系的位置和大小data = normrnd(0,1,1000,1); % 产生标准正态分布随机数hist(data); %绘制标准正态分布随机数的频数直方图grid on; % 添加参考网格xlabel('考试成绩'); %为X轴加标签ylabel('人数'); %为Y轴加标签%将图形复制到粘贴板hgexport(zft, '-clipboard');%将图形粘贴到当前文档里(表格的第8行第1列的单元格里),并设置图形版式为浮于文字上方% Select ion .Ra nge.PasteSpecial;DTI.Cell(8,1).Ra nge. Paragraphs.ltem(1).Ra nge. PasteSpecial;Shape.ltem(1).WrapFormat.Type = 3;Shape.ltem(1).ZOrder('msoBringlnFrontOfText'); % 设置图片叠放次序为浮于文字上方delete(zft); %删除图形句柄Document.ActiveWindow.ActivePane.View.Type = 'wdPrintView'; % 设置视图方式为页面Document.Save; % 保存文档口Love is n ot a maybe thing. You know whe n you love some one.。
MATLAB生成Word和Excel文档-PPT精选文档

的静态程序,而是随时可以用新组件取代旧组件而
返老还童的动态程序。
2019/3/17
©
谢中华, 天津科技大学数学系.
论坛报告会
二、COM接口 1. IUnknown接口 接口查询
生存期控制
2. IDispatch接口 IDispatch接口接收一个函数名并执行它
2019/3/17
©谢中华, 天ຫໍສະໝຸດ 科技大学数学系.论坛报告会
利用MATLAB生成Word和Excel文档
天 津 科 技 大 学 数 学 系 谢中华
E-mail:
2019/3/17
©
谢中华, 天津科技大学数学系.
论坛报告会
我的新书
感谢赵社长和陈守平编辑!
感谢rocwoods(吴鹏)!
感谢math! 感谢各位版友的捧场!
论坛报告会
第二节 MATLAB中的ActiveX控件接口技术
一、actxcontrol函数
h = actxcontrol('progid') h = actxcontrol('progid','param1',value1,…) h = actxcontrol('progid', position) h = actxcontrol('progid', position, fig_handle) h = actxcontrol('progid',position,fig_handle,event_handler) h = actxcontrol('progid',position,fig_handle,event_handler,'filename')
Matlab输出内容到Excel的方法(WPS可用)

return; end; file= [Pathname Filename];
try Excel = actxGetRunningServer('Excel.Application');
Sheets = Excel.ActiveWorkbook.Sheets; Sheets.Item(1).name='数据'; Sheet1 = Sheets.Item(1); Sheet1.Activate; Sheet1.Range('A1').Value ='aaa'; Sheet1.Range('B1').Value='bbb';
catch Excel = actxserver('Excel.Application');
end; %Excel.Visible = 1;
if exist(file,'file') delete(file);
end
Workbook = Excel.Workbooks.Add; Workbook.SaveAs(file);
登录后才能查看或发表评论立即登录或者逛逛博客园首页
Matlab输出内容到 Excel的方法( WPS可用)
本文主要用于将Matlab内容输出到指定excel文件,具有如下功能:
可自定义文件路径 如果路径下文件不存在,则新建文件 如果路径下文件重名,则替换文件 由于本人安装的WPS,使用xlswrite命令会报错,使用以下方法亲测可行
利用MATLAB生成WORD和EXCEL文档

function ceshi_Word%利用MATLAB生成Word文档%ceshi_Word%%Copyright2009-2010xiezhh.%$Revision:1.0.0.0$$Date:2009/12/1412:37:00$%设定测试Word文件名和路径filespec_user=[pwd'\测试.doc'];%判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word try%若Word服务器已经打开,返回其句柄WordWord=actxGetRunningServer('Word.Application');catch%创建一个Microsoft Word服务器,返回句柄WordWord=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);elseDocument=Word.Documents.Add;%Document=invoke(Word.Documents,'Add');Document.SaveAs(filespec_user);endContent=Document.Content;Selection=Word.Selection;Paragraphformat=Selection.ParagraphFormat;Document.PageSetup.TopMargin=60;Document.PageSetup.BottomMargin=45;Document.PageSetup.LeftMargin=45;Document.PageSetup.RightMargin=45;Content.Start=0;title='试卷分析';Content.Text=title;Content.Font.Size=16;Content.Font.Bold=4;Content.Paragraphs.Alignment='wdAlignParagraphCenter';Selection.Start=Content.end;Selection.TypeParagraph;xueqi='(2009—2010学年第一学期)'; Selection.Text=xueqi;Selection.Font.Size=12;Selection.Font.Bold=0;Selection.MoveDown; paragraphformat.Alignment='wdAlignParagraphCenter'; Selection.TypeParagraph;Selection.TypeParagraph;Selection.Font.Size=10.5;Tables=Document.Tables.Add(Selection.Range,12,9);DTI=Document.Tables.Item(1);%或DTI=Tables;DTI.Borders.OutsideLineStyle='wdLineStyleSingle';DTI.Borders.OutsideLineWidth='wdLineWidth150pt';DTI.Borders.InsideLineStyle='wdLineStyleSingle';DTI.Borders.InsideLineWidth='wdLineWidth150pt';DTI.Rows.Alignment='wdAlignRowCenter';DTI.Rows.Item(8).Borders.Item(1).LineStyle='wdLineStyleNone';DTI.Rows.Item(8).Borders.Item(3).LineStyle='wdLineStyleNone';DTI.Rows.Item(11).Borders.Item(1).LineStyle='wdLineStyleNone';DTI.Rows.Item(11).Borders.Item(3).LineStyle='wdLineStyleNone';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:9DTI.Columns.Item(i).Width=column_width(i);endfor i=1:12DTI.Rows.Item(i).Height=row_height(i);endfor i=1:12for j=1:9DTI.Cell(i,j).VerticalAlignment='wdCellAlignVerticalCenter';endendDTI.Cell(1,4).Merge(DTI.Cell(1,5));DTI.Cell(2,4).Merge(DTI.Cell(2,5));DTI.Cell(3,4).Merge(DTI.Cell(3,5));DTI.Cell(4,4).Merge(DTI.Cell(4,5));DTI.Cell(5,2).Merge(DTI.Cell(5,5));DTI.Cell(5,3).Merge(DTI.Cell(5,6));DTI.Cell(6,2).Merge(DTI.Cell(6,5));DTI.Cell(6,3).Merge(DTI.Cell(6,6));DTI.Cell(5,1).Merge(DTI.Cell(6,1));DTI.Cell(7,1).Merge(DTI.Cell(7,9));DTI.Cell(8,1).Merge(DTI.Cell(8,9));DTI.Cell(9,1).Merge(DTI.Cell(9,3));DTI.Cell(9,2).Merge(DTI.Cell(9,3));DTI.Cell(9,3).Merge(DTI.Cell(9,4));DTI.Cell(9,4).Merge(DTI.Cell(9,5));DTI.Cell(10,1).Merge(DTI.Cell(10,9));DTI.Cell(11,5).Merge(DTI.Cell(11,9));DTI.Cell(12,5).Merge(DTI.Cell(12,9));DTI.Cell(11,1).Merge(DTI.Cell(12,4));Selection.Start=Content.end;Selection.TypeParagraph;Selection.Text='主管院长签字:年月日'; Paragraphformat.Alignment='wdAlignParagraphRight';Selection.MoveDown;DTI.Cell(1,1).Range.Text='课程名称';DTI.Cell(1,3).Range.Text='课程号';DTI.Cell(1,5).Range.Text='任课教师学院';DTI.Cell(1,7).Range.Text='任课教师';DTI.Cell(2,1).Range.Text='授课班级';DTI.Cell(2,3).Range.Text='考试日期';DTI.Cell(2,5).Range.Text='应考人数';DTI.Cell(2,7).Range.Text='实考人数';DTI.Cell(3,1).Range.Text='出卷方式';DTI.Cell(3,3).Range.Text='阅卷方式';DTI.Cell(3,5).Range.Text='选用试卷A/B';DTI.Cell(3,7).Range.Text='考试时间';DTI.Cell(4,1).Range.Text='考试方式';DTI.Cell(4,3).Range.Text='平均分';DTI.Cell(4,5).Range.Text='不及格人数';DTI.Cell(4,7).Range.Text='及格率';DTI.Cell(5,1).Range.Text='成绩分布';DTI.Cell(5,2).Range.Text='90分以上人占%';DTI.Cell(5,3).Range.Text='80---89分人占%';DTI.Cell(6,2).Range.Text='70--79分人占%';DTI.Cell(6,3).Range.Text='60---69分人占%';DTI.Cell(7,1).Range.Text=['试卷分析(含是否符合教学大纲、难度、知识覆'...'盖面、班级分数分布分析、学生答题存在的共性问题与知识掌握情况、教学中'...'存在的问题及改进措施等内容)'];DTI.Cell(7,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';DTI.Cell(9,2).Range.Text='签字:';DTI.Cell(9,4).Range.Text='年月日';DTI.Cell(10,1).Range.Text='教研室审阅意见:';DTI.Cell(10,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';DTI.Cell(10,1).VerticalAlignment='wdCellAlignVerticalTop';DTI.Cell(11,2).Range.Text='教研室主任(签字):年月日';DTI.Cell(11,2).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';DTI.Cell(8,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';DTI.Cell(8,1).VerticalAlignment='wdCellAlignVerticalTop';DTI.Cell(9,2).Borders.Item(2).LineStyle='wdLineStyleNone';DTI.Cell(9,2).Borders.Item(4).LineStyle='wdLineStyleNone';DTI.Cell(9,3).Borders.Item(4).LineStyle='wdLineStyleNone';DTI.Cell(11,1).Borders.Item(4).LineStyle='wdLineStyleNone';Shape=Document.Shapes;ShapeCount=Shape.Count;if ShapeCount~=0;for i=1:ShapeCount;Shape.Item(1).Delete;end;end;%产生标准正态分布随机数,画直方图,并设置图形属性zft=figure('units','normalized','position',...[0.2804690.5533850.4289060.251302],'visible','off');%新建图形窗口,设为不可见set(gca,'position',[0.10.20.850.75]);%设置坐标系的位置和大小data=normrnd(0,1,1000,1);%产生标准正态分布随机数hist(data);%绘制标准正态分布随机数的频数直方图grid on;%添加参考网格xlabel('考试成绩');%为X轴加标签ylabel('人数');%为Y轴加标签%将图形复制到粘贴板hgexport(zft,'-clipboard');%将图形粘贴到当前文档里(表格的第8行第1列的单元格里),并设置图形版式为浮于文字上方%Selection.Range.PasteSpecial;DTI.Cell(8,1).Range.Paragraphs.Item(1).Range.PasteSpecial;Shape.Item(1).WrapFormat.Type=3;Shape.Item(1).ZOrder('msoBringInFrontOfText');%设置图片叠放次序为浮于文字上方delete(zft);%删除图形句柄Document.ActiveWindow.ActivePane.View.Type='wdPrintView';%设置视图方式为页面Document.Save;%保存文档function ceshi_Excel%利用MATLAB生成Excel文档%ceshi_Excel%%Copyright2009-2010xiezhh.%$Revision:1.0.0.0$$Date:2009/12/1420:40:00$filespec_user=[pwd'\测试.xls'];tryExcel=actxGetRunningServer('Excel.Application');catchExcel=actxserver('Excel.Application');end;Excel.Visible=1;%set(Excel,'Visible',1);if exist(filespec_user,'file');Workbook=Excel.Workbooks.Open(filespec_user);%Workbook=invoke(Excel.Workbooks,'Open',filespec_user);elseWorkbook=Excel.Workbooks.Add;%Workbook=invoke(Excel.Workbooks,'Add');Workbook.SaveAs(filespec_user);endSheets=Excel.ActiveWorkbook.Sheets;%Sheets=Workbook.Sheets; Sheet1=Sheets.Item(1);Sheet1.Activate;Sheet1.PageSetup.TopMargin=60;Sheet1.PageSetup.BottomMargin=45;Sheet1.PageSetup.LeftMargin=45;Sheet1.PageSetup.RightMargin=45;RowHeight=[26,22,15,29,37,29,29,25,25,36,280,31,40,29,15,24]'; Sheet1.Range('A1:A16').RowHeight=RowHeight;Sheet1.Range('A1:H1').ColumnWidth=[9,15,9,9,9,9,9,9];Sheet1.Range('A1:H1').MergeCells=1;Sheet1.Range('A2:H2').MergeCells=1;Sheet1.Range('A8:A9').MergeCells=1;Sheet1.Range('B8:D8').MergeCells=1;Sheet1.Range('E8:H8').MergeCells=1;Sheet1.Range('B9:D9').MergeCells=1;Sheet1.Range('E9:H9').MergeCells=1;Sheet1.Range('A10:H10').MergeCells=1;Sheet1.Range('A11:H11').MergeCells=1;Sheet1.Range('A12:H12').MergeCells=1;Sheet1.Range('A13:H13').MergeCells=1;Sheet1.Range('A14:H14').MergeCells=1;Sheet1.Range('D16:H16').MergeCells=1;Sheet1.Range('A4:H14').Borders.Weight=3;Sheet1.Range('A10:H12').Borders.Item(3).Linestyle=0;Sheet1.Range('A10:H12').Borders.Item(4).Linestyle=0;Sheet1.Range('A13:H13').Borders.Item(4).Linestyle=0;Sheet1.Range('A14:H14').Borders.Item(3).Linestyle=0;Sheet1.Range('A1:H9').HorizontalAlignment=3;Sheet1.Range('A4:A9').HorizontalAlignment=6;Sheet1.Range('C4:C7').HorizontalAlignment=6;Sheet1.Range('E4:E7').HorizontalAlignment=6;Sheet1.Range('G4:G7').HorizontalAlignment=6;Sheet1.Range('A10:H10').HorizontalAlignment=6;Sheet1.Range('A11:H11').HorizontalAlignment=6;%-4130 Sheet1.Range('A11:H11').VerticalAlignment=1;Sheet1.Range('A12:H12').HorizontalAlignment=4;Sheet1.Range('A13:H13').VerticalAlignment=1;Sheet1.Range('A14:H14').HorizontalAlignment=4;Sheet1.Range('D16:H16').HorizontalAlignment=4;Sheet1.Range('A1').Value='试卷分析';Sheet1.Range('A2').Value='(2009—2010学年第一学期)'; Sheet1.Range('A4:H4').Value={'课程名称','','课程号',...'','任课教师学院','','任课教师',''};Sheet1.Range('A5:H5').Value={'授课班级','','考试日期',...'','应考人数','','实考人数',''};Sheet1.Range('A6:H6').Value={'出卷方式','','阅卷方式',...'','选用试卷A/B','','考试时间',''};Sheet1.Range('A7:H7').Value={'考试方式','','平均分',...'','不及格人数','','及格率',''};Sheet1.Range('A8').Value='成绩分布';Sheet1.Range('B8').Value='90分以上人占%';Sheet1.Range('E8').Value='80---89分人占%';Sheet1.Range('B9').Value='70---79分人占%';Sheet1.Range('E9').Value='60---69分人占%';Sheet1.Range('A10').Value=['试卷分析(含是否符合教学大纲、难度、知识覆'...'盖面、班级分数分布分析、学生答题存在的共性问题与知识掌握情况、教学中'...'存在的问题及改进措施等内容)'];Sheet1.Range('A12').Value='签字:年月日'; Sheet1.Range('A13').Value='教研室审阅意见:';Sheet1.Range('A14').Value='教研室主任(签字):年月日'; Sheet1.Range('D16').Value='主管院长签字:年月日';Sheet1.Range('A4:H12').Font.size=10.5;Sheet1.Range('A1').Font.size=16;Sheet1.Range('A1').Font.bold=2;Shapes=Sheet1.Shapes;if Shapes.Count~=0;for i=1:Shapes.Count;Shapes.Item(1).Delete;end;end;zft=figure('units','normalized','position',...[0.2804690.5533850.4289060.251302],'visible','off');set(gca,'position',[0.10.20.850.75]);data=normrnd(75,4,1000,1);hist(data);grid on;xlabel('考试成绩');ylabel('人数');hgexport(zft,'-clipboard');Sheet1.Range('A11').Select;Sheet1.Paste%Sheet1.PasteSpecial;delete(zft);Workbook.Save%保存文档。
调用Matlab函数制作Excel插件

说明:实现Matlab帮助文件中的例子,打开help/Matlab Build EX/Microsoft Excel Add‐In Integration/Building and Integrating a COM Component Using Microsoft Visual Basic: the Spectral Analysis Example如下图:主要步骤:1.从Matlab代码中创建一个独立的COM组件。
2.实现必要的VBA代码来完成输入和对代码的调用3.创建图形用户界面4.创建一个Excel插件并对所有必要的组件进行打包第一步:创建组件:从Matlab中复制computefft.m和plotfft.m到当前的工作目录。
按照下列步骤创建组件:1.输入mbuild –setup,选择VC的编译器2.输入deploytool,按照一下提示输入Note:有时候在自己的工作目录下编译不成功,可以把工程文件和所需的m文件copy到默认的matlab的目录下试试可能的工作:第二步:将组件集成到VBA中1.启动Excel2.点击开发工具/Visual Basic3.点击:工具/引用4.选择“Fourier 1.0 Type Library”和“MWComUtil 7.15 Type Library”创建VBA主模块1.右键单击VBAProject,点击插入/模块2.在模块的属性页修改“名称”属性为“FourierMain”3.在模块中输入一下代码:'' FourierMain ‐ Main module stores global state of controls' and provides initialization code'Public theFourier As Fourier.Fourier 'Global instance of Fourier objectPublic theFFTData As MWComplex 'Global instance of MWComplex to accept FFTPublic InputData As Range 'Input data rangePublic Interval As Double 'Sampling intervalPublic Frequency As Range 'Output frequency data rangePublic PowerSpect As Range 'Output power spectral density rangePublic bPlot As Boolean 'Holds the state of plot flagPublic theUtil As MWUtil 'Global instance of MWUtil objectPublic bInitialized As Boolean 'Module‐is‐initialized flagPrivate Sub LoadFourier()'Initializes globals and Loads the Spectral Analysis formDim MainForm As frmFourierOn Error GoTo Handle_ErrorCall InitAppSet MainForm = New frmFourierCall MainForm.ShowExit SubHandle_Error:MsgBox (Err.Description)End SubPrivate Sub InitApp()'Initializes classes and libraries. Executes once'for a given session of ExcelIf bInitialized Then Exit SubOn Error GoTo Handle_ErrorIf theUtil Is Nothing ThenSet theUtil = New MWUtilCall theUtil.MWInitApplication(Application)End IfIf theFourier Is Nothing ThenSet theFourier = New Fourier.FourierEnd IfIf theFFTData Is Nothing ThenSet theFFTData = New MWComplexEnd IfbInitialized = TrueExit SubHandle_Error:MsgBox (Err.Description)End Sub(注:帮助文件中有个地方有错)第三步:创建图形用户界面:1.右键单击VBAProject,点击插入/用户窗体2.在窗体的属性页:名称:frmFourier,Caption: Spectral Analysis3.给空白的窗体添加控件,属性设置如下:如下:右键单击窗体,查看代码:出入以下代码:第四步:添加Excel菜单1.右键单击This Workbook,查看代码,添加如下代码:Private Sub Workbook_AddinInstall()'Called when Addin is installedCall AddFourierMenuItemEnd SubPrivate Sub Workbook_AddinUninstall()'Called when Addin is uninstalledCall RemoveFourierMenuItemEnd SubPrivate Sub AddFourierMenuItem()Dim ToolsMenu As CommandBarPopupDim NewMenuItem As CommandBarButton'Remove if already existsCall RemoveFourierMenuItem'Find Tools menuSet ToolsMenu = mandBars(1).FindControl(ID:=30007) If ToolsMenu Is Nothing Then Exit Sub'Add Spectral Analysis menu itemSet NewMenuItem = ToolsMenu.Controls.Add(Type:=msoControlButton) NewMenuItem.Caption = "Spectral Analysis..."NewMenuItem.OnAction = "LoadFourier"End SubPrivate Sub RemoveFourierMenuItem()Dim CmdBar As CommandBarDim Ctrl As CommandBarControlOn Error Resume Next'Find tools menu and remove Spectral Analysis menu itemSet CmdBar = mandBars(1)Set Ctrl = CmdBar.FindControl(ID:=30007)Call Ctrl.Controls("Spectral Analysis...").DeleteEnd Sub保存插件:1.点击Excel,准备/文档属性2.选择:信息摘要,输入:Spectral Analysis3.另存为:Fourier.xlam第五步:测试1.启动一个新的Excel2.加载项,找到Fourier.xlam3.加载数据:点击Spectral Analysis输入数据:结果显示:如下第六步:打包点击Fourier.prj选择Package,加载一些必要的或者你想要的文件,可以添加MCR点击编译后面的打包(如何某个文件不存在,可以在相应的位置创建一个,保证打包程序运行)打包完成。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
©
谢中华, 天津科技大学数学系.
论坛报告会
六、识别COM对象、接口、属性、方法和事件
表2 调用方式 tf = h.iscom tf = iscom(h) tf = h.isinterface tf = isinterface(h) isprop(h, 'name') ismethod(h, 'name') tf = h.isevent('name') tf = isevent(h, 'name') 若字符串 name 指定的是 COM 对象 h 的一个属性, 则返回 1, 否则返回 0 若 name 指定的是一个在 COM 对象 h 上可调用的方法, 则返 回 1,否则返回 0 若 name 指定的是一个由 COM 对象 h 认可和响应的事件, 则 返回 tf 为 1,否则返回 tf 为 0 iscom 、isinterface、isprop、ismethod 和 isevent 函数 说 明
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
第三节 利用MATLAB生成Word文档
创建Microsoft Word服务器
建立Word文本文档
插入表格
插入图片
插入页眉、页码 插入公式 保存文档 完整案例
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
第二节 MATLAB中的ActiveX控件接口技术
一、actxcontrol函数
h = actxcontrol('progid') h = actxcontrol('progid','param1',value1,…) h = actxcontrol('progid', position) h = actxcontrol('progid', position, fig_handle) h = actxcontrol('progid',position,fig_handle,event_handler) h = actxcontrol('progid',position,fig_handle,event_handler,'filename')
2014-5-26
©
谢中华, 天津科技大学数学系.
Selection接口
个区域,并对所选区域进行相关操作。
论坛报告会
利用Word服务器的Selection接口可以在文档中选定一
% 返回Word服务器的Selection接口的句柄 >> Selection = Word. Selection; % 设置选定区域的起始位置为文档内容的末尾 >> Selection.Start = Content.end; >> Selection.TypeParagraph; % 回车,另起一段 >> xueqi = '( 2009 — 2010 学年 第一学期)'; >> Selection.Text = xueqi; % 在选定区域输入文字内容 >> Selection.Font.Size = 12; % 设置字号为12 >> Selection.Font.Bold = 0; % 字体不加粗 >> Selection.paragraphformat.Alignment = 'wdAlignParagraphCenter'; % 居中对齐 >> Selection.MoveDown; % 光标移到所选区域的最后 >> Selection.TypeParagraph; % 回车,另起一段 >> Selection.TypeParagraph; % 回车,另起一段 >> Selection.Font.Size=10.5; % 设置字号为10.5 2014-5-26 © 谢中华, 天津科技大学数学系.
的静态程序,而是随时可以用新组件取代旧组件而
返老还童的动态程序。
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
二、COM接口 1. IUnknown接口 接口查询
生存期控制
2. IDispatch接口 IDispatch接口接收一个函数名并执行它
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
一、创建Microsoft Word服务器 1. 创建Microsoft Word服务器
try % 若Word服务器已经打开,返回其句柄Word Word = actxGetRunningServer('Word.Application'); catch % 创建一个Microsoft Word服务器,返回句柄Word Word = actxserver('Word.Application'); end
论坛报告会
>> eventhandle = @(varargin)disp('祝大家身体健康!'); >> f = figure('position',[360,278,535,410]); >> cal = actxcontrol('mscal.calendar',[0,0,535,410],f,eventhandle);
论坛报告会
利用MATLAB生成Word和Excel文档
天 津 科 技 大 学 数 学 系 谢中华
E-mail: xiezhh@
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
我的新书
感谢赵社长和陈守平编辑!
感谢rocwoods(吴鹏)!
感谢math! 感谢各位版友的捧场!
% 返回Document的Content接口的句柄 >> Content = Document.Content; >> Content.Start = 0; % 设置文档内容的起始位置 >> title = '试 卷 分 析'; >> Content.Text = title; % 输入文字内容 >> Content.Font.Size = 16 ; % 设置字号为16
2014-5-26
©
谢中华, 天津科技大学数学系.
论坛报告会
五、查询COM对象的接口、方法和事件
表1 函数名 invoke interfaces methods methodsview events invoke、interfaces 、methods、methodsview 和 events 函数 功能说明 调用 COM 对象上的方法或接口, 也可用来显示方法列 表 显示 COM 服务器中由组件实现的所有定制接口列表 显示类方法相关信息列表, 可用来显示 COM 对象的所 有方法名 在单独的图形窗口中显示类方法相关信息,可用来显 示 COM 对象的所有方法的相关信息 显示 COM 对象能触发的所有事件名
2. 设置对象属性
% 设置Word服务器为可见状态 >> set(Word, 'Visible', 1); %或Word.Visible = 1;
©
2014-5-26
谢中华, 天津科技大学数学系.
论坛报告会
二、建立Word文本文档 1. 新建空白文档
% 调用Add方法建立一个空白文档,并返回其句柄Document. >> Document = Word.Documents.Add;
入一段文字。Content接口有很多属性和方法,读者可通过
Content.get和Content.methodsview命令查看。其中Start属 性用来获取或设定文字内容的起始位置,End属性用来获取 或设定文字内容的终止位置,Text属性用来写入文字内容, Font属性用于字体设置,Paragraphs属性用于段落设置。
七、查询和设置COM对象(或接口)的属性 get方法
inspect方法
set方法 addproperty方法 deleteproperty方法
2014-5-26
©
谢中华, 天津科技大学数学系.
八、调用COM对象和接口下的方法 S = h.invoke('methodname')
论坛报告会
S = h.invoke('methodname', arg1, arg2,…)
V = h.methodname
V = h.methodname(arg1, arg2,…)
【例3】创建一个日历控件,在一个循环里调用NextDay 方法,将时间后推1000天,并返回当时的时间
>> cal = actxcontrol('mscal.calendar',[10 10 540 400]); >> for i=1:1000 cal.NextDay; end >> cal.Value % 返回修改后的时间
【例2】通过MATLAB控制Windows Media Player 播放歌曲
% 创建Windows Media Player服务器,并返回对象句柄h >> h = actxserver('WMPlayer.OCX.7'); % 播放歌曲青花瓷 >> h.openPlayer('F:\我的音乐盒\青花瓷.mp3')
2014-5-26
©
谢中华, 天津科技大学数学系.