1. 用matlab生成帶有圖片的Excel文檔 function ceshi_Excel %設(shè)定測試Excel文件名和路徑 filespec_user=[pwd '\測試.xls']; %判斷Excel是否已經(jīng)打開,,若已打開,,就在打開的Excel中進行操作,, %否則就打開Excel try Excel=actxGetRunningServer('Excel.Application'); catch Excel = actxserver('Excel.Application'); end; %設(shè)置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'); %如果當前工作表中有圖形存在,,通過循環(huán)將圖形全部刪除 Shapes=Excel.ActiveSheet.Shapes; if Shapes.Count~=0; for i=1:Shapes.Count; Shapes.Item(1).Delete; end; end; %隨機產(chǎn)生標準正態(tài)分布隨機數(shù),,畫直方圖,並設(shè)置圖形屬性 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('人數(shù)'); %將圖形復(fù)制到粘貼板 hgexport(zft, '-clipboard'); %將圖形粘貼到當前表格的A5:B5欄裏 Excel.ActiveSheet.Range('A5:B5').Select; Excel.ActiveSheet.Paste; %刪除圖形句柄 delete(zft); 2. 用matlab生成帶有圖片的Word文檔 function ceshi_Word %設(shè)定測試Word文件名和路徑 filespec_user=[pwd '\測試.doc']; %判斷Word是否已經(jīng)打開,,若已打開,,就在打開的Word中進行操作, %否則就打開Word try Word = actxGetRunningServer('Word.Application'); catch Word = actxserver('Word.Application'); end; %設(shè)置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; %頁面設(shè)置 document.PageSetup.TopMargin = 60; document.PageSetup.BottomMargin = 45; document.PageSetup.LeftMargin = 45; document.PageSetup.RightMargin = 45; %設(shè)定內(nèi)容起始位置和標題 set(content, 'Start',0); title='測 試 文 件'; set(content, 'Text',title); set(paragraphformat, 'Alignment','wdAlignParagraphCenter'); %設(shè)定標題字體格式 rr=document.Range(0,10); rr.Font.Size=16; rr.Font.Bold=4; %設(shè)定下面內(nèi)容的起始位置 end_of_doc = get(content,'end'); set(selection,'Start',end_of_doc); %另起一段 selection.TypeParagraph; %如果當前工作文檔中有圖形存在,通過循環(huán)將圖形全部刪除 shape=document.Shapes; shape_count=shape.Count; if shape_count~=0; for i=1:shape_count; shape.Item(1).Delete; end; end; %隨機產(chǎn)生標準正態(tài)分布隨機數(shù),,畫直方圖,,並設(shè)置圖形屬性 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('人數(shù)'); %將圖形復(fù)制到粘貼板 hgexport(zft, '-clipboard'); %將圖形粘貼到當前文檔裏,並設(shè)置圖形屬性為浮於文字上方 selection.Range.PasteSpecial; shape.Item(1).WrapFormat.Type=3; shape.Item(1).ZOrder('msoBringInFrontOfText'); %刪除圖形句柄 delete(zft); |
|