PHP:sprintf格式的excel文件

我有一个脚本,需要我的MySQL查询,并写入Excel文件。 我想在写入查询结果之前添加我自己的自定义行。我想添加一个“To date”和“From date”。 查询字段名称是在第一行,但我想将其添加到第二行,然后在第一行第一个单元格“To Date:22/01/2016”在同一行第二个单元格我想放在“从date:22/02/2016”。 目前“From date”和第二个“To date”在第二行。

<?php $sQuery="SELECT * FROM tblnames;"; $rResult = mysql_query( $sQuery) or die(); $count = mysql_num_fields($rResult); $html = '<table border="1"><thead><tr>%s</tr><thead><tbody>%s</tbody></table>'; $thead = ''; $tbody = ''; $line = '<tr>%s</tr>'; //my attempt $tbody = sprintf('<thead><tr>%s</tr><thead><tbody>%s</tbody>','','<tr><td>From Date : 22/01/2016</td><td>To Date : 22/02/2016</td></tr>'); // end of attempt for ($i = 0; $i < $count; $i++){ $thead .= sprintf('<th>%s</th>',mysql_field_name($rResult, $i)); } while(false !== ($row = mysql_fetch_row($rResult))){ $trow = ''; foreach($row as $value){ $trow .= sprintf('<td>%s</td>', $value); } $tbody .= sprintf($line, $trow); } header("Content-type: application/vnd.ms-excel; name='excel'"); header("Content-Disposition: filename=exportfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); print sprintf($html, $thead, $tbody); exit; //file_put_contents("exportfile.xls", $html.$thead.$tbody); echo '<script>alert("COMPLETE")</script>'; ?>