我用PHP导出Excel的时候用\t换行.导出的字段有"客户,客户PO号,货品名称,数量,单价,总金额,前期制作费,物料费,加工费,折扣%,其它开销",在Excel重,每当PHP输出到"货品名称"的时候就会自动换行.请大家帮帮忙求解.先谢谢大家了.
下面是导出Excel的PHP源码:<?php
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=财务报表.xls");

include('../config.php');

$conn = mysql_connect($hostwf, $wfuser, $pawfssword);
mysql_set_charset('utf8', $conn);
mysql_select_db($dwbf); $t1="客户";
$t2="客户PO号";
$t3="货品名称";
$t4="数量";
$t5="单价";
$t6="总金额";
$t7="前期制作费";
$t8="物料费";
$t9="加工费";
$t10="折扣%";
$t11="其它开销";
$t12="盈利";

//输出表标题  
   print ("财务报表\n\n");   
 //输出标题字段:
   printf("%s\t",$t1);
   printf("%s\t",$t2);
   printf("%s\t",$t3);
   printf("%s\t",$t4);
   printf( "%s\t",$t5);
   printf("%s\t",$t6);
   printf("%s\t",$t7);
   printf("%s\t",$t8);
   printf("%s\t",$t9);
   printf("%s\t",$t10);
   printf("%s\t",$t11);
   printf("%s\t",$t12);
   echo "\t\n";
   
   $postr = sprintf("select * from po where status != 'c'");
   $poq = mysql_query($postr);
   
   for($i=0;$i<mysql_num_rows($poq);$i++){

$wstr = sprintf("select wo_no from wo where parent_poid = '%s'", mysql_result($poq, $i,'po_id'));
$wpq = mysql_query($wstr);
$wip = "";
$sub_pp_total = 0;
$sub_mat_total = 0;
$sub_lab_total = 0;

for($j=0; $j<mysql_num_rows($wpq);$j++){
$wip .= sprintf("%s",mysql_result($wpq,$j,'wo_no'));
if($j<mysql_num_rows($wpq)-1){
$wip .= ", ";
}
$cstr = sprintf("select * from costing_main where wono = '%s'", mysql_result($wpq,$j,'wo_no'));
$costq = mysql_query($cstr);
if(!$costq){
echo mysql_error();
}
$sub_pp_total += mysql_result($costq,0,'total_pp');
$sub_mat_total += mysql_result($costq,0,'total_mat');
$sub_lab_total += mysql_result($costq,0,'total_lab');
}

if(!mysql_result($poq,$i,'c_name')){
$cname = "客户";
}else{
$cname = mysql_result($poq,$i,'c_name');
}
if(!mysql_result($poq,$i,'po_no')){
$pono = "客户PO号";
}else{
$pono = mysql_result($poq,$i,'po_no');
}
if(!mysql_result($poq,$i,'description')){
$desc = "货品名称";
}else{
$desc = mysql_result($poq,$i,'description');
}
if(!mysql_result($poq,$i,'quantity')){
$quantity = "数量";
}else{
$quantity = mysql_result($poq,$i,'quantity');
}
if(!mysql_result($poq,$i,'unit_cost')){
$ucost = "单价";
}else{
$ucost = mysql_result($poq,$i,'unit_cost');
}
if(!mysql_result($poq,$i,'amount')){
$amount = "总金额";
}else{
$amount = mysql_result($poq,$i,'amount');
}
if(!mysql_result($poq,$i,'discount')){
$discount = "无";
}else{
$discount = mysql_result($poq,$i,'discount');
}
if(!mysql_result($poq,$i,'other_expenses')){
$other_expenses = "无";
}else{
$other_expenses = mysql_result($poq,$i,'other_expenses');
}
if(!isset($sub_pp_total)){
$pp_total = "前期制作费";
}else{
$pp_total = $sub_pp_total;
}
if(!isset($sub_mat_total)){
$mat_total = "物料费";
}else{
$mat_total = $sub_mat_total;
}
if(!isset($sub_lab_total)){
$lab_total = "加工费";
}else{
$lab_total = $sub_lab_total;
}

if(isset($other_expenses) && isset($pp_total) && isset($mat_total) && isset($lab_total) && isset($discount) && isset($amount)){
$profit = ($amount * (100-$discount)/100) - ($other_expenses + $pp_total + $mat_total + $lab_total);
if($profit < 0){
$profit = $profit * -1;
}else{
}
}else{
$profit = "盈利";
}


printf("%s\t",$cname);
printf("%s(工单号:%s)\t",$pono, mysql_result($poq,$i,'po_id'));
printf("%s\t",$desc);
printf("%s\t",$quantity);
printf("%s\t",$ucost);
printf("%s\t",round($amount,2));
printf("%s\t", $pp_total);
printf("%s\t", $mat_total);
printf("%s\t",$lab_total);
printf("%s\t",$discount);
printf("%s\t",$other_expenses);
printf("%s\t",$profit);
echo "\t\n";
   }
