设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2910|回复: 3

Matlab生成Word报告(代码)

  [复制链接]

36

主题

337

金钱

521

积分

初级用户

发表于 2019-11-7 15:24:31 | 显示全部楼层 |阅读模式

  1. function fun_word
  2. %利用MATLAB生成Word文档

  3. filespec_user = [pwd '\测试.doc'];

  4. %===启用word调用功能========================================================
  5. try   
  6.     Word = actxGetRunningServer('Word.Application');
  7. catch   
  8.     Word = actxserver('Word.Application');
  9. end
  10. Word.Visible = 1; % 使word为可见;或set(Word, 'Visible', 1);
  11. %===打开word文件,如果路径下没有则创建一个空白文档打开========================
  12. if exist(filespec_user,'file');
  13.     Document = Word.Documents.Open(filespec_user);   
  14. else
  15.     Document = Word.Documents.Add;     
  16.     Document.SaveAs2(filespec_user);
  17. end
  18. %===格式定义===============================================================
  19. Content = Document.Content;
  20. Selection = Word.Selection;
  21. Paragraphformat = Selection.ParagraphFormat;
  22. %===文档的页边距===========================================================
  23. Document.PageSetup.TopMargin    = 60;
  24. Document.PageSetup.BottomMargin = 45;
  25. Document.PageSetup.LeftMargin   = 45;
  26. Document.PageSetup.RightMargin  = 45;
  27. %==========================================================================

  28. %===文档组成部分============================================================
  29. % 文档的标题及格式
  30. headline            = '报告';
  31. Content.Start       = 0;    % 起始点为0,即表示每次写入覆盖之前资料
  32. Content.Text        = headline;
  33. Content.Font.Size   = 16;   % 字体大小
  34. Content.Font.Bold   = 1;    % 字体加粗
  35. Content.Paragraphs.Alignment = 'wdAlignParagraphCenter'; % 居中,wdAlignParagraphLeft/Center/Right

  36. % 文档的创建时间
  37. Selection.Start     = Content.end;  % 开始的地方在上一个的结尾
  38. Selection.TypeParagraph;            % 插入一个新的空段落
  39. % 插入时间
  40. currentdate         = datestr(now, 0);  % 获取当前时间
  41. Selection.Text      = currentdate;      % 当前时间作为输出
  42. Selection.Font.Size = 12;               % 字号
  43. Selection.Font.Bold = 0;                % 不加粗
  44. Selection.MoveDown;                     %将所选内容向下移动,并返回移动距离的单位数
  45. Paragraphformat.Alignment = 'wdAlignParagraphCenter'; % 居中

  46. % 插入回车
  47. Selection.TypeParagraph;% 插入一个新的空段落
  48. Selection.Font.Size = 10.5;% 新的空段落字号

  49. % 插入表格
  50. Selection.Start     = Content.end;
  51. Selection.TypeParagraph;
  52. Paragraphformat.Alignment = 'wdAlignParagraphLeft';
  53. Selection.MoveDown;

  54. Tables = Document.Tables.Add(Selection.Range,12,9);    % 插入一个12行9列的表格

  55. DTI = Document.Tables.Item(1); % 表格句柄

  56. DTI.Borders.OutsideLineStyle    = 'wdLineStyleSingle';  % 最外框,实线
  57. DTI.Borders.OutsideLineWidth    = 'wdLineWidth150pt';   % 线宽
  58. DTI.Borders.InsideLineStyle     = 'wdLineStyleSingle';  % 所有的内框线条
  59. DTI.Borders.InsideLineWidth     = 'wdLineWidth150pt';   % 线宽

  60. DTI.Rows.Alignment                          = 'wdAlignRowCenter'; %大表格居中
  61. DTI.Rows.Item(8).Borders.Item(1).LineStyle  = 'wdLineStyleNone'; % 第八行的上边线消失
  62. DTI.Rows.Item(8).Borders.Item(3).LineStyle  = 'wdLineStyleNone';% 第八行的下边线消失
  63. DTI.Rows.Item(11).Borders.Item(1).LineStyle = 'wdLineStyleNone';
  64. DTI.Rows.Item(11).Borders.Item(3).LineStyle = 'wdLineStyleNone';

  65. % 设置行高,列宽
  66. column_width = [53.7736,85.1434,53.7736,35.0094,...
  67.     35.0094,76.6981,55.1887,52.9245,54.9057];
  68. row_height = [28.5849,28.5849,28.5849,28.5849,25.4717,25.4717,...
  69.     32.8302,312.1698,17.8302,49.2453,14.1509,18.6792];

  70. for i = 1:9
  71.     DTI.Columns.Item(i).Width = column_width(i);
  72. end

  73. for i = 1:12
  74.     DTI.Rows.Item(i).Height = row_height(i);
  75. end

  76. % 设置垂直居中
  77. for i = 1:12        % 行
  78.     for j = 1:9     % 列
  79.         DTI.Cell(i,j).VerticalAlignment = 'wdCellAlignVerticalCenter';
  80.     end
  81. end

  82. % 合并单元格
  83. DTI.Cell(1, 4).Merge(DTI.Cell(1, 5)); % 第一行第四个到第五个合并
  84. DTI.Cell(2, 4).Merge(DTI.Cell(2, 5));
  85. DTI.Cell(3, 4).Merge(DTI.Cell(3, 5));
  86. DTI.Cell(4, 4).Merge(DTI.Cell(4, 5));
  87. DTI.Cell(5, 2).Merge(DTI.Cell(5, 5));
  88. DTI.Cell(5, 3).Merge(DTI.Cell(5, 6));
  89. DTI.Cell(6, 2).Merge(DTI.Cell(6, 5));
  90. DTI.Cell(6, 3).Merge(DTI.Cell(6, 6));
  91. DTI.Cell(5, 1).Merge(DTI.Cell(6, 1));
  92. DTI.Cell(7, 1).Merge(DTI.Cell(7, 9));
  93. DTI.Cell(8, 1).Merge(DTI.Cell(8, 9));
  94. DTI.Cell(9, 1).Merge(DTI.Cell(9, 3));
  95. DTI.Cell(9, 2).Merge(DTI.Cell(9, 3));
  96. DTI.Cell(9, 3).Merge(DTI.Cell(9, 4));
  97. DTI.Cell(9, 4).Merge(DTI.Cell(9, 5));
  98. DTI.Cell(10, 1).Merge(DTI.Cell(10, 9));% 第10行第1个到第9个合并
  99. DTI.Cell(11, 5).Merge(DTI.Cell(11, 9));
  100. DTI.Cell(12, 5).Merge(DTI.Cell(12, 9));
  101. DTI.Cell(11, 1).Merge(DTI.Cell(12, 4));


  102. % 表格之后的段落
  103. Selection.Start = Content.end;
  104. Selection.TypeParagraph;
  105. Selection.Text = '主管院长签字:            年    月    日';
  106. Paragraphformat.Alignment = 'wdAlignParagraphRight';
  107. Selection.MoveDown;

  108. % 定义表格中的内容
  109. DTI.Cell(1,1).Range.Text = '课程名称';
  110. DTI.Cell(1,3).Range.Text = '课程号';
  111. DTI.Cell(1,5).Range.Text = '任课教师学院';
  112. DTI.Cell(1,7).Range.Text = '任课教师';
  113. DTI.Cell(2,1).Range.Text = '授课班级';
  114. DTI.Cell(2,3).Range.Text = '考试日期';
  115. DTI.Cell(2,5).Range.Text = '应考人数';
  116. DTI.Cell(2,7).Range.Text = '实考人数';
  117. DTI.Cell(3,1).Range.Text = '出卷方式';
  118. DTI.Cell(3,3).Range.Text = '阅卷方式';
  119. DTI.Cell(3,5).Range.Text = '选用试卷A/B';
  120. DTI.Cell(3,7).Range.Text = '考试时间';
  121. DTI.Cell(4,1).Range.Text = '考试方式';
  122. DTI.Cell(4,3).Range.Text = '平均分';
  123. DTI.Cell(4,5).Range.Text = '不及格人数';
  124. DTI.Cell(4,7).Range.Text = '及格率';
  125. DTI.Cell(5,1).Range.Text = '成绩分布';
  126. DTI.Cell(5,2).Range.Text = '90分以上      人占        %';
  127. DTI.Cell(5,3).Range.Text = '80---89分        人占        %';
  128. DTI.Cell(6,2).Range.Text = '70--79分      人占        %';
  129. DTI.Cell(6,3).Range.Text = '60---69分        人占        %';
  130. DTI.Cell(7,1).Range.Text = ['试卷分析(含是否符合教学大纲、难度、知识覆'...
  131.     '盖面、班级分数分布分析、学生答题存在的共性问题与知识掌握情况、教学中'...
  132.     '存在的问题及改进措施等内容)'];
  133. DTI.Cell(7,1).Range.ParagraphFormat.Alignment = 'wdAlignParagraphLeft';
  134. DTI.Cell(9,2).Range.Text = '签字 :';
  135. DTI.Cell(9,4).Range.Text = '年    月    日';
  136. DTI.Cell(10,1).Range.Text = '教研室审阅意见:';
  137. DTI.Cell(10,1).Range.ParagraphFormat.Alignment = 'wdAlignParagraphLeft';
  138. DTI.Cell(10,1).VerticalAlignment = 'wdCellAlignVerticalTop';
  139. DTI.Cell(11,2).Range.Text = '教研室主任(签字):          年    月    日';
  140. DTI.Cell(11,2).Range.ParagraphFormat.Alignment = 'wdAlignParagraphLeft';
  141. DTI.Cell(8,1).Range.ParagraphFormat.Alignment = 'wdAlignParagraphLeft';
  142. DTI.Cell(8,1).VerticalAlignment = 'wdCellAlignVerticalTop';
  143. DTI.Cell(9,2).Borders.Item(2).LineStyle = 'wdLineStyleNone';
  144. DTI.Cell(9,2).Borders.Item(4).LineStyle = 'wdLineStyleNone';
  145. DTI.Cell(9,3).Borders.Item(4).LineStyle = 'wdLineStyleNone';
  146. DTI.Cell(11,1).Borders.Item(4).LineStyle = 'wdLineStyleNone';

  147. % 暂时没搞懂意思,貌似不用也不影响
  148. Shape = Document.Shapes;
  149. ShapeCount = Shape.Count;
  150. if ShapeCount ~= 0;
  151.     for i = 1:ShapeCount;
  152.         Shape.Item(1).Delete;
  153.     end
  154. end

  155. % 绘图并放置于表格单元(8,1)
  156. zft = figure('units','normalized','position',...
  157. [0.280469 0.553385 0.428906 0.251302],'visible','off'); % 定义句柄
  158. % 绘图
  159. set(gca,'position',[0.1 0.2 0.85 0.75]);
  160. data = normrnd(0,1,1000,1);
  161. hist(data);
  162. grid on;
  163. xlabel('考试成绩');
  164. ylabel('人数');

  165. hgexport(zft, '-clipboard'); % 将图片复制到剪切板
  166. DTI.Cell(8,1).Range.Paragraphs.Item(1).Range.PasteSpecial; % 粘贴操作
  167. Shape.Item(1).WrapFormat.Type = 3;
  168. Shape.Item(1).ZOrder('msoBringInFrontOfText');
  169. delete(zft);    % 删除句柄

  170. Document.ActiveWindow.ActivePane.View.Type = 'wdPrintView';
  171. Document.Save;  % 保存文档
  172. Word.Quit;      % 关闭文档
复制代码

7

主题

362

金钱

3127

积分

中级用户

发表于 2019-12-31 15:25:40 | 显示全部楼层
运行过吗?
ximenyan
回复

使用道具 举报

7

主题

362

金钱

3127

积分

中级用户

发表于 2019-12-31 15:26:18 | 显示全部楼层
运行中没有问题吧?
ximenyan
回复 支持 反对

使用道具 举报

1

主题

53

金钱

67

积分

新手用户

发表于 2020-7-20 14:18:14 | 显示全部楼层
大家都试了吗
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表