0

我已经从数据库中检索到所需的数据并通过浏览器将其输出以供 Web 使用,但现在我正尝试将其插入到 Excel 文档中并通过电子邮件发送。

我有文档的开头,并且可以将其作为附件通过电子邮件发送,这只是如何将 html 输出转换为我坚持的 excel 文档,因为我想不出最好的方法。

这就是我实现 HTML 输出的方式(对长度表示歉意):

// 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>';  

这将输出具有以下结构的表:

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

希望这是有道理的,...它们只是重复数据的占位符。

我不需要做任何公式,因为我已经拥有了我需要的所有数据,但我不会排除它,因为使用 PHPExcel 进行总计可能更容易。

我知道这是一个可怕的问题,但我真的很难过如何开始。我并不期待对我的具体情况给出完整、准确的答案(尽管那会很神奇),但实际上任何指针都会有所帮助。

PS 我知道如何使用 PHPExcel 将数据插入 Excel 文档,但它正在转置我的表格,这就是问题所在。我认为第一步是将所有数据添加到多维数组而不是打印它,但我会先看看响应是什么。

另外,请注意我不想输出为 CSV,因为我希望自动通过电子邮件发送预先准备好的和格式化的 Excel 表。

4

0 回答 0