【MATLAB/app设计】用程序写word,将appdesigner的坐标区图片复制到word中,自动生成表格,批量生成可读性报告

appdesigner设计,调用word命令,生成word报告。

此处主要通过代码来实现对图片和表格的复制。

同时写入图片和表格时,建议用Selection控制写入图片,用Content控制写入表格

[file, path] = uiputfile('*.docx');%命名文件
if isequal(file,0) || isequal(path,0)

    errordlg('未命名文件','错误');%错误提示
    return
else
    filespec_user = strcat(path,file);% 设Word文件名和路径
end
if exist(filespec_user,'file')
    delete(filespec_user);
end
% 判断Word是否已经打开,若已打开,就在打开的Word中进行操作,否则就打开Word
try
    % 若Word服务器已经打开,返回其句柄Word
    Word = actxGetRunningServer('Word.Application');
catch
    % 否则,创建一个Microsoft Word服务器,返回句柄Word
    Word = actxserver('Word.Application');
end
Word.Visible = 1; % 写入Word可视化,0则不可见
Document = Word.Documents.Add;
Document.SaveAs2(filespec_user);
Content = Document.Content;
Selection = Word.Selection;
%编辑标题
Content.Start =0;
Content.Text = "差值计算报告";
Content.Paragraphs.Alignment='wdAlignParagraphCenter';%
Content.Font.Name = '宋体' ;
Content.Font.Size=20;

Selection.Start = Content.End;%此处用段落控制图像,content控制出现问题,未解决
% 复制坐标区图像
Selection.TypeParagraph;
copygraphics(app.UIAxes4_2);
Selection.Paste;%粘贴
Selection.TypeParagraph;    % 回车,另起一段
Selection.Font.Size = 10;
Selection.Text = "图1. 电压波形";
Content.Paragraphs.Alignment='wdAlignParagraphCenter';%居中
Selection.MoveDown;
Selection.TypeParagraph;
copygraphics(app.UIAxes4_4);
Selection.Paste;
Selection.TypeParagraph;
Selection.Start = Selection.End;
Selection.Text = "图2. 有功功率波形";
Content.Paragraphs.Alignment='wdAlignParagraphCenter';%居中
Selection.MoveDown;
Selection.TypeParagraph
copygraphics(app.UIAxes4_5);
Selection.Paste;
Selection.TypeParagraph;
Selection.Start = Selection.End;
%% 在图后另起一段插入表格
Selection.Text = "表1. 误差分析表";
Document.PageSetup.Orientation=1;
Selection.MoveDown;
Selection.TypeParagraph;
Content.Start = Selection.End;
Content.Paragraphs.Alignment='wdAlignParagraphCenter';%居中
size_Row = max(size(app.UITable.RowName));%此处直接调用appdesigner中的表格内容
size_Col = max(size(app.UITable.ColumnName));
Table=Document.Tables.Add(Selection.Range,size_Row+1,size_Col+1);
Table.Borders.InsideLineStyle='wdLineStyleSingle';
Table.Borders.OutsideLineStyle='wdLineStyleSingle';

Table.Cell(1,1).Range.Text = "参数";
for i=1:1:size_Row
    Table.Cell(i+1,1).Range.Text = string(app.UITable.RowName(i));
end
for i=1:1:size_Col
    Table.Cell(1,i+1).Range.Text = string(app.UITable.ColumnName(i));
end
sizenum = size(app.UITable.Data);
for i = 1:1:sizenum(1)
    for j=1:1:sizenum(2)
        if isnan(app.UITable.Data(i,j))%剔除NaN值,否则无法写入word
            Table.Cell(i+1,j+1).Range.Text = "/";
        else
            Table.Cell(i+1,j+1).Range.Text = string(app.UITable.Data(i,j));
        end
    end
end

Content.Font.Name = '宋体' ;
Content.Font.Size=10;
Document.Save; % 保存文档
Document.Close; % 关闭文档

Word.Quit; % 退出word服务器
msgbox("word已经生成","提示");

参考链接:

https://blog.csdn.net/sisyphus_zhou/article/details/40422623?spm=1001.2014.3001.5506

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值