我正在通过 php 创建一个 csv 文件,并从数据库中获取一些值。
我成功获取了一个值并出色地生成了 csv。
我有一个像这样的值 0012345 和 0051234
我的问题是,当我查看 csv 文件中的数据时,00 不只显示 12345 和 51234。
当我检查 Excel 并转到“设置单元格格式”时,它似乎已设置为“常规”模式。应将其设置为 Text,以便接受 00 作为数字的第一位。
这是我的代码:
$csv_filename="ePay.csv";
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=$csv_filename");
include("/includes/config.php");
$sql = "SELECT * FROM moneyroute where sender_country ='Nigeria' and status = 'new'";
#echo $sql;
$olu="AfrotradeGroup";
$result=mysql_query($sql);
if(mysql_num_rows($result)>0){
$fileContent="Beneficiary Name,Beneficiary Account No,Beneficiary Bank Code,Transaction Amount,Narration\n";
while($data=mysql_fetch_array($result))
{
$name_=$data['sender_first_name']." ".$data['sender_last_name'];
if($data['status']=='u')
$sta='Approved';
else
$sta='Pending';
$fileContent.= "".$data['receiver_first_name']." ".$data['receiver_last_name'].",".$data['accountno'].",".$data['sortcode'].",".$data['equal_currency'].",".$olu."\n";
}
$fileContent=str_replace("\n\n","\n",$fileContent);
echo $fileContent;
}
我得到这样的结果: 阿德你67654578 97865 30151.52 AfrotradeGroup\ 67654578 应为 067654578,97865 应为 097865
这个问题有什么解决办法吗? 提前致谢,祝您有美好的一天。
最佳答案
问题似乎在于您必须使用强制转换运算符。
$myText = (string)$myVar;
这样变量就被用作文本而不是数字。
关于php - 使用 PHP 创建 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6882862/