Matlab Excel开发(VBA)

新建一个空的Excel表格

输入filename是带有Excel文件名的绝对路径,如:

matlab 复制代码
filename = 'D:\new folder\myfile.xls';
matlab 复制代码
function newExcelFile(filename)
% 激活 actxserver
try 
    excel = actxGetRunningServer('Excel.Application'); % 检查当前是否开启了excel服务器
catch
    excel = actxserver('Excel.Application');    % 未检测到则手动开启excel服务器
end
eWorkbook = excel.Workbooks.Add;
excel.Visible = 0;
eSheets = excel.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate
SaveAs(eWorkbook,filename)
eWorkbook.Saved = 1;
Close(eWorkbook)
Quit(excel)
delete(excel)
end

对已有的Excel文件操作

1. 创建Excel对象

matlab 复制代码
try
	excel = actxGetRunningServer('Excel.Application'); % 检查当前是否开启了excel服务器
catch
	excel = actxserver('Excel.Application');    % 未检测到则手动开启excel服务器
end

2. Excel文件(工作簿)的操作 ------ Workbooks 接口

  1. 打开包含数据的 Excel 文件
matlab 复制代码
excelFile = excel.Workbooks.Open([docroot '/techdoc/matlab_external/examples/input_resp_data.xls']);

3. Excel sheet(工作表)的操作 ------ Worksheets接口

  1. 读取指定的工作表
matlab 复制代码
eSheet1 = excelFile.Sheets.Item('Sheet1');
  1. 将 excelFile1 中的 "Sheet2" 复制到 excelFile2 的第一个sheet的位置
matlab 复制代码
excelFile1 = excel.Workbooks.Open('D:\new folder\myfile1.xls');
excelFile2 = excel.Workbooks.Open('D:\new folder\myfile2.xls');
excelFile1_sheet2 = excelFile1.Worksheets.Item('Sheet2');
excelFile1_sheet2.Copy(excelFile2.Worksheets.Item(1));
  1. 设置Sheet的名字
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
set(excelFile_sheet,'Name','New sheet name');
  1. 设置/取消设置Sheet的保护密码
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Protect('sheetPassword');
excelFile_sheet.Unprotect('sheetPassword');
  1. 删除Sheet
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Delete;

4. Excel表格内容(Range对象)的操作

Range 对象存储了对指定工作表内某个数据范围的引用。

  1. 将Matlab数据置入工作表
matlab 复制代码
A = [1 2; 3 4];
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
eRange = get(excelFile_sheet,'Range','A1:B2');
eRange.Value = A;
  1. 将数据读回 MATLAB,其中的数组 B 为元胞数组
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
eRange = get(excelFile_sheet,'Range','A1:B2');
B = eRange.Value;
  1. 获取Sheet中所使用的行数/列数

第一种方式:

matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
rowsNum = excelFile_sheet.UsedRange.Rows.Count;
colsNum = excelFile_sheet.UsedRange.Columns.Count;

第二种方式:

matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
robj = excelFile_sheet.Columns.End(4);       % Find the end of the column
rowsNum = robj.row;                    % And determine what row it is
cobj = excelFile_sheet.Rows.End(4);
colsNum = cobj.column;
  1. 访问从列 A 中的第一个单元格到列 G 中的最后一个单元格的所有数据,并将这些数据存放在 MATLAB 元胞数组 excelData 中。

第一种方式

matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
rowsNum = excelFile_sheet.UsedRange.Rows.Count;
excelData = excelFile_sheet.Range(['A1:G' num2str(rowsNum)]).Value;

第二种方式

matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
robj = excelFile_sheet.Columns.End(4);       % Find the end of the column
rowsNum = robj.row;                    % And determine what row it is
data_range = ['A1:G' num2str(rowsNum)]; % Read to the last row
rngObj = excelFile_sheet.Range(data_range);
excelData = rngObj.Value;
  1. 数据剪切:把 "B1:D2" 范围内的数据剪切到 以 "H1" 为起点的位置
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Range('B1:D2').Cut(excelFile_sheet.Range('H1'));
  1. 设置字体
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Range('A1:H1').Font.Name = 'Times New Roman';
excelFile_sheet.Range('A1:H1').Font.Bold = true; % "A1:H1" 粗体
excelFile_sheet.Range('A1:H1').Font.Size = 14; 
  1. 设置自动列宽
matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Range('A1:H5')..Columns.AutoFit; % "A1:H5" 设置自动列宽

5. 保存Excel文件,关闭Excel文件

matlab 复制代码
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
% 修改excel内容之后
excelFile.Save;
Close(excelFile);

6. 退出 Excel 程序并删除服务器对象

matlab 复制代码
Quit(excel)
delete(excel)

参考:

  1. https://ww2.mathworks.cn/help/matlab/matlab_external/using-a-matlab-application-as-an-automation-client.html
  2. https://ww2.mathworks.cn/help/matlab/matlab_external/example-reading-excel-spreadsheet-data.html
  3. https://blog.csdn.net/qq_43157190/article/details/99751310
  4. 微软 VBA Range.Range 属性 (Excel)
  5. matlab m脚本操作excel表格
  6. Excel_VBA 设置单元格的字体属性(字体、字号、加粗、斜体、颜色等)
  7. 微软 VBA Range.AutoFit 方法 (Excel)
  8. 【Excel VBA】自动调整列宽和行高
  9. Copy an Excel worksheet from one workbook to another with Matlab
  10. Matlab操作Excel复制Sheet、添加单元格批注
相关推荐
吃好睡好便好14 小时前
查找函数的使用
学习·算法·matlab·生活·查找函数
chhttty19 小时前
Design Verifier检查:整型数溢出检查与修复
matlab·simulink
tryCbest1 天前
Excel常用公式(WPS vs Office)
excel·wps
吃好睡好便好1 天前
判断函数的使用
学习·算法·matlab·生活·判断函数
hu9245195591 天前
Matlab保护源代码—P文件和exe文件详细解析
开发语言·matlab
史上最甜的躯1 天前
控制系统——基础、导论;以直流电机控制系统为例详解微分方程模型、拉普拉斯变换、传递函数模型的理论、推导及仿真
matlab·simulink·传递函数·控制系统·拉普拉斯·电机模型
taller_20001 天前
【006】何时用 Python?何时用 Excel 公式?一文讲清
python·excel·公式·pie·py
2601_949950631 天前
Word、PDF、Excel题目怎么快速整理?
pdf·word·excel
Eiceblue2 天前
React 项目实战:用 JavaScript 合并多个本地 Excel 文件
前端·javascript·react.js·excel
lingchen19062 天前
用MATLAB r2010b和r2016b求解微分方程组
开发语言·算法·matlab