获取HTML输出并通过PHPExcel插入它的Excel文档?
我已经从数据库中检索到了所需的数据,并通过浏览器将其输出以供web使用,但现在我正在尝试将其插入excel文档并通过电子邮件发送 我有文档的开头,并且能够将其作为附件发送给电子邮件。很好,这只是如何将html输出转换为excel文档,我一直在做这件事,因为我想不出最好的方法 这就是我实现HTML输出的方式(抱歉长度太长): 希望这有点道理,获取HTML输出并通过PHPExcel插入它的Excel文档?,php,multidimensional-array,phpexcel,Php,Multidimensional Array,Phpexcel,我已经从数据库中检索到了所需的数据,并通过浏览器将其输出以供web使用,但现在我正在尝试将其插入excel文档并通过电子邮件发送 我有文档的开头,并且能够将其作为附件发送给电子邮件。很好,这只是如何将html输出转换为excel文档,我一直在做这件事,因为我想不出最好的方法 这就是我实现HTML输出的方式(抱歉长度太长): 希望这有点道理,…只是重复数据的占位符 我不需要做任何公式,因为我已经有了我需要的所有数据,但我不会排除它,因为使用PHPExcel进行合计可能更容易 我知道这是一个可怕的问
…
只是重复数据的占位符
我不需要做任何公式,因为我已经有了我需要的所有数据,但我不会排除它,因为使用PHPExcel进行合计可能更容易
我知道这是一个可怕的问题,但我真的很困惑如何开始。我并不期待对我的具体场景给出完整、准确的答案(尽管那会很神奇),但事实上任何指针都会有所帮助
另外,我知道如何使用PHPExcel将数据插入Excel文档,但问题在于它正在转换我的表。我认为第一步是将所有数据添加到多维数组中,而不是将其打印出来,但我将首先查看响应
另外,请注意,我不希望输出为CSV,因为我希望自动通过电子邮件发送预先准备好并格式化的Excel表格。查看已接受的答案。。。可能会为您指出正确的方向谢谢,但我不想以csv形式输出,只是为了自动通过电子邮件发送格式化的excel工作表,而无需对其进行任何操作,我想把这一点放在我的问题中,因为我想一些人会建议使用csv。您是否尝试过将HTML输出保存到文件中,然后使用PHPExcel的HTML阅读器?我知道HTML阅读器目前非常基本,需要更多的工作才能使其有用,但它可能是一种选择。。。。否则,等到我已经改进了HTML阅读器,或者使用标准的PHPCELL单元设置,而不是你的HTML OutPuthi标记,甚至没有考虑它,这是我第一次使用PHPExcel(它是优秀的BTW),所以我有点N0B关于它能做什么。听起来是个好主意,谢谢你的建议@MarkBaker刚刚尝试了HTML阅读器,它几乎完美无瑕,唯一的问题是,我的第一篇专栏文章的
行span
没有得到尊重。除了使用额外的th
和td
元素并用空格填充它们之外,还有其他方法可以解决这个问题吗?与colspan
s类似,但我相信您已经知道这一点。快速提问-使用HTML阅读器时是否遵守CSS样式规则,是否不知道它是否受支持,是否受支持?我一定是做错了。如果不是,那么最好先将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 | ... | ... | ... | ... | ... | ... | ... |
----------------------------------------------------------------------------------