目錄
- 前言
- 單元格內(nèi)容操作
- 1.設(shè)置單元格的值
- 2.復(fù)制,粘貼,剪切
- 3. 將單元格內(nèi)容作為圖片復(fù)制到剪切板
- 4.向Excel添加MATLAB中的圖片
- 單元格屬性操作
- 1. 設(shè)置單元格內(nèi)字體樣式
- 2. 設(shè)置單元格顏色
- 3. 合并單元格
- 4.文本對(duì)齊設(shè)置
- 5.寬度和高度設(shè)置
- 總結(jié)
前言
接上文 matlab與Excel交互 非xlsread和xlswrite (1) ,此處默認(rèn):
Excel=actxserver(‘Excel.application');
Workbook 為已添加工作簿
單元格內(nèi)容操作
1.設(shè)置單元格的值
eSheet2.Range('A1:C3').Value = magic(3);
% 這個(gè)等于magic(4)也行,但是多出的部分會(huì)自動(dòng)忽略,
eSheet2.Range('A1').Value = 'Value'; % 設(shè)置某一個(gè)單元格的值
eSheet2.Columns.Item(1).Value = 123; % 設(shè)置某一列的值 行同理

2.復(fù)制,粘貼,剪切
% 清除單元格內(nèi)容
eSheet1.Range('A1').Clear;
% 復(fù)制單元格內(nèi)容
eSheet1.Range('A1').Copy;
% 剪切單元格內(nèi)容
eSheet1.Range('A1').Cut;
% 刪除單元格
eSheet1.Range('A1:C3').Delete
% 粘貼
eSheet1.Range('A1').PasteSpecial;
% 或者
% eSheet1.Paste;
3. 將單元格內(nèi)容作為圖片復(fù)制到剪切板
% 將單元格內(nèi)容作為圖片復(fù)制到剪切板
eSheet1.Range('A1:C3').CopyPicture
效果如下:

4.向Excel添加MATLAB中的圖片
%向Excel中添加圖片
a=0:0.1:5;
b=sin(a);
plot(a,b);
hgexport(gcf,'-clipboard');
eSheet1.Range('A1').PasteSpecial;
% 或者
% eSheet1.Paste;
效果如下:

單元格屬性操作
1. 設(shè)置單元格內(nèi)字體樣式
eSheet2.Range('A1').Font.FontProperty = PropertyValue
% 其中:常用的FontProperty有:Name,Size,Bold,Italic,Underline,Color
% 分別對(duì)應(yīng)字體,大小,加粗,斜體,下劃線,顏色
FontProperty具體有那些可以查看
%字體加粗
eSheet2.Columns.Item(1).Font.Bold = true % 第一列加粗
% eSheet2.Columns..Font.Bold = true % 全部加粗
eSheet2.Range('A1:C3').Font.Bold = true % 選中范圍加粗
eSheet2.Rows.Item(1).Font.Bold = true % 第1列加粗

eSheet2.Range('A1:C3').Font.Name = '華文行楷';
eSheet2.Range('A1:C3').Font.Size = 12;
eSheet2.Range('A1:C3').Font.Italic = true ; %斜體
2. 設(shè)置單元格顏色
eSheet1.Range('A1:B3').Interior.Color = 2^24;% 二進(jìn)制 前8位表示R,中8位表示G,后8位表示B

提示:顏色可以用十六進(jìn)制表示(但記得要在賦值的時(shí)候轉(zhuǎn)換成二進(jìn)制),這樣更容易提現(xiàn)RGB的值
3. 合并單元格
eSheet2.Range('A1:C3').Merge

4.文本對(duì)齊設(shè)置
Title.HorizontalAlignment=-4108; % 水平居中
Title.VerticalAlignment=-4108; % 垂直居中


5.寬度和高度設(shè)置
Sheet1.Columns.ColumnWidth=1; % 列寬
Sheet1.Columns.RowHeight=10; % 行距 單位為磅
總結(jié)
到此這篇關(guān)于利用matlab與Excel交互之單元格操作的文章就介紹到這了,更多相關(guān)matlab與Excel單元格操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!