xiezhh 发表于 2008-11-9 23:07:07

用Matlab生成Word文档

应bainhome版主要求,把我编的一段生成word文档的Matlab程序贴出来,生成的文档中含有表格,代码如下:

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

set(Word, 'Visible', 1);
documents = Word.Documents;
if exist(filespec,'file')
    document = invoke(documents,'Open',filespec);   
else
    document = invoke(documents, 'Add');
    document.SaveAs(filespec);
end

content = document.Content;
duplicate = content.Duplicate;
inlineshapes = content.InlineShapes;
selection = Word.Selection;
paragraphformat = selection.ParagraphFormat;

%页面设置
document.PageSetup.TopMargin = 60;
document.PageSetup.BottomMargin = 45;
document.PageSetup.LeftMargin = 45;
document.PageSetup.RightMargin = 45;

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

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

selection.TypeParagraph;
set(selection, 'Text','(20   —20   学年 第   学期)');
selection.Font.Size=12;
selection.MoveDown;
set(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.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=;
row_height=;
for i=1:9
    DTI.Columns.Item(i).Width =column_width(i);
end
for i=1:12
    DTI.Rows.Item(i).Height =row_height(i);
end
for i=1:12
    for j=1:9
      DTI.Cell(i,j).VerticalAlignment='wdCellAlignVerticalCenter';
    end
end

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));

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

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';

生成的文档见附件

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

小兰花 发表于 2008-11-10 00:16:33

我试了,果然能得到一张word的“试卷分析”。

MATLAB的功能真强大!
博大精深!要学的东西太多了!!

bainhome 发表于 2008-11-10 00:41:52

把两个相关的帖子链接放在一起,便于查看:
链接1:
共享matlab编辑word代码
链接2:
帮忙解决一下matlab与word接口的问题

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

abaodyb 发表于 2012-1-10 17:18:52

楼主好强大!见识了啊!

龙啸 发表于 2011-12-14 09:50:23

本帖最后由 龙啸 于 2011-12-14 09:50 编辑

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

Augest 发表于 2011-12-15 15:02:09

楼主辛苦了

zhouyanqing1981 发表于 2011-12-25 22:14:54

太强了!

20052959 发表于 2012-1-6 15:49:34

:D太强大了楼主好厉害

碧海潮生 发表于 2012-1-10 12:19:43

哇,,,,厉害强大

azdiamondback 发表于 2012-1-11 22:35:04

我的matlab2011a,office2010,运行这个程序,14行出错了,好像是 找不到这个函数,

hao2927430 发表于 2012-1-26 10:18:57

太厉害了,功能强大

cwit 发表于 2012-2-13 15:49:55

顶一个。谢谢!

sunset 发表于 2012-2-15 22:08:25

这么有意思啊,我也试试去啊

jiwenyu 发表于 2012-2-18 16:09:08

楼主强大!

紫月2008 发表于 2012-2-23 15:24:07

:hug:楼主太好了,正在找这个,多谢楼主啦!

hhwuai007 发表于 2012-2-26 10:47:08

只是不明白,这个有什么用处啊。
只要能把数据保存在一个文件里即可,直接用word来绘制表格,更加方便。

zzz420325 发表于 2012-5-13 21:04:17

厉害,楼主辛苦了!

piaoping2011 发表于 2012-5-16 17:11:05

好东西,学习了!

fanxiangn 发表于 2012-11-15 18:28:52

楼主太牛逼了

LuckyAngel 发表于 2012-12-20 09:21:03

正在学习matlab,有些看不明白,先收藏了。谢谢谢老师分享。
页: [1] 2
查看完整版本: 用Matlab生成Word文档