?>

解决方案 »

  1.   

    可以换一种方式来解决。我有了。<?php
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: filename=销售报表.xls');
    ?>
    <html>
    <head>
    <title></title>
    <style>
    td{
        font-size: 12px;
        vertical-align: middle;}
    strong{
        font-size: 14px;}
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <div><b>销售报表</b></div>
    <table width="100%" border="1" align="center" cellspacing="1" cellpadding="1">
      <tr align="center">
        <td nowrap><b>客户名称</b></td>
        <td nowrap><b>客户PO号</b></td>
        <td nowrap><b>PO数量</b></td>
        <td nowrap><b>货品名称</b></td>
        <td nowrap><b>单价</b></td>
        <td nowrap><b>送货单号</b></td>
        <td nowrap><b>出货数量</b></td>
      </tr>
      <?php
    include('../config.php');
    $conn = mysql_connect($hostwf, $wfuser, $pawfssword);
    mysql_set_charset('utf8', $conn);
    mysql_select_db($dwbf);

    $dstr = sprintf("select * from dnote where status = 'f'");
    $dq = mysql_query($dstr);for($i=0; $i<mysql_num_rows($dq); $i++){
    $dstr2 = sprintf("select t1.c_name as c_name, t1.dnote_po as po_id, t1.delivered_quantity as delivered, t1.timestamp as ddate, t2.g_name as description from dnote as t1 inner join dnote_child as t2 where t1.dnote_no = t2.dnote_no and t1.dnote_no = '%s'", mysql_result($dq,$i,'dnote_no'));

    $dq2 = mysql_query($dstr2); $descstr = "";
    $quanstr = "";

    for($k=0; $k<mysql_num_rows($dq2);$k++){
    $descstr .= sprintf("%s<br />", mysql_result($dq2,$k,'description'));
    $quanstr .= sprintf("%s<br />", mysql_result($dq2,$k,'quantity'));
    } $postr = sprintf("select po_no, quantity, unit_cost, currency from po where po_id='%s'", mysql_result($dq,$i,'dnote_po'));
    $poq = mysql_query($postr);

    if(!mysql_result($poq,0,'po_no')){
    $po_no = ";"; 
    }else{
    $po_no = mysql_result($poq,0,'po_no');
    }
    if(!mysql_result($dq2,0,'c_name')){
    $c_name = "";
    }else{
    $c_name = mysql_result($dq2,0,'c_name');
    }
    if(!mysql_result($dq,$i,'dnote_no')){
    $dnote_no = " ";
    }else{
    $dnote_no = sprintf("<a href=\"dnote_detail.php?dnote_no=%s\" target=\"_blank\" style=\"text-decoration:none\">%s</a>",mysql_result($dq,$i,'dnote_no'),mysql_result($dq,$i,'dnote_no'));
    }
    if(!mysql_result($poq,0,'quantity')){
    $quantity = " ";
    }else{
    $quantity = mysql_result($poq,0,'quantity');
    }
    if(empty($descstr)){
    $description = " ";
    }else{
    $description = $descstr;
    }
    if(!mysql_result($poq,0,'unit_cost')){
    $ucost = " ";
    }else{
    $ucost = mysql_result($poq,0,'unit_cost');
    }
    if(!mysql_result($dq2,0,'delivered')){
    $delivered = " ";
    }else{
    $delivered = mysql_result($dq2,0,'delivered');
    }
    if(!mysql_result($dq2,0,'ddate')){
    $ddate = " ";
    }else{
    $ddate = mysql_result($dq2,0,'ddate');
    $ddate = strftime("%Y-%m-%d", $ddate);
    }
    if(!mysql_result($poq,0,'currency')){
    $curr = " ";
    }else{
    $curr = mysql_result($poq,0,'currency');
    }
    if($curr == "人民币"){
    $curr = "RMB";
    }elseif($curr == "港币"){
    $curr = "HK$";
    }else{
    $curr = "USD";
    }
    if($i%2==0){
    $line_color = "#FFF";
    }else{
    $line_color = "#FF9900";
    }
                
    printf("
      <tr align=\"center\">
    <td nowrap><b>%s</b></td>
    <td nowrap><b>%s</b></td>
    <td nowrap><b>%s</b></td>
    <td nowrap><b>%s</b></td>
    <td nowrap><b>%s %s</b></td>
    <td nowrap><b>%s</b></td>
    <td nowrap><b>%s</b></td>
      </tr>",$c_name,$po_no,$quantity,$description,$curr, $ucost,$dnote_no,$delivered,$curr,$$ucost*$delivered,$ddate);
      }
    ?> 
    </table>
    </body>
    </html>
    这样就可以了。“nowrap”是让它不自动换行。