472,133 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Cell Formatting

10
Hi
I am currently writing excel(.xls) files using xml and would like to format the cells so that when the file is opened the cells are already formatted.Does anybody out there know how this is done

Expand|Select|Wrap|Line Numbers
  1. $data = "
  2.             <?xml version='1.0'?>
  3.             <?mso-application progid='Excel.Sheet'?>
  4.             <Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>
  5.             <Worksheet ss:Name='Investors'>
  6.             <Table>
  7.              ";
  8. $x = 0;
  9. for ($x = 0; $x < $resultCounter; $x++){
  10.         $data .= "<Row>";
  11.         $data .= "<Cell><Data ss:Type='Number'>".$numericField[$x]."</Data></Cell>";
  12.         $data .= "<Cell><Data ss:Type='String'>".$StringField[$x]."</Data></Cell>";
  13.        $data .= "</Row>";
  14. }
  15.  
  16. $data .= "</Table></Worksheet></Workbook>";
  17.  
Thanks in advance for the help
Jan 14 '09 #1
3 8402
jkmyoung
2,057 Expert 2GB
What type of formatting? Bold, Italic, and Underline are easy: just add <b>,<i>, or <u> tags like in html. (note, this is not the microsoft office proper way, but is faster) eg:
Expand|Select|Wrap|Line Numbers
  1. <Cell><ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">Partial <B>Bold</B></ss:Data></Cell>
gives you a cell:
Partial Bold

Another way is to define styles. Change your <Styles> node (or add one to your Workbook node if you don't already have it.)

Here we defined one style, and we are applying to a cell by using the cell's styleID attribute.
Expand|Select|Wrap|Line Numbers
  1. <Styles>
  2.   <Style ss:ID="Default" ss:Name="Normal">
  3.    <Alignment ss:Vertical="Bottom"/>
  4.    <Borders/>
  5.    <Font/>
  6.    <Interior/>
  7.    <NumberFormat/>
  8.    <Protection/>
  9.   </Style>
  10.    <Style ss:ID="s29">
  11.    <Alignment ss:Horizontal="Center" ss:Vertical="Bottom" ss:WrapText="1"/>
  12.    <Font ss:FontName="Arial Black" x:Family="Swiss" ss:Size="12" ss:Bold="1"/>
  13.   </Style>
  14.  </Styles>....
  15. <Cell ss:StyleID="s29"><Data ss:Type="String">formatted text</Data></Cell>
  16.  
Jan 14 '09 #2
wolfjmt
10
Thanks for the help, the code you posted helped loads, thanks very much
Jan 15 '09 #3
wolfjmt
10
Hi rlejason

Thanks for the advice, I did manage to get the cells formated correctly (using XML). It was for a site the used PHP, thus the XML way of doing it was required ... I think

Thank anyway thou.
Aug 11 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by James | last post: by
2 posts views Thread by Paul E Collins | last post: by
2 posts views Thread by GV | last post: by
2 posts views Thread by Brian Parker | last post: by
2 posts views Thread by patrick.waldo | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.