www.sas.com > SAS UK > In the Know Homepage Search | Contact Us     
SAS UK Newsletter Banner SAS - The power to know(tm)  

How to Change the Formatting on an Excel Spreadsheet when using DDE

The example below demonstrates how to create a formatted Excel spreadsheet using DDE. The code creates sample data and then writes it out to DDE having formatted the text using different fonts, font sizes and colours.

The colours numbered up to 16 have been outlined below although numbers above 16 can also be selected. I.e. 0 or 1 = Black, 2 = White, 3 = Red, 4 = Light Green, 5 = Blue, 6 = Yellow, 7 = Pink, 8 = Light Blue, 9 = Brown, 10 = Green, 11 = Dark Blue, 12 = Dark Green, 13 = Dark Pink, 14 = Pale Green, 15 = Grey, 16 = Dark Grey.

 
   /* Set options                                               */
options noxwait noxsync;

   /* Create some test data                                     */
data aklsales;
   input type $1-10 area $12-26 year 28-31 value 33-40;
   cards;
Art Deco   Mount Eden      1938  250000
Art Deco   Mount Albert    1935  284000
Villa      Mount Eden      1920  425000
Town House Ponsonby        1992  225000
Villa      Epsom           1932  338000
Flats      Mount Roskill   1970  212500
;

/* Start up Excel - Change the directory structure below to where Excel.exe is installed on your machine   */

x ' "c:\program files\Microsoft office\office10\excel.exe" ';

   /* Wait for application to start up                          */
data _null_;
   x=sleep(5);
run;

 /* Create a spreadsheet and format the cells using the following attributes in order from left to right.  
i.e. Font, Size, Bold, Italic, Underline, Strike -Through, Colour, Outline and Shadow. 
To turn an option on, change the value FALSE to TRUE.  */

filename cmds dde 'excel|system';
filename pop dde 'Excel|D:\temp\[example.XLS]Sheet1!R1C1:R13C5';

data _null_;
   file cmds ;
   /* Send save as command                                      */
put '[save.as("D:\temp\example.XLS")]';
   /* Select and format title cells                             */
    put '[select("sheet1!R1C1:R1C4")]';
    put '[format.font("Century Gothic",20,FALSE,FALSE,TRUE,FALSE,3,FALSE,FALSE)]';
    put '[select("sheet1!R2C1:R2C1")]';
    put '[format.font("Times Roman",16,FALSE,FALSE,TRUE,FALSE,1,FALSE,FALSE)]';
	put '[select("sheet1!R11C1:R11C4")]';
    put '[format.font("Century Gothic",20,FALSE,FALSE,TRUE,FALSE,3,FALSE,FALSE)]';
    put '[select("sheet1!R12C1:R12C1")]';
    put '[format.font("Times Roman",16,FALSE,FALSE,TRUE,FALSE,1,FALSE,FALSE)]';
   /* Select and column headings                                */
    put '[select("sheet1!R4C1:R4C5")]';
    put '[format.font("Times Roman",12,FALSE,FALSE,TRUE,FALSE,5,FALSE,FALSE)]';

data _null_;
   set aklsales end=last;
   file pop notab;
   if _n_=1 then do;
   /* Set up report and column headings                         */
    put  "Central Realty Limited";
    put 'Housing Sales for the Central Auckland Region.';
    put ' ';
    put 'Type' '09'x 'Area' '09'x 'Year' '09'x 'Value';
    end;

   /* Pass data                                                 */
  put Type '09'x Area '09'x Year '09'x Value;
  if last  then do;
  put "This is a footnote";
  put 'Housing Sales for the Central Auckland Region.';
  end;
  run;

data _null_;
      file cmds;
       put '[save()]';
       put '[close()]';
        x = sleep(3);
       put '[quit()]';
run;
	

The Power to Know
   Contact Us      Worldwide Sites     Search     Site Map     RSS Feeds     Terms of Use    Privacy Statement   Copyright © 2008 SAS Institute Inc. All Rights Reserved