声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 8778|回复: 31

[综合讨论] 用Matlab生成Word文档

[复制链接]
发表于 2008-11-9 21:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
[ 本帖最后由 牛小贱 于 2014-3-30 16:52 编辑 ]\n\n用Matlab编了一段程序,可以生成Word文档,文档中含有表格,代码如下:
[code]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=[53.7736,80.9434,53.7736,29.0094,29.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: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';[/code]生成的文档见附件

xiezh.doc

30.5 KB, 下载次数: 156

点评

赞成: 4.0
赞成: 4
  发表于 2014-3-30 16:49

评分

4

查看全部评分

回复
分享到:

使用道具 举报

发表于 2008-11-9 22:12 | 显示全部楼层

回复 楼主 xiezhh 的帖子

Excellent job!
发表于 2008-11-9 22:18 | 显示全部楼层
cool,非常实用的资料
发表于 2008-11-9 22:45 | 显示全部楼层

回复 楼主 xiezhh 的帖子

佩服佩服!
没有做不到,只有想不到!
发表于 2008-11-28 22:24 | 显示全部楼层

回复 楼主 xiezhh 的帖子

辛苦辛苦!!!
发表于 2009-4-25 11:22 | 显示全部楼层
这帖之前就看过, 不过未曾细看, 早上设断点并边看help, 学习了下! 的确满佩服楼主的功力! 自叹不如!
可能工程玩久了, 总喜欢实际些! 不知会不会太白目, 但实在想不透! 还是请教吧
个人怎觉得太复杂些且实用性好像也不高! 搅了半天只为了一张表单, 直接在Word处理不就可以吗?
个人不才, 一定是忽略些东西, 知道的说说嘛!
发表于 2009-4-25 13:05 | 显示全部楼层

回复 6楼 ChaChing 的帖子

我想对于单个这样的问题进行这样的处理确实不是很方便,但是如果需要很多类似的重复工作就比较有意义吧。
比如有100个班的成绩需要统计(就是对每个班的成绩做统计和运算,存成100个结果文件)的话,楼主的方法还是比较有借鉴意义的。
在工程中,这种方法可以用来将计算的过程和结果生成计算书——这个应该是很有意义的。
个人的一点儿浅见,欢迎大家讨论。

评分

1

查看全部评分

发表于 2009-4-25 13:14 | 显示全部楼层

回复 7楼 sogooda 的帖子

没错! 重复工作就比较有意义!
 楼主| 发表于 2009-4-25 21:41 | 显示全部楼层
做这个的动机是因为我在做图像处理的时候,总是时常计算很多数据,我不想每次都把结果手动整理成表格,就在程序中加了生成word和Excel文档的功能。后来看到很多同事做试卷分析时比较痛苦,虽然很简单,可毕竟不是每一个人都会统计和Excel,就编了上面的程序,甚至还做了个生成试卷分析的界面,内置了3个模板,用来生成word和Excel版的试卷分析(该程序发在matlab中文论坛)。几个同事用过后反映还不错,不过就是生成的文档有点雷同了,并且让神圣的工作流于形式,真是罪过罪过!不过若能给别人一点启发的话,我心里就多了一丝安慰了。

评分

1

查看全部评分

发表于 2009-4-26 21:34 | 显示全部楼层
怎么不能实现啊,我下了啊。
发表于 2009-4-26 22:27 | 显示全部楼层

回复 10楼 nkdtxf 的帖子

出错提示?
发表于 2009-11-7 20:13 | 显示全部楼层

请教高手:用Matlab生成含有表格的word文档,并在表格中分别填入信息跟照片

能在Word中自动生成表格,太好了。请问高手,能在生成的表格中显示照片吗。我的意思是说自动生成一个表格,前面面的表格输出人的名字,后面的表格能显示(或者贴上)对应的照片吗,照片是JPg格式的或BMP格式的。请指点。

请教高手:用Matlab生成含有表格的word文档,并在表格中分别填入信息跟照片
想生成一个5行2列的WORD表格,表格的第一列填人名,第二列显示对应的照片,照片的格式是JPG或者BMP格式的。
能做吗?

[ 本帖最后由 ChaChing 于 2009-11-7 21:02 编辑 ]
发表于 2009-11-7 21:10 | 显示全部楼层

回复 12楼 cccccccccsss0 的帖子

看看这个, 不过个人不熟, 楼主才是高手!
http://forum.vibunion.com/forum/ ... 5703&highlight=
发表于 2009-11-10 15:38 | 显示全部楼层
为啥我执行了上述程序只生成了个空白word文档啊?请楼主赐教。我是菜鸟!
发表于 2009-11-11 08:04 | 显示全部楼层
谢谢,真是高人。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-16 02:01 , Processed in 0.066856 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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