1

我有一个程序可以将文件转换为 excel 文件,但由于某种原因,我的电话和电子邮件字段被切换了。看起来像这样 -

firstname   lastname    email   phone
test                            bi@gmail.com
test            d               813-767 leighmimail.com
test            Pds         888-413 Bperotects.com

邮箱和电话应该换了

代码:

   <?php
session_start();

$num = $_SESSION['num'];
    $firstname =  $_SESSION['firstnameexport'];
    $lastname = $_SESSION['lastnameexport'];
    $email = $_SESSION['emailexport'];
    $phone = $_SESSION['phoneexport'];

    for($i =0; $i< $num; $i++){
    $data[$i] = array(
    "firstname" => $firstname[$i] , "lastname" => $lastname[$i], "email" => $email[$i], "phone" => $phone[$i]);
    }

function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

// filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }
     array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\r\n";
  }


unset($_SESSION['num'],$_SESSION['firstnameexport'],$_SESSION['lastnameexport'],$_SESSION['emailexport'],$_SESSION['phoneexport']);
?>

感谢您的时间!

4

1 回答 1

0

修复它 - 我所要做的就是切换一些会话变量,这样它就会为右列提供正确的值。

感谢您的时间!

于 2012-09-04T19:53:55.540 回答