简体   繁体   中英

How to export into excel from php leading with zeroes

Here's my code of export. Just want to lead with zeroes because that's my unique code of every students but after I export into excel the zeroes are gone. What am I suppose to do.

  function db_connect(){
include('dbcon.php');
if ($dbcon->connect_error) {
 die("Connection failed: " . $dbcon->connect_error);
}
  return $dbcon;
 }

 $filename ="Users.xls";
 header('Content-type: application/ms-excel');
 header('Content-Disposition: attachment;filename='.$filename);

 $sql = "SELECT * FROM member";
 $dbcon = db_connect();
 if($dbcon){
$result = $dbcon->query($sql);
echo "User ID\tFNAME\tLNAME\tTYPE\tCOURSE\tYEAR LEVEL\tCODE\n"; // prints header line with field names
if ($result->num_rows > 0) {// output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row['stud_num']."\t".$row['firstname']."\t".$row['lastname']."\t". $row['type']."\t".$row['course']."\t".$row['year_level']."\t".$row['code']."\n";// prints each record with five fields in a row
            }
    } 
}

汇出Excel 零 https://imgur.com/a/bUOog

Excel will input an all numeric field as an int and drop your left 0's. To trick Excels importer to see it as a text field, you need to format it like a simple forumula. In your echo line, change

."\t".$row['code']."\n"

to

."\t\"=\"\"".$row['code']."\"\"\"\n"

Cut n paste that directly in and see if that gives you the results you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM