打印的表头已经处理好的
导出的时候不能按格式顺利的输出
$sql = "select row_number() over (order by id desc ) as rownum
,[companyname],isnull(dbo.get_IdToValue(unitprop),'') as [unitprop], isnull(dbo.get_IdToValue(unitspec),'') as [unitspec], isnull(dbo.get_IdToValue(fundspychannels),'') [fundspychannels],[officepostnum] ,[staffnum],[retirednum],[availcar],[scrapcar], [quotacar],[dbnum], isnull(dbo.get_IdToValue(brand),'') [brand], isnull(dbo.get_IdToValue(cartype),'') as [cartype],isnull(dbo.get_IdToValue(output),'') as [output],isnull(dbo.get_IdToValue(seats),'') as [seats],[oneprice],[num],[price],[appurchrate],[totalprice],isnull(dbo.get_IdToValue(afundfrom),'') as [afundfrom]
,isnull(dbo.get_IdToValue(abuytype),'') as [abuytype],[acaruse],[areason],[auditopn],[auditorname],CONVERT(varchar(100), auditime, 23) as [auditime]
, (case when auditflag = 0 then '未审核' when auditflag =1 then '不同意' when auditflag=2 then '同意' end ) as auditflag
,[approveopn],[approver]
,(case when approveflag = 0 then '未审批' when approveflag =1 then '不同意' when approveflag=2 then '同意' end ) as approveflag,CONVERT(varchar(100), approvetime, 23) as [approvetime],[acfzrname],[managername],[magtel]
,(select manname from t_account where acid=V_applypurchasecar.acid) as operator,[remark],CONVERT(varchar(100), applydate, 23) as [applydate],CONVERT(varchar(100), created, 23) as created from V_applypurchasecar";
if ($where != "") {
$sql = $sql . " where " . $where;
}
$result = $db->query($sql);
//$title = "序号\t申购单位名称\t单位规格\t单位性质\t经费供应渠道\t厅级职数\t人员编制\t离退休干部数\t编制车数\t现有车辆数\t应报废数\t定编文号\t申购车辆类型\t种类\t排量\t座位数\t单价\t数量\t金额(万元)\t购置费(万元)\t经费合计(万元)\t购车资金来源\t购置方式\t用途\t申购理由\t主管部门意见\t负责人姓名\t审核时间\t审核结果\t审批意见\t审批人\t审批结果\t审批时间\t申购单位负责人\t经办人\t联系电话\t录入人员\t备注\t申报时间\t录入时间";
$title = "<table id='tb' name='tb' class='ptbiankuang' width='100%' border='0' cellspacing='0' cellpadding='0' style='margin-top:-1px;'>
<tr style='height:36pt;'>
</tr>
<tr style='height:36pt;'>
<th rowspan=2>序号</th>
<th rowspan=2>单位</th>
<th colspan=6>机关有关情况</th>
<th colspan=3>现有车辆情况</th>
<th colspan=7>申购车辆情况</th>
<th rowspan=2>备注</th>
</tr>
<tr style='height:36pt;'>
<th>规格</th>
<th>性质</th>
<th>资金供应渠道</th>
<th>领导职数</th>
<th>人员编制</th>
<th>离退休干部数</th>
<th>编制车数</th>
<th>实有车数</th>
<th>应报废车辆数</th>
<th>厂牌型号</th>
<th>种类</th>
<th>排量或座位数</th>
<th>数量</th>
<th>单价</th>
<th>经费合计</th>
<th>经费来源</th>
</tr></table>";
$fn = saveexcelfile($title, $result);
echo $fn;
freedbquerystmt($result);
exit;
之前的简单的表头,可以按格式导出,现在把title换了之后,不太好使
function saveexcelfile($titles,$stmt)
{
$titles=iconv("UTF-8", "GBK", $titles);
$str = '';
$numFields = sqlsrv_num_fields( $stmt );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
{
/* Iterate through the fields of each row. */
$rowstr="";
for($i = 0; $i < $numFields; $i++)
{
$cols=$row[$i];
if($rowstr=="")
{
// $rowstr=$cols;
$rowstr=iconv("UTF-8", "GBK", $cols);
}else{
//$rowstr=$rowstr."\t".$cols;
$rowstr=$rowstr."\t".iconv("UTF-8", "GBK", $cols);
}
}
$str.=$rowstr.PHP_EOL;
}
$str=$titles. PHP_EOL .$str;
$fn=date("YmdHis").rand(100,999).".xls";
$path="download/".$fn;
ob_end_clean();
header("Content-type: text/html; charset=utf-8");
if($f = file_put_contents($path, $str,FILE_APPEND)){// 这个函数支持版本(PHP 5)
return $path;
}else{
return "error";
}
}
表格导出函数是之前封装好的。
title该如何修改呢?