Php 创建不带库的excel文件
我使用此代码:Php 创建不带库的excel文件,php,excel,Php,Excel,我使用此代码: header( "Content-Type: application/vnd.ms-excel" ); header("Content-Disposition: attachment; filename=liste.xls"); echo "\xEF\xBB\xBF"; 我在while循环中编写SQL数据,如下所示: echo "\n<table><tr><td>".$first[$post_a]."</td>\t<td&
header( "Content-Type: application/vnd.ms-excel" );
header("Content-Disposition: attachment; filename=liste.xls");
echo "\xEF\xBB\xBF";
我在while循环中编写SQL数据,如下所示:
echo "\n<table><tr><td>".$first[$post_a]."</td>\t<td>".$second[$post_a]."</td>\t<td>".$third[$post_a]."</td>\t</td></tr></table>";
echo“\n”.$first[$post\u a]。“\t”.$second[$post\u a]。“\t”.$third[$post\u a]。“\t”;
当我试图打开这个创建的文件时,excel会说版本和类型不匹配,所以我可以在桌面上发出警告后打开文件,但当我试图在手机上打开文件时,警告后无法打开文件。您需要在此处执行类似操作。也可以忽略我在for循环中的逻辑,可以使用自己的
$filename = "export_".date("m-d-Y_hia") . ".csv";
ob_end_clean();
$fp = fopen($filename,"w");
$is_header = true;
$no_meta_appnd = array('SKU','Name','Publish','Categories','Description');
foreach ($res as $index => $product){
$arr = array();
if($is_header){ // put all the header in array
foreach ($product as $index => $p){
if(in_array($index,$no_meta_appnd)){
array_push($arr,$index);
}else{
array_push($arr,"Meta: ".$index);
}
}
$is_header = false;
fputcsv($fp,$arr); // add headers
$arr = array(); // empty out array for first row
foreach ($product as $index => $p){
array_push($arr,$p);
}
fputcsv($fp,$arr); // add first row after header
}else{
foreach ($product as $index => $p){
array_push($arr,$p);
}
fputcsv($fp,$arr); // rest of the rows
}
}
//unserialize();
fclose($fp);
// download
// header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; ");
header('Pragma: no-cache');
readfile($filename);
// deleting file
unlink($filename);
exit();
嗯,HTML表格不能生成Excel文件。伪造MIME类型对于没有导入筛选器的客户端不起作用。因此,您的问题的答案是:从技术上讲,no.PD of它仍然不是Excel文件,但它可能是一种更好的方法。当你们写一个答案时,你们应该确切地解释你们的代码是做什么的谢谢你们的答案,但我需要xls文件格式。