我有一个脚本,它接受我的mysql查询并写入excel文件。我想在写入查询结果之前添加自己的自定义行。我想添加“To date”和“From date”。查询字段名称在第一行,但我想在第二行添加它,然后在第一行第一个单元格“To Date:22/01/2016”在同一行第二个单元格中我想放入“从日期:22/02/2016”。目前,“起始日期”和第二个“迄今为止”位于第二行。
<?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>';
?>