Php 将XML数据转换为Excel
如何将具有以下结构的XML数据转换为Excel文件,其中包含格式化单元格(标题为粗体),转换后的电子表格末尾用于汇总小时数的函数,以及应用于标题的自动筛选 我使用SimpleXML将XML加载到PHP脚本中,如下所示: PHPPhp 将XML数据转换为Excel,php,xml,excel,Php,Xml,Excel,如何将具有以下结构的XML数据转换为Excel文件,其中包含格式化单元格(标题为粗体),转换后的电子表格末尾用于汇总小时数的函数,以及应用于标题的自动筛选 我使用SimpleXML将XML加载到PHP脚本中,如下所示: PHP $sxml = simplexml_load_file($url); return $sxml; 然后遍历每一行,只选择需要转换为Excel的内容,如CaseNbr、Summary和Hours\u total。其余的都被忽略了 XML <results>
$sxml = simplexml_load_file($url);
return $sxml;
然后遍历每一行,只选择需要转换为Excel的内容,如CaseNbr、Summary和Hours\u total。其余的都被忽略了
XML
<results>
<row Result_Number="1">
<CaseNbr>4845</CaseNbr>
<Urgency>High</Urgency>
<hours_total>3.75</hours_total>
<status>RESOLVED</status>
<date_resolved>1/6/2014 1:01:36 PM</date_resolved>
<customer_name>A Big Guy</customer_name>
<contact>Bane</contact>
<Address_Line1>...</Address_Line1>
<Address_Line2>...</Address_Line2>
<City>...</City>
<State>...</State>
<Summary>...</Summary>
</row>
<row Result_Number="2">
...
</row>
</results>
4845
高的
3.75
断然的
2014年1月6日下午1:01:36
大个子
祸根
...
...
...
...
...
...
编辑:
function generateReport($sxml){
// generate the report if XML data was successfully extracted
if(isset($sxml)){
$html = null;
$html .= '<p>Customer: ' . $_GET['customer'] . '</p>';
$html .= '<p>Period: ' . date('F j, Y', strtotime($_GET['from'])) . ' - ' . date('F j, Y', strtotime($_GET['to'])) . '</p>';
$total_actual_hours = 0;
$html .= '<table cellspacing="0" cellpadding="2" style="border: 1px solid black;">';
foreach($sxml->row as $row){
$html .= // for the sake of simplicity I will explain what happens here
// I echo <tr>'s, <td>'s and actual data I want like so:
// $row->Summary, and I also sanitize it
$total_actual_hours += (float)$row->hours_total; // this is the total hours of all CaseNbrs
}
$html .= '<tr style="background-color:#C0C0C0;font-weight:bold;font-size:1.15em;">';
$html .= '<td>Total(hrs): </td>';
$html .= '<td colspan="3" style="text-align:right;vertical-align:top;">' . $total_actual_hours . '</td>';
$html .= '</tr>';
$html .= '</table>';
echo $html;
}
}
函数生成器报告($sxml){
//如果成功提取XML数据,则生成报告
if(isset($sxml)){
$html=null;
$html.='Customer:'.$\u获取['Customer'].';
$html.='Period:'.date($fj,Y',strottime($\u GET['from'])).-'.date($fj,Y',strottime($\u GET['to'])。';
$total_actual_hours=0;
$html.='';
foreach($sxml->行作为$row){
$html.=//为了简单起见,我将解释这里发生的事情
//我回显我想要的数据和实际数据,如下所示:
//$row->Summary,我还对其进行了清理
$total_actual_hours+=(float)$row->hours_total;//这是所有案例的总小时数br
}
$html.='';
$html.='总小时数:';
$html.=''.$total_实际_小时''.';
$html.='';
$html.='';
echo$html;
}
}
然后,我创建一个链接,使用Javascript将回响的HTML表下载为XLS:
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
textFile = window.URL.createObjectURL(data);
return textFile;
};
document.getElementById('downloadxls').href = makeTextFile(document.getElementsByTagName('html')[0].outerHTML);
}//]]>
</script>
//
使用PHPExcel()您将从实际编写一些代码开始。我们可以先看看您的尝试吗?我们喜欢鼓励人们尝试一下,并在遇到困难时提出一个具体的问题。您可能会发现,事实证明,您可以在没有帮助的情况下完成所有这一切!更新。我的方法是可行的,但没有办法让Excel将一个现成的SUM()嵌入基于hours\u total单元格的hours\u actual\u total单元格中