MATLAB: Is there an example of printing an Excel sheet programmatically from MATLAB 7.8 (R2009a)

MATLAB

I have written data to an Excel file using the XLSWRITE function using the following code.
filename = 'C:\SomeExcelFile.xls';
% Write Random Data
xlswrite(filename,rand(10));
Now I would like to programmatically print this file.

Best Answer

Using the following workflow, you can print your Excel file to a printer.
filename = 'C:\SomeExcelFile.xls';
% Write Random Data
xlswrite(filename,rand(10));
Excel = actxserver('excel.application');
Excel.visible = 1
Workbooks = Excel.Workbooks;
% Make Excel visible
Excel.Visible=1;
% Open Excel file
Workbook=Workbooks.Open(filename);
% The Syntax for PrintOut on the Excel Developer reference is:
% PrintOut(From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate,
% PrToFileName, IgnorePrintAreas)
% It is possible to pass these arguments to the function as shown below
Excel.ActiveWorkbook.PrintOut(1,1,1,'False','PrinterName');
% You can also use the default settings and just PrintOut
% Excel.ActiveWorkbook.PrintOut
Excel.Quit;