Matlab Excel开发(VBA)

时间:2024-03-04 17:19:10

新建一个空的Excel表格

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

filename = 'D:\new folder\myfile.xls';
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对象

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

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

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

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

  1. 读取指定的工作表
eSheet1 = excelFile.Sheets.Item('Sheet1');
  1. 将 excelFile1 中的 “Sheet2" 复制到 excelFile2 的第一个sheet的位置
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的名字
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
set(excelFile_sheet,'Name','New sheet name');
  1. 设置/取消设置Sheet的保护密码
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
excelFile = excel.Workbooks.Open('D:\new folder\myfile.xls');
excelFile_sheet = excelFile.Worksheets.Item('Sheet2');
excelFile_sheet.Delete;

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

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

  1. 将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 为元胞数组
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中所使用的行数/列数

第一种方式:

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;

第二种方式:

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 中。

第一种方式

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;

第二种方式

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” 为起点的位置
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. 设置字体
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. 设置自动列宽
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文件

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

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

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、添加单元格批注