我有一个程序可以将文件转换为 excel 文件,但由于某种原因,我的电话和电子邮件字段被切换了。看起来像这样 -
firstname lastname email phone
test bi@gmail.com
test d 813-767 leighmimail.com
test Pds 888-413 Bperotects.com
邮箱和电话应该换了
代码:
<?php
session_start();
$num = $_SESSION['num'];
$firstname = $_SESSION['firstnameexport'];
$lastname = $_SESSION['lastnameexport'];
$email = $_SESSION['emailexport'];
$phone = $_SESSION['phoneexport'];
for($i =0; $i< $num; $i++){
$data[$i] = array(
"firstname" => $firstname[$i] , "lastname" => $lastname[$i], "email" => $email[$i], "phone" => $phone[$i]);
}
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// filename for download
$filename = "website_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
foreach($data as $row) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
unset($_SESSION['num'],$_SESSION['firstnameexport'],$_SESSION['lastnameexport'],$_SESSION['emailexport'],$_SESSION['phoneexport']);
?>
感谢您的时间!