I want help in generating excel sheet using php.

WHen the value is like 001 or any number starting with 0 that 0 is not dispaying …
Please Help me in getting the value as it is entered in excell sheet also .
this is my code :-

<?php $fdate = $_GET['fdate']; $tdate = $_GET['tdate']; $logcarts = $_GET['logcarts']; mysql_connect("localhost", "pharmsource", "pharmsource") or die(mysql_error()); mysql_select_db("affiliate") or die(mysql_error()); $quer = $count = 0; $header=""; $sqlquery = "select neworders.ordernumber,orderdate,ordertype,prorealname,category,salesprice,loguser,agentname,customername,dob,gender,height,weight,shipping,city,state,zipcode,telephone,bill,city2,state2,zip2,shipping2,ndccard,cncsale,cvvnumber,nameonaccount,accnumber,routingnumber,orderstatus,tracknumber,commission,admincomm,expmonth,expyear from neworders, addquality where neworders.username=addquality.id and orderdate BETWEEN '$fdate ' AND '$tdate ' AND loguser='$logcarts' AND orderstatus='Pending' ORDER BY ordernumber DESC"; $result = mysql_query($sqlquery) or die(mysql_error()); $count = mysql_num_fields($result); for ($i = 0; $i < $count; $i++) { $columnname = mysql_field_name($result, $i); if ($columnname == "ordernumber") {$columnname = "Order Number";} if ($columnname == "orderdate") {$columnname = "Order Date";} if ($columnname == "ordertype") {$columnname = "Order Type";} if ($columnname == "prorealname") {$columnname = "Product";} if ($columnname == "category") {$columnname = "Category";} if ($columnname == "salesprice") {$columnname = "Sales Price";} if ($columnname == "loguser") {$columnname = "Affiliate";} if ($columnname == "agentname") {$columnname = "Quality";} if ($columnname == "customername") {$columnname = "Customer Name";} if ($columnname == "dob") {$columnname = "Date of Birth";} if ($columnname == "gender") {$columnname = "Gender";} if ($columnname == "height") {$columnname = "Height";} if ($columnname == "weight") {$columnname = "Weight";} if ($columnname == "shipping") {$columnname = "Shipping Address";} if ($columnname == "city") {$columnname = "City";} if ($columnname == "state") {$columnname = "State";} if ($columnname == "zipcode") {$columnname = "Zip";} if ($columnname == "telephone") {$columnname = "Telephone Number";} if ($columnname == "bill") {$columnname = "Billing Address";} if ($columnname == "city2") {$columnname = "City_Billing";} if ($columnname == "state2") {$columnname = "State_Billing";} if ($columnname == "zip2") {$columnname = "Zip_Billing";} if ($columnname == "shipping2") {$columnname = "Shipping Type";} if ($columnname == "ndccard") {$columnname = "Name On Debit And Credit Card";} if ($columnname == "cncsale") {$columnname = "Card Number";} if ($columnname == "cvvnumber") {$columnname = "Cvv Number";} if ($columnname == "nameonaccount") {$columnname = "Name On Account";} if ($columnname == "accnumber") {$columnname = "Account Number";} if ($columnname == "routingnumber") {$columnname = "Routing Number";} if ($columnname == "orderstatus") {$columnname = "Order Status";} if ($columnname == "tracknumber") {$columnname = "Tracking Number";} if ($columnname == "commission") {$columnname = "Affliate Commission";} if ($columnname == "admincomm") {$columnname = "Admin Commission";} if ($columnname == "expmonth") {$columnname = "Expiry Month";} if ($columnname == "expyear") {$columnname = "Expiry Year";} $header .= $columnname."\t"; } $data=""; while($row = mysql_fetch_row($result)) { $line = ''; foreach($row as $value) { if(!isset($value) || $value == "") { $value = "\t"; } else { # important to escape any quotes to preserve them in the data. $value = str_replace('"', '""', $value); # needed to encapsulate data in quotes because some data might be multi line. # the good news is that numbers remain numbers in Excel even though quoted. $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } # this line is needed because returns embedded in the data have "\r" # and this looks like a "box character" in Excel $data = str_replace("\r", "", $data); # Nice to let someone know that the search came up empty. # Otherwise only the column name headers will be output to Excel. if ($data == "") { $data = "\nno matching records found\n"; } $count = mysql_num_fields($result); # This line will stream the file to the user rather than spray it across the screen header("Content-type: application/octet-stream"); //header("Content-type: text/plain"); # replace excelfile.xls with whatever you want the filename to default to header("Content-Disposition: attachment; filename=Affiliate Marketing.xls"); header("Pragma: no-cache"); header("Expires: 0"); //echo $header."\n".$data; echo $header."\n".$data."\n"; ?>

Thankyou Yours Faithfully
Phaneendra

That is actually the default behavior of excel. I would look at changing the column type in excel, that is going to be where your problem is.

First, I would use a simple array of column names and do a simple replace for them instead of all of those
“IF” statements. Would be much simpler and a lot faster.

Next, showing leading zeros can be done in Excel, but, it is a formatting issue. You would need to set
Excel’s number format to show the leading zeros. There are a lot of problems writing numbers into a file
that is going to be loaded into Excel. You appear to be using “quoted” numbers, so “001” should show up
in Excel as “1” unless you have that cell formatted to show the leading zeros. Since your code does not
show much in the way of Excel formatting code, it appears this might be the issue.

Just for your info, when I create an Excel sheet from PHP, I use a library that allows you full control over all
of Excel’s functions. This includes colors, formatting, custom formatting, images and just about anything you
might want to place in an Excel sheet. The library is free and you can find it at:
https://phpexcel.codeplex.com/
There is a slight learning curve on getting your first sheet to work as you want it, but, after that it is easy to
create any type of sheet with multiple linked pages, tabs and anything you can do in Excel. Might help you
if you plan to do more complex spreadsheets in the future…

Not sure if this helps, but, good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service