[英]Export Table as Excel file in php
CSS在UI中起作用,但在Excel中不起作用。 任何人都可以帮助我。
下面的代码会将表导出到excel中,但不能在标签前提供空格。
<?php
echo $excel_data = '<table border="1">
<thead>
<th align="left">S.No.</th>
<th align="left">Name</th>
<th align="left">DOJ</th>
</thead>
<tbody>
<tr>
<td align="left">1</td>
<td align="left">Sreekanth Kuriyala</td>
<td align="left">04-06-2015</td>
</tr>
<tr>
<td align="left">2</td>
<td style="padding-left:20px;">SK</td>
<td align="left">26-07-2015</td>
</tr>
</tbody>
</table>';
$excel_file = 'Reports.xls';
file_put_contents ($excel_file, $excel_data);
?>
</br>
</br>
<a href="<?php echo $excel_file; ?>" download>Export to Excel</a>
您必须创建一个CSV文件,而只需创建一个新的php文件,该文件的功能应为选择数据库的结果。
然后只是回声cols被分隔; 和承运人返回下一行。
这是我做的功能:
<?php
function printcsv($result){
//result of database select
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=example.csv');
echo "\xEF\xBB\xBF";
$nl = "\n";
//this will be the carrier return var
if($rs = $result->fetch_array(MYSQLI_NUM)){
while($rs != ''){
echo $rs[0].";".$rs[1].";".$rs[2].";".$rs[3].";".$rs[4].";".$rs[5].";".$nl;
$rs = $result->fetch_array(MYSQLI_NUM);
}
}
}
?>
通过所需的结果获取来更改我的mysqli_fetch_array并尝试=)
哦,您不能在上面写表,只能回显用分号分隔的数据。 每列的每个数据,用分号将下一个数据放入下一列,而载体返回以写在下一行。
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.