我是靠谱客的博主 娇气咖啡,这篇文章主要介绍matlab批量写word,现在分享给大家,希望可以做个参考。

原链接:https://www.cnblogs.com/airbird/articles/11455243.html

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
filespec_user = [pwd '测试.doc']; %===启用word调用功能======================================================== try Word = actxGetRunningServer('Word.Application'); catch Word = actxserver('Word.Application'); end Word.Visible = 1; % 使word为可见;或set(Word, 'Visible', 1); %===打开word文件,如果路径下没有则创建一个空白文档打开======================== if exist(filespec_user,'file'); Document = Word.Documents.Open(filespec_user); else Document = Word.Documents.Add; Document.SaveAs2(filespec_user); end %===格式定义=============================================================== Content = Document.Content; Selection = Word.Selection; Paragraphformat = Selection.ParagraphFormat; %===文档的页边距=========================================================== Document.PageSetup.TopMargin = 60; Document.PageSetup.BottomMargin = 45; Document.PageSetup.LeftMargin = 45; Document.PageSetup.RightMargin = 45; %========================================================================== %===文档组成部分============================================================ % 文档的标题及格式 headline = '报告'; Content.Start = 0; % 起始点为0,即表示每次写入覆盖之前资料 Content.Text = headline; Content.Font.Size = 16; % 字体大小 Content.Font.Bold = 1; % 字体加粗 Content.Paragraphs.Alignment = 'wdAlignParagraphCenter'; % 居中,wdAlignParagraphLeft/Center/Right % 文档的创建时间 Selection.Start = Content.end; % 开始的地方在上一个的结尾 Selection.TypeParagraph; % 插入一个新的空段落 % 插入时间 currentdate = datestr(now, 0); % 获取当前时间 Selection.Text = currentdate; % 当前时间作为输出 Selection.Font.Size = 12; % 字号 Selection.Font.Bold = 0; % 不加粗 Selection.MoveDown; %将所选内容向下移动,并返回移动距离的单位数 Paragraphformat.Alignment = 'wdAlignParagraphCenter'; % 居中 % 插入回车 Selection.TypeParagraph;% 插入一个新的空段落 Selection.Font.Size = 10.5;% 新的空段落字号 % 插入表格 Selection.Start = Content.end; Selection.TypeParagraph; Paragraphformat.Alignment = 'wdAlignParagraphLeft'; Selection.MoveDown; Tables = Document.Tables.Add(Selection.Range,12,9); % 插入一个129列的表格 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,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: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));%10行第1个到第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 % 绘图并放置于表格单元(8,1) zft = figure('units','normalized','position',... [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('考试成绩'); ylabel('人数'); hgexport(zft, '-clipboard'); % 将图片复制到剪切板 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; % 保存文档 Word.Quit; % 关闭文档

最后

以上就是娇气咖啡最近收集整理的关于matlab批量写word的全部内容,更多相关matlab批量写word内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部