0

I already have my desired data retrieved from the database and output it via the browser for web use but now I am trying to insert it in to an excel doc and email it.

I have the beginnings of the document and am able to email it as an attachment fine, it's just how to transpose the html output into the excel document that I am stuck at as I can't think of the best way to do it.

This is how I achieve my HTML output (apologies for the length):

// print table
echo '<table>';
echo '<tr><th rowspan="2">Day</th>';

foreach($typesorder as $type) {
    if(in_array($type, $types)) {
        echo '<th colspan="3">' . $type . '</th>';  
    }
}

echo '<th colspan="3">Total Conversions</th>';
echo '</tr>';
echo '<tr>';

foreach($typesorder as $type) {
    if(in_array($type, $types)) {
        echo '<th>Week ' . $weekstart_A_data['week'] . ' ' . $weekstart_A_data['year'] . '</th>';  
        echo '<th>Week ' . $weekstart_B_data['week'] . ' ' . $weekstart_B_data['year'] . '</th>';  
        echo '<th>+/-</th>';  
     }
}

// Total Conversions section
echo '<th>Week ' . $weekstart_A_data['week'] . ' ' . $weekstart_A_data['year'] . '</th>';  
echo '<th>Week ' . $weekstart_B_data['week'] . ' ' . $weekstart_B_data['year'] . '</th>';  
echo '<th>+/-</th>';    

echo '</tr>';

foreach($dailytotals as $thedate => $data) {

    $daily_conversions = 0;
    $daily_conversionsB = 0;

    echo '<tr>';
    echo '<td>' . date('l', strtotime($thedate)) . '</td>';

    foreach($typesorder as $type) {

        if(in_array($type, $types)) {

                $conversions  = $data[$type];

                $total_conversions[$type]   += $conversions;
                $daily_conversions          += $conversions;

                $week_A_conversions = $conversions;
                $week_B_conversions = $dailytotalsB[$weekstartB][$type];

                $total_conversionsB[$type] += $dailytotalsB[$weekstartB][$type];

                $daily_conversionsB  += $week_B_conversions;

                $differential = $dailytotalsB[$weekstartB][$type] - $conversions;

                echo '<td>'. number_format($week_A_conversions) . '</td>';   
                echo '<td>'. number_format($week_B_conversions) . '</td>';   

                if($differential < 0 ) {
                    $class = "class='diffred'";
                } else if($differential >  0) {
                    $class = "class='diffblue'";
                } else {        
                    $class='';
                }   

                // differential between Week A and Week B
                echo '<td ' . $class . '>' . $differential . '</td>';  

        }

    }

    $weekstartB = date("Y-m-d", strtotime('+1 day', strtotime($weekstartB)));   

    $differentialtotal = $daily_conversionsB - $daily_conversions;

    echo '<td>' . number_format($daily_conversions) . '</td>';     
    echo '<td>' . number_format($daily_conversionsB) . '</td>';    

    if($differentialtotal < 0 ) {
        $class = "class='diffred'";
    } else if($differentialtotal >  0) {
        $class = "class='diffblue'";
    } else {        
        $class='';
    }

    echo '<td ' . $class . '>' . $differentialtotal . '</td>';    
    echo '</tr>';

}


echo '<tr>';
echo '<td><strong>Total</strong></td>';

// reset both week A and B
$overall_conversions = 0; 
$overall_conversionsB = 0;

foreach($typesorder as $type) {

    if(in_array($type, $types)) {

        $conversions = $total_conversions[$type];
        $overall_conversions    += $conversions;

        $conversionsB = $total_conversionsB[$type];
        $overall_conversionsB    += $conversionsB;

        echo '<th>' . number_format($conversions) . '</th>';   
        echo '<th>' . number_format($conversionsB) . '</th>';  
        echo '<th>' . number_format($conversionsB - $conversions) . '</th>';  
    }            
}

echo '<th>' . number_format($overall_conversions) . '</th>';
echo '<th>' . number_format($overall_conversionsB) . '</th>';
echo '<th>' . number_format($overall_conversionsB - $overall_conversions) . '</th>';

echo '</tr>';

echo '</table>';  

This outputs a table with the following structure:

----------------------------------------------------------------------------------
|       |           Type 1          |   Type 2  |    ...    |  Total Conversions |
| Day   -------------------------------------------------------------------------|
|       | Week 1 2013 | Week 1 2012 | ... | ... | ... | ... |         ...        |      
|--------------------------------------------------------------------------------|
|Sunday |      135    |      143    | ... | ... | ... | ... |         ...        |      
|--------------------------------------------------------------------------------|
|  ...  |      ...    |      ...    | ... | ... | ... | ... |         ...        |      
|--------------------------------------------------------------------------------|
|Total  |      ...    |      ...    | ... | ... | ... | ... |         ...        |    
----------------------------------------------------------------------------------  

Hopefully that makes some sense, the ... are just placeholders for repeating data.

I don't need to do any formulas since I already have all the data I need but I wouldn't rule it out as it might be easier to total using PHPExcel.

I know this is a horrible question but I am genuinely stumped how to start off. I'm not expecting the full, exact answer to my specific scenario (although that would be magical) but in reality any pointers would help.

P.S. I know how to insert data into the Excel document using PHPExcel but it's transposing my table that's the problem. I think the first step is to add all the data to a multi-dimensional array instead of printing it but I will see what the responses are first.

Also, please note that I don't want to output as CSV as I wish to automatically email the pre-prepared and formatted Excel sheet.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
  • Have a look at http://stackoverflow.com/questions/8082523/export-records-in-excel-file accepted answer... may point you in the right direction – Jason Fingar Jan 16 '13 at 13:28
  • thanks but I don't want to output as csv, just to email the formatted excel sheet automatically without any work needing done to it, I meant to put that in my question as I think a few people will suggest csv. – martincarlin87 Jan 16 '13 at 13:30
  • Have you tried saving your HTML output to file, then using PHPExcel's HTML Reader? I know the HTML Reader is pretty basic at the moment, and needs more work to make it useful, but it may be an option.... otherwise, wait till I've improved the HTML REader enough, or use the standard PHPExcel cell setting rather than your HTML output – Mark Baker Jan 16 '13 at 13:43
  • Hi Mark, didn't even consider it, this is the first time I've used PHPExcel (it's excellent btw) so I'm a bit of a n00b as to what it can do. Sounds like an idea though, thanks for the suggestion! – martincarlin87 Jan 16 '13 at 13:46
  • @MarkBaker just tried the HTML reader and it's *almost* perfect, only niggle is that the `rowspan` for my first column isn't being honoured. Are there any ways around this aside from using extra `th` and `td` elements and filling them with a space? Similarly with the `colspan`s but I'm sure you already know this. Quick question - are CSS style rules honoured when using the HTML reader, can't find if it's supported and if it is I must be doing it wrong. If not, is it best to import the html into the spreadsheet first and then use the inbuilt methods to then style the content? – martincarlin87 Jan 16 '13 at 16:39
  • HTML Reader doesn't support CSS (I did say it was pretty basic). But I can take a look at the rowspan and see if it's an easy fix. – Mark Baker Jan 16 '13 at 16:46
  • @MarkBaker no probs. I have used empty table elements as a quick 'hack' and now I get what I need in my excel file. I just need to now style it, not sure if there are any limitations imposed on styling when the data has been imported from an html file? e.g. when I merge and center manually on the emailed doc it gives a warning so not sure if it will be ok doing that programatically (sorry if this is a dumb question!) – martincarlin87 Jan 16 '13 at 16:50
  • The PHPExcel object doesn't actually know or care how it was generated, whether a load from file (any format) or instantiated using new; so all styling options should be available – Mark Baker Jan 16 '13 at 17:22
  • thanks Mark, you've been a great help and thanks for your work on PHPExcel. – martincarlin87 Jan 17 '13 at 08:54

0 Answers0