找回密码
 注册
Simdroid-非首页
查看: 2359|回复: 38

用Matlab生成Word文档

[复制链接]
发表于 2008-11-9 23:07:07 | 显示全部楼层 |阅读模式 来自 天津
应bainhome版主要求,把我编的一段生成word文档的Matlab程序贴出来,生成的文档中含有表格,代码如下:

  1. filespec = 'C:\Documents and Settings\Administrator\桌面\xiezh.doc';
  2. try;
  3.     Word=actxGetRunningServer('Word.Application');
  4. catch;
  5.     Word = actxserver('Word.Application');
  6. end;

  7. set(Word, 'Visible', 1);
  8. documents = Word.Documents;
  9. if exist(filespec,'file')
  10.     document = invoke(documents,'Open',filespec);   
  11. else
  12.     document = invoke(documents, 'Add');
  13.     document.SaveAs(filespec);
  14. end

  15. content = document.Content;
  16. duplicate = content.Duplicate;
  17. inlineshapes = content.InlineShapes;
  18. selection = Word.Selection;
  19. paragraphformat = selection.ParagraphFormat;

  20. %页面设置
  21. document.PageSetup.TopMargin = 60;
  22. document.PageSetup.BottomMargin = 45;
  23. document.PageSetup.LeftMargin = 45;
  24. document.PageSetup.RightMargin = 45;

  25. set(content, 'Start',0);
  26. title='试  卷  分  析';
  27. set(content, 'Text',title);
  28. set(paragraphformat, 'Alignment','wdAlignParagraphCenter');
  29. rr=document.Range(0,10);
  30. rr.Font.Size=16;
  31. rr.Font.Bold=4;

  32. end_of_doc = get(content,'end');
  33. set(selection,'Start',end_of_doc);

  34. selection.TypeParagraph;
  35. set(selection, 'Text','(20     —20     学年 第     学期)');
  36. selection.Font.Size=12;
  37. selection.MoveDown;
  38. set(paragraphformat, 'Alignment','wdAlignParagraphCenter');

  39. selection.TypeParagraph;
  40. selection.TypeParagraph;
  41. selection.Font.Size=10.5;
  42. Tables=document.Tables.Add(selection.Range,12,9);


  43. %设置边框
  44. DTI=document.Tables.Item(1);
  45. DTI.Borders.OutsideLineStyle='wdLineStyleSingle';
  46. DTI.Borders.OutsideLineWidth='wdLineWidth150pt';
  47. DTI.Borders.InsideLineStyle='wdLineStyleSingle';
  48. DTI.Borders.InsideLineWidth='wdLineWidth150pt';
  49. DTI.Rows.Alignment='wdAlignRowCenter';
  50. DTI.Rows.Item(8).Borders.Item(1).LineStyle='wdLineStyleNone';
  51. DTI.Rows.Item(8).Borders.Item(3).LineStyle='wdLineStyleNone';
  52. DTI.Rows.Item(11).Borders.Item(1).LineStyle='wdLineStyleNone';
  53. DTI.Rows.Item(11).Borders.Item(3).LineStyle='wdLineStyleNone';

  54. column_width=[53.7736,80.9434,53.7736,29.0094,29.0094,76.6981,55.1887,52.9245,54.9057];
  55. 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];
  56. for i=1:9
  57.     DTI.Columns.Item(i).Width =column_width(i);
  58. end
  59. for i=1:12
  60.     DTI.Rows.Item(i).Height =row_height(i);
  61. end
  62. for i=1:12
  63.     for j=1:9
  64.         DTI.Cell(i,j).VerticalAlignment='wdCellAlignVerticalCenter';
  65.     end
  66. end

  67. DTI.Cell(1, 4).Merge(DTI.Cell(1, 5));
  68. DTI.Cell(2, 4).Merge(DTI.Cell(2, 5));
  69. DTI.Cell(3, 4).Merge(DTI.Cell(3, 5));
  70. DTI.Cell(4, 4).Merge(DTI.Cell(4, 5));
  71. DTI.Cell(5, 2).Merge(DTI.Cell(5, 5));
  72. DTI.Cell(5, 3).Merge(DTI.Cell(5, 6));
  73. DTI.Cell(6, 2).Merge(DTI.Cell(6, 5));
  74. DTI.Cell(6, 3).Merge(DTI.Cell(6, 6));
  75. DTI.Cell(5, 1).Merge(DTI.Cell(6, 1));
  76. DTI.Cell(7, 1).Merge(DTI.Cell(7, 9));
  77. DTI.Cell(8, 1).Merge(DTI.Cell(8, 9));
  78. DTI.Cell(9, 1).Merge(DTI.Cell(9, 3));
  79. DTI.Cell(9, 2).Merge(DTI.Cell(9, 3));
  80. DTI.Cell(9, 3).Merge(DTI.Cell(9, 4));
  81. DTI.Cell(9, 4).Merge(DTI.Cell(9, 5));
  82. DTI.Cell(10, 1).Merge(DTI.Cell(10, 9));
  83. DTI.Cell(11, 5).Merge(DTI.Cell(11, 9));
  84. DTI.Cell(12, 5).Merge(DTI.Cell(12, 9));
  85. DTI.Cell(11, 1).Merge(DTI.Cell(12, 4));

  86. end_of_doc = get(content,'end');
  87. set(selection,'Start',end_of_doc);
  88. selection.TypeParagraph;
  89. set(selection, 'Text','主管院长签字:            年    月    日');
  90. set(paragraphformat, 'Alignment','wdAlignParagraphRight');

  91. DTI.Cell(1,1).Range.Text = '课程名称';
  92. DTI.Cell(1,3).Range.Text = '课程号';
  93. DTI.Cell(1,5).Range.Text = '任课教师学院';
  94. DTI.Cell(1,7).Range.Text = '任课教师';
  95. DTI.Cell(2,1).Range.Text = '授课班级';
  96. DTI.Cell(2,3).Range.Text = '考试日期';
  97. DTI.Cell(2,5).Range.Text = '应考人数';
  98. DTI.Cell(2,7).Range.Text = '实考人数';
  99. DTI.Cell(3,1).Range.Text = '出卷方式';
  100. DTI.Cell(3,3).Range.Text = '阅卷方式';
  101. DTI.Cell(3,5).Range.Text = '选用试卷A/B';
  102. DTI.Cell(3,7).Range.Text = '考试时间';
  103. DTI.Cell(4,1).Range.Text = '考试方式';
  104. DTI.Cell(4,3).Range.Text = '平均分';
  105. DTI.Cell(4,5).Range.Text = '不及格人数';
  106. DTI.Cell(4,7).Range.Text = '及格率';
  107. DTI.Cell(5,1).Range.Text = '成绩分布';
  108. DTI.Cell(5,2).Range.Text = '90分以上      人占        %';
  109. DTI.Cell(5,3).Range.Text = '80---89分        人占        %';
  110. DTI.Cell(6,2).Range.Text = '70--79分      人占        %';
  111. DTI.Cell(6,3).Range.Text = '60---69分        人占        %';
  112. DTI.Cell(7,1).Range.Text = '试卷分析(含是否符合教学大纲、难度、知识覆盖面、班级分数分布分析、学生答题存在的共性问题与知识掌握情况、教学中存在的问题及改进措施等内容)';
  113. DTI.Cell(7,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';
  114. DTI.Cell(9,2).Range.Text = '签字 :';
  115. DTI.Cell(9,4).Range.Text = '年    月    日';
  116. DTI.Cell(10,1).Range.Text = '教研室审阅意见:';
  117. DTI.Cell(10,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';
  118. DTI.Cell(10,1).VerticalAlignment='wdCellAlignVerticalTop';
  119. DTI.Cell(11,2).Range.Text = '教研室主任(签字):          年    月    日';
  120. DTI.Cell(11,2).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';
  121. DTI.Cell(8,1).Range.ParagraphFormat.Alignment='wdAlignParagraphLeft';
  122. DTI.Cell(8,1).VerticalAlignment='wdCellAlignVerticalTop';
  123. DTI.Cell(9,2).Borders.Item(2).LineStyle='wdLineStyleNone';
  124. DTI.Cell(9,2).Borders.Item(4).LineStyle='wdLineStyleNone';
  125. DTI.Cell(9,3).Borders.Item(4).LineStyle='wdLineStyleNone';
  126. DTI.Cell(11,1).Borders.Item(4).LineStyle='wdLineStyleNone';
复制代码


生成的文档见附件

[ 本帖最后由 bainhome 于 2008-11-10 00:47 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

发表于 2008-11-10 00:16:33 | 显示全部楼层 来自 广西南宁
Simdroid开发平台
我试了,果然能得到一张word的“      ”。

MATLAB的功能真强大!
博大精深!要学的东西太多了!!
回复 不支持

使用道具 举报

发表于 2008-11-10 00:41:52 | 显示全部楼层 来自 新疆乌鲁木齐
把两个相关的帖子链接放在一起,便于查看:
链接1:
共享matlab编辑word代码
链接2:
帮忙解决一下matlab与word接口的问题

[ 本帖最后由 bainhome 于 2008-11-10 00:45 编辑 ]
回复 不支持

使用道具 举报

发表于 2012-1-10 17:18:52 | 显示全部楼层 来自 浙江杭州
楼主好强大!见识了啊!

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2011-12-14 09:50:23 | 显示全部楼层 来自 大连理工大学
本帖最后由 龙啸 于 2011-12-14 09:50 编辑

谢老师,您好,看了你的程序,非常感谢!现在我在编写一个matlab数据写入word文档的程序。word文档中有n个表格(n数值未知),现在要把数据写入到最后一个表格,怎么实现,查看word中表格的个数??谢谢
即:53.DTI=document.Tables.Item(1);  中的1怎么替代???不胜感激
回复 不支持

使用道具 举报

发表于 2011-12-15 15:02:09 | 显示全部楼层 来自 湖南长沙
楼主辛苦了

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2011-12-25 22:14:54 | 显示全部楼层 来自 陕西西安
太强了!

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2012-1-6 15:49:34 | 显示全部楼层 来自 黑龙江哈尔滨
:D太强大了  楼主好厉害

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2012-1-10 12:19:43 | 显示全部楼层 来自 河北保定
哇  ,,,,厉害强大

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2012-1-11 22:35:04 | 显示全部楼层 来自 四川成都
我的matlab2011a,office2010,运行这个程序,14行出错了,好像是 找不到这个函数,
回复 不支持

使用道具 举报

发表于 2012-1-26 10:18:57 | 显示全部楼层 来自 江苏徐州
太厉害了,功能强大

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2012-2-13 15:49:55 | 显示全部楼层 来自 上海
顶一个。谢谢!
回复 不支持

使用道具 举报

发表于 2012-2-15 22:08:25 | 显示全部楼层 来自 上海
这么有意思啊,我也试试去啊
回复 不支持

使用道具 举报

发表于 2012-2-18 16:09:08 | 显示全部楼层 来自 广西河池
楼主强大!
回复 不支持

使用道具 举报

发表于 2012-2-23 15:24:07 | 显示全部楼层 来自 湖北武汉
:hug:楼主太好了,正在找这个,多谢楼主啦!

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2012-2-26 10:47:08 | 显示全部楼层 来自 河南郑州
只是不明白,这个有什么用处啊。
只要能把数据保存在一个文件里即可,直接用word来绘制表格,更加方便。

点评

当你需要批量生成word文档的时候就知道用处了。  发表于 2012-5-15 13:41
回复 不支持

使用道具 举报

发表于 2012-5-13 21:04:17 | 显示全部楼层 来自 湖北武汉
厉害,楼主辛苦了!
回复 不支持

使用道具 举报

发表于 2012-5-16 17:11:05 | 显示全部楼层 来自 辽宁
好东西,学习了!
回复 不支持

使用道具 举报

发表于 2012-11-15 18:28:52 | 显示全部楼层 来自 浙江杭州
楼主太牛逼了
回复 不支持

使用道具 举报

发表于 2012-12-20 09:21:03 | 显示全部楼层 来自 安徽合肥
正在学习matlab,有些看不明白,先收藏了。谢谢谢老师分享。
回复 不支持

使用道具 举报

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

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-4-20 00:06 , Processed in 0.079893 second(s), 25 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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