0

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

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

I have a value like this 0012345 and 0051234

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

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.

This is my code:

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

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.

2
  • It sounds like your csv is being created correctly. If you are only using this inside of Excel one way might be to add ` before your number so `0012345 instead of just 0012345. If I remember my Excel correctly then the ` tells it to accept the value as text. Not sure if it will work in a CSV though worth a try. Commented Jul 30, 2011 at 11:42
  • If ` doesn't work, try outputting a single quote before the numbers: msofficeforums.com/excel/… If that doesn't work, you could open the .csv file as plain-text and ensure that this is definitely a problem on the Excel end. Commented Jul 30, 2011 at 11:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@arunkumar: I've tried putting ' before the values, the '(apostrophe) is also printed in the csv. I want something that it won't be printed in the csv.
@Hans Engel: The apostrophe is also printed in the csv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.