使用PHP创建CSV文件 [英] Creating a CSV file with PHP

查看:123
本文介绍了使用PHP创建CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过php创建一个csv文件,我从我的数据库获取一些值。

I'm creating a csv file via php and I'm getting some values from my database.

我获得了一个成功的值并生成了csv。

I'm getting a value successfully and generating the csv excellently.

我有一个像这样的值0012345和0051234

I have a value like this 0012345 and 0051234

我的问题是,当我查看我的csv文件中的数据,00不显示只有12345和51234。

My problem here is when I view the data in my csv file, the 00 is not displaying only the 12345 and 51234.

当我检查我的excel,我去格式单元格,它似乎它被设置为通用模式。它应该设置为Text,以便它将接受00作为数字的第一个数字。

When I check on my excel and I go to Format Cells, it seems it is set into General mode. It should be set into Text so that it would accept 00 as the first digit of the number.

这是我的代码:

$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;
    }



我得到这样的结果:
Ade you 67654578 97865 30151.52 AfrotradeGroup \
67654578应为067654578,97865应为097865

I'm getting a result like this: Ade you 67654578 97865 30151.52 AfrotradeGroup\ The 67654578 should be 067654578 and 97865 should be 097865

这个问题的解决方案是什么?
提前感谢并有一个美好的一天。

What's the solution for this problem? Thanks in advance and have a great day.

推荐答案

看来问题是你必须使用铸件运营商。

It seems the problem is that you have to use the casting operators.

$myText = (string)$myVar;

这种方式将变量用作文本而不是数字。

This way the variable is used as a text and not as a number.

这篇关于使用PHP创建CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