winggundam
Would you like to react to this message? Create an account in a few clicks or log in to continue.

用matlab生成帶有圖片的Excel和Word文檔

向下

用matlab生成帶有圖片的Excel和Word文檔 Empty 用matlab生成帶有圖片的Excel和Word文檔

發表 由 lung 周一 11月 28, 2011 10:39 am

lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

用matlab生成帶有圖片的Excel和Word文檔 Empty 回復: 用matlab生成帶有圖片的Excel和Word文檔

發表 由 lung 周一 11月 28, 2011 10:39 am

1. 用matlab生成帶有圖片的Excel文檔

function ceshi_Excel
%設定測試Excel文件名和路徑
filespec_user=[pwd '\測試.xls'];
%判斷Excel是否已經打開,若已打開,就在打開的Excel中進行操作,
%否則就打開Excel
try
Excel=actxGetRunningServer('Excel.Application');
catch
Excel = actxserver('Excel.Application');
end;
%設置Excel屬性為可見
set(Excel, 'Visible', 1);
%返回Excel工作簿句柄
Workbooks = Excel.Workbooks;
%若測試文件存在,打開該測試文件,否則,新建一個工作簿,並保存,文件名為測試.Excel
if exist(filespec_user,'file');
Workbook = invoke(Workbooks,'Open',filespec_user);
else
Workbook = invoke(Workbooks, 'Add');
Workbook.SaveAs(filespec_user);
end
%返回工作表句柄
Sheets = Excel.ActiveWorkBook.Sheets;
%返回第一個表格句柄
sheet1 = get(Sheets, 'Item', 1);
%激活第一個表格
invoke(sheet1, 'Activate');
%如果當前工作表中有圖形存在,通過循環將圖形全部刪除
Shapes=Excel.ActiveSheet.Shapes;
if Shapes.Count~=0;
for i=1:Shapes.Count;
Shapes.Item(1).Delete;
end;
end;

%隨機產生標準正態分布隨機數,畫直方圖,並設置圖形屬性
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');
%將圖形粘貼到當前表格的A5:B5欄裏
Excel.ActiveSheet.Range('A5:B5').Select;
Excel.ActiveSheet.Paste;
%刪除圖形句柄
delete(zft);


2. 用matlab生成帶有圖片的Word文檔

function ceshi_Word
%設定測試Word文件名和路徑
filespec_user=[pwd '\測試.doc'];
%判斷Word是否已經打開,若已打開,就在打開的Word中進行操作,
%否則就打開Word
try
Word = actxGetRunningServer('Word.Application');
catch
Word = actxserver('Word.Application');
end;
%設置Word屬性為可見
set(Word, 'Visible', 1);
%返回Word文件句柄
documents = Word.Documents;
%若測試文件存在,打開該測試文件,否則,新建一個文件,並保存,文件名為測試.doc
if exist(filespec_user,'file');
document = invoke(documents,'Open',filespec_user);
else
document = invoke(documents, 'Add');
document.SaveAs(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;
%設定內容起始位置和標題
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;
%如果當前工作文檔中有圖形存在,通過循環將圖形全部刪除
shape=document.Shapes;
shape_count=shape.Count;
if shape_count~=0;
for i=1:shape_count;
shape.Item(1).Delete;
end;
end;

%隨機產生標準正態分布隨機數,畫直方圖,並設置圖形屬性
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');
%將圖形粘貼到當前文檔裏,並設置圖形屬性為浮於文字上方
selection.Range.PasteSpecial;
shape.Item(1).WrapFormat.Type=3;
shape.Item(1).ZOrder('msoBringInFrontOfText');
%刪除圖形句柄
delete(zft);
lung
lung
Admin

文章數 : 26067
注冊日期 : 2009-07-12

回頂端 向下

回頂端


 
這個論壇的權限:
無法 在這個版面回復文章