Cell Formatting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wolfjmt
    New Member
    • Jan 2009
    • 10

    Cell Formatting

    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

    Code:
    $data = "
                <?xml version='1.0'?>
                <?mso-application progid='Excel.Sheet'?>
                <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'>
                <Worksheet ss:Name='Investors'>
                <Table>
                 ";
    $x = 0;
    for ($x = 0; $x < $resultCounter; $x++){
            $data .= "<Row>";
            $data .= "<Cell><Data ss:Type='Number'>".$numericField[$x]."</Data></Cell>";
            $data .= "<Cell><Data ss:Type='String'>".$StringField[$x]."</Data></Cell>";
           $data .= "</Row>";
    }
    
    $data .= "</Table></Worksheet></Workbook>";
    Thanks in advance for the help
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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:
    Code:
    <Cell><ss:Data ss:Type="String" xmlns="[URL]http://www.w3.org/TR/REC-html40">Partial[/URL] <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.
    Code:
    <Styles>
      <Style ss:ID="Default" ss:Name="Normal">
       <Alignment ss:Vertical="Bottom"/>
       <Borders/>
       <Font/>
       <Interior/>
       <NumberFormat/>
       <Protection/>
      </Style>
       <Style ss:ID="s29">
       <Alignment ss:Horizontal="Center" ss:Vertical="Bottom" ss:WrapText="1"/>
       <Font ss:FontName="Arial Black" x:Family="Swiss" ss:Size="12" ss:Bold="1"/>
      </Style>
     </Styles>....
    <Cell ss:StyleID="s29"><Data ss:Type="String">formatted text</Data></Cell>

    Comment

    • wolfjmt
      New Member
      • Jan 2009
      • 10

      #3
      Thanks for the help, the code you posted helped loads, thanks very much

      Comment

      • wolfjmt
        New Member
        • Jan 2009
        • 10

        #4
        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.

        Comment

        Working...