将SQL查询导出到CSV文件

时间:2012-02-29 22:32:26

标签: php sql-server

我需要将SQL查询导出到.csv文件。我在导出查询时使用的代码,但.csv文件在信息开头之上有2行空格。它也是(TABS)带头的第一行。有没有阻止这个?

这是我的代码:

   $comments = array(",", "\r\n", "\r", "\n");

// EXPORT STREAMLINE.CSV

    header('Content-type: text/csv');

    header('Content-Disposition: attachment; filename="streamline.csv"');  

    readfile('streamline.csv');

    $vaStreamline = $dataConnection->SelectAllWhere( "applicants", "loan_purpose='VA Streamline' AND statusdate >= '3/1/2011'", "ORDER BY statusdate DESC" );   


    echo "applicantid,active,fromsite,siteref,category,staus,statusdate,preapproved_email1,preapproved_email2,preapproved_email3,finalstatus,datefinalstatus,loan_purpose,current_rate,loan_balance,estimated_value,b_firstname,b_middlename,b_lastname,b_ssno,b_dob,b_marital,b_creditreport,b_disability,prop_address,prop_city,prop_state,prop_zip,prop_type,b_workphone,b_extension,b_homephone,b_email,b_emp_lastyeargross,coborrower,cb_firstname,cb_middlename,cb_lastname,cb_ssno,liab_latepayments,mortgage_latepayments_monthly,datecompleted,comments,mortgage_type,current_add_diff,cur_address,cur_city,cur_state,cur_zip,liab_credit_score,c_name \r\n";

     for( $i = 0 ; $i < count( $vaStreamline ) ; $i++ ) 

     echo $vaStreamline[$i][ 'applicantid' ] . ',' . $vaStreamline[$i][ 'active' ] . ',' . $vaStreamline[$i][ 'fromsite' ] . ',' . $vaStreamline[$i][ 'siteref' ] . ',' . $vaStreamline[$i][ 'category' ] . ',' . $vaStreamline[$i][ 'staus' ] . ',' . $vaStreamline[$i][ 'statusdate' ] . ',' . $vaStreamline[$i][ 'preapproved_email1' ] . ',' . $vaStreamline[$i][ 'preapproved_email2' ] . ',' . $vaStreamline[$i][ 'preapproved_email3' ] . ',' . $vaStreamline[$i][ 'finalstatus' ] . ',' . $vaStreamline[$i][ 'datefinalstatus' ] . ',' . $vaStreamline[$i][ 'loan_purpose' ] . ',' . $vaStreamline[$i][ 'current_rate' ] . ',' . str_replace(",","",trim($vaStreamline[$i][ 'loan_balance' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'estimated_value' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'b_firstname' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'b_middlename' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'b_lastname' ])) . ',' . $vaStreamline[$i][ 'b_ssno' ] . ',' . $vaStreamline[$i][ 'b_dob' ] . ',' . $vaStreamline[$i][ 'b_marital' ] . ',' . $vaStreamline[$i][ 'b_creditreport' ] . ',' . $vaStreamline[$i][ 'b_disability' ] . ',' . str_replace(",","",trim($vaStreamline[$i][ 'prop_address' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'prop_city' ])) . ',' . str_replace("0,","",trim($vaStreamline[$i][ 'prop_state' ])) . ',' . $vaStreamline[$i][ 'prop_zip' ] . ',' . $vaStreamline[$i][ 'prop_type' ] . ',' . $vaStreamline[$i][ 'b_workphone' ] . ',' . $vaStreamline[$i][ 'b_extension' ] . ',' . $vaStreamline[$i][ 'b_homephone' ] . ',' . $vaStreamline[$i][ 'b_email' ] . ',' . $vaStreamline[$i][ 'b_emp_lastyeargross' ] . ',' . $vaStreamline[$i][ 'coborrower' ] . ',' . str_replace(",","",trim($vaStreamline[$i][ 'cb_firstname' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'cb_middlename' ])) . ',' . str_replace(",","",trim($vaStreamline[$i][ 'cb_lastname' ])) . ',' . $vaStreamline[$i][ 'cb_ssno' ] . ',' . $vaStreamline[$i][ 'liab_latepayments' ] . ',' . $vaStreamline[$i][ 'mortgage_latepayments_monthly' ] . ',' . $vaStreamline[$i][ 'datecompleted' ] . ',' . str_replace($comments,"",trim($vaStreamline[$i][ 'comments' ])) . ',' . $vaStreamline[$i][ 'mortgage_type' ] . ',' . $vaStreamline[$i][ 'current_add_diff' ] . ',' . str_replace(",","",trim($vaStreamline[$i][ 'cur_address' ])) . ',' . $vaStreamline[$i][ 'cur_city' ] . ',' . $vaStreamline[$i][ 'cur_state' ] . ',' . $vaStreamline[$i][ 'cur_zip' ] . ',' . $vaStreamline[$i][ 'liab_credit_score' ] . ',' . $vaStreamline[$i][ 'c_name' ]. "\n" ;

0 个答案:

没有答案