Code covered by the BSD License  

Highlights from
Export2Excel

image thumbnail
from Export2Excel by Shameemraj Nadaf
A function which exports a given Matlab array / Cell array to an Excel Spreadsheet.

Export2Excel(Data,firstcell,Versionflag)
function Flag = Export2Excel(Data,firstcell,Versionflag)
% EXPORT2EXCEL function exports a Matlab array / Cell array to Excel spreadsheet.
% Microsoft Excel actxserver i.e Excel Com object is used to export data
% from Matlab to Excel.Hence Microsoft Excel must be installed in the system. 
% This Function also changes the Cell Properties.Color of each Cell in the spreadsheet will be Yellow. 
% Excel has 56 Color Index Values to Choose from. Use conditional formating for changing Cell color depending on its Value.
% An Excel sheet containing the 56 colors and corresponding color indicies is included in this package(Colors.xls).
% The Maximum number of Columns supported by this release is 702. A Matlab array / Cell array with columns greater than 702 is not supported.
% Error messages corresponding to invalid input and array exceeding Columns are also displayed.
% Versionflag determines the version of Excel, this is needed because Excel 2003 can import only 256 columns (i.e from 'A' to 'IV') of data and 
% Excel 2007 allows a large number of columns to be imported, but in this program no of columns for Excel 2007 are restricted 702 (i.e from 'A' to 'ZZ')
% E.g. 1) Flag = Export2Excel(ones(100,100),'A4');
%      2) x = [1 2 3 4];
%         Flag = Export2Excel(x,'B4');
%======= Input Parameters  ==========
% Data        -    Matlab array / Cell array which needs to exported to the Spreadsheet.
% firstcell   -    Cell from which you want to place the array data. [ Range supported col 'A' to col 'ZZ' ]
% Versionflag -    0 for Excel 2003 or Older version , 1 for Excel 2007. (since Excel 2003 supports 256 Columns only )
%                  Default Value is 0.
%======= Output Parameters ==========
% Flag        -    Value is 1 if export is sucessfull else Value is 0. 

%===== Declarations ===========%
columns = ['A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z'];
count_na = 0;
count_col = 0;
col_count = 0;
%===== error checking =========%
error(nargchk(2,3,nargin));
array_flag = CHK_hasSize(Data,'any','any');
if array_flag ~= 1 && ~ischar(Data)
error('Input is not an Matlab array/ Cell array');
end
[m n] = size(Data);
if ~ischar(firstcell)
error('Cell input is not a string')    
end  
if nargin == 2
Versionflag = 0;
Max_columns = 256;
else
if Versionflag ~= 0 && Versionflag ~= 1     
error('Invalid Version Flag Specified'); 
else
Max_columns = 702;    
end
end
%==== Computation =============%
len_cell = length(firstcell);
%find the rownumber from given cell
if len_cell == 2
 col_number = find(columns == upper(firstcell(1)));
 row_number = str2num(firstcell(2:end));    
elseif len_cell == 3
 col_number1 = find(columns == upper(firstcell(1)));   
 col_number2 = find(columns == upper(firstcell(2)));
 col_number = length(columns)*col_number1 + col_number2;
 row_number = str2num(firstcell(3:end));
elseif len_cell == 1
 error('Invalid Cell input');
else 
 error('Invalid Cell input');   
end

% find the last cell
if ~isempty(row_number)
while (col_count ~= (col_number+n-1))
      if col_count == Max_columns
      error(sprintf('Maximum Columns Supported is %d\n',Max_columns));
      end  
      if count_na == 0
      lastcell = columns(col_count+1);
      count_col = count_col + 1;
      else     
      lastcell = strcat(columns(count_na),columns(count_col+1));
      count_col = count_col + 1;    
      end    
      if count_col==26
      count_col = 0;
      count_na = count_na + 1;
      end  
      col_count = col_count + 1;
end
lastcell = strcat(lastcell,num2str(m+(row_number-1)));
else
error('Invalid Cell input');
end

try
% Activate Server Excel application.
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
Activesheet = Excel.Activesheet;
ActivesheetRange = get(Activesheet,'Range',firstcell,lastcell);
set(ActivesheetRange, 'Value',Data);
ActivesheetRange.Interior.ColorIndex = 6; %Yellow Color
ActivesheetRange.Font.Bold = 'True';
ActivesheetRange.Font.Size = 10;
ActivesheetRange.ColumnWidth = 11;
ActivesheetRange.Borders.LineStyle = 1;
ActivesheetRange.Borders.Weight = 2;
Flag = 1;
return;
catch
Flag = 0;
return;
end    

function result = CHK_hasSize(x,n_rows,n_cols)

%== Error checking ============================================================
error(nargchk(3,3,nargin));
   
%== Computation ===============================================================
[m,n] = size(x);
result = all((m==n_rows | n_rows=='any') & (n==n_cols | n_cols=='any'));

Contact us at files@mathworks.com