[英]how to export and then download a excel file with ajax call in background in php
[英]how to export an excel in php with columns background coloured?
我正在尝试使用以下代码导出Excel-
$data = array(
array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
array("firstname" => "James", "lastname" => "Brown", "age" => 31),
array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
);
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/excel");
$flag = false;
foreach($data as $row)
{
if(!$flag){
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, __NAMESPACE__ . '\cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
exit;
上面的代码对于导出excel来说工作正常,但是我希望excel的标头可以使用任何特定的颜色进行着色,但是找不到任何可以做到这一点的方法。 请帮忙。
您要导出的是制表符分隔的* .csv文件,而不是xlsx。
如果你想申请必须导出造型/使用类似产生一个真正的XLSX文件https://github.com/box/spout , https://github.com/PHPOffice/PHPExcel或(无耻插头) /:HTTPS /github.com/nimmneun/onesheet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.