from
MATLAB read/write/control Excel
by George Thiers
What XLSREAD and XLSWRITE do under the hood
|
| XLSREADexample.m |
%%% What XLSREAD is doing under the hood, plus more
%% Parameters (if you make this a function, these should be inputs)
file = [pwd '\XLSREADtest.xls']; %If it's in the current directory
sheet = 2;
range = 'A2:A4';
%% Open the Excel application
h = actxserver('Excel.Application');
% Show the Excel window
h.Visible = 1;
% What attributes and operations are available for the handle "h"? These
% are attributes and operations of the Excel object ... and are documented
% in the Excel Visual Basic Reference:
% (In Microsoft Office 2003 Excel)
% Help -> Microsoft Excel Help -> Table of Contents -> Microsoft Excel Visual Basic Reference
%% Open the example "test.xls" (FULL PATH NEEDED)
workbook = h.Workbooks.Open( file );
%% Within a workbook, there are worksheets
Sheets = h.ActiveWorkBook.Sheets;
% Start with worksheet 1
Sheets.Item(1).Activate;
%% Get all raw data
% Get the entire used range of the worksheet in a cell array
AllRawData = h.ActiveSheet.UsedRange.Value
%% Get some raw data
% Get a subset of the data in a cell array
h.Range('A2:A4').Select;
SubsetRawData = h.get('Selection').Value
%% Get the background color of Cell A3
A3 = h.Range('A3')
A3.Select;
colorA3 = A3.Interior.ColorIndex
% Make a modification:
% A3.Interior.ColorIndex = colorA3 + 10;
%% Get the background color of Cell B3
B3 = h.Range('B3')
B3.Select;
colorB3 = B3.Interior.ColorIndex
% Make a modification
% B3.Interior.ColorIndex = colorB3 - 10;
%% Close
% Close workbook, don't save changes
% (this will fail if the file is "locked")
workbook.Close(false);
%% Close COM connection
% Terminate the server session to which the handle is atttached
h.Quit;
% Release all interfaces derived from the server
delete(h)
%% Clean up handles
clear ans A3 B3 Sheets workbook h AllRawData SubsetRawData colorA3 colorB3
|
|
Contact us at files@mathworks.com