MATLAB: How to add a Header or a Footer to an EXCEL worksheet using MATLAB 7.7 (R2008b)

20032007excelfootersheadersMATLABsheetspreadsheetwork

I am writing results to an EXCEL file using the XLSWRITE command. I currently have to manually add an EXCEL header/footer using View – > Header/Footer pull down menu items for each data worksheet. Using MATLAB, I am unable to programmatically write a Header/Footer for the worksheet I create.

Best Answer

The ability to programmatically add the Header and Footer to an Excel worksheet can be implemented in MATLAB using the COM interface.
The ACTXSERVER command can be used to create a COM server running Microsoft Excel. You can then add the Header and Footer using the different methods for the workbook and then call XLSWRITE to write the data to the Excel worksheet as follows:
a = actxserver('Excel.Application');
a.Visible =1;
a.Workbooks.Add;
a.activeWorkbook.activesheet.PageSetup.LeftFooter = 'MyFooter';
a.activeWorkbook.activesheet.PageSetup.LeftHeader = 'MyHeader';
a.activeWorkbook.SaveAs('test.xlsx');
a.activeWorkbook.Close;
xlswrite('test.xlsx',[1 2 3]);
The following options can also be modified in a similar way:
a.activeWorkbook.activeWorksheet.PageSetup.CenterHeader
a.activeWorkbook.activeWorksheet.PageSetup.RightHeader
a.activeWorkbook.activeWorksheet.PageSetup.CenterFooter
a.activeWorkbook.activeWorksheet.PageSetup.RightFooter