Excel返回0

时间:2014-04-26 16:28:51

标签: php sql-server excel sqlsrv

尝试从我的sql server生成excel文件,但它在excel文件中返回0。不知道为什么。

在excel文档中返回0的SQLSRV版本。

 $result=sqlsrv_query($conn,$sql) or die("Couldn't execute query:<br>" . sqlsrv_error(). "<br>" . sqlsrv_errno()); 


$file_ending = "xls";
$reals=array();
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character

//start of printing column names as names of MySQL fields

/*for ($i = 0; $i < sqlsrv_num_fields($result); $i++) {
    $type = sqlsrv_field_metadata($result,$i);
    echo sqlsrv_field_metadata($result,$i) . "\t";
    if ($type == "real")
    {
        $reals[] = $i;
    }
}
*/
$i=0;
foreach( sqlsrv_field_metadata( $result ) as $fieldMetadata ) {    
       echo $fieldMetadata["Name"]+"\t";
       if($fieldMetadata["Type"]=="real")//$fieldMetadata["Type"]=== SQL_REAL
       {
           $reals[] = $i;
       }
       $i++;
}


print("\n");    
//end of printing column names  
//start while loop to get data
while($row = sqlsrv_fetch($result))
{
    $schema_insert = "";
    for($j=0; $j<sqlsrv_num_fields($result);$j++)
    {
        if(!isset($row[$j]))
            $schema_insert .= "NULL".$sep;
        elseif ($row[$j] != ""){
            if (in_array($j, $reals)){
                $schema_insert .= str_replace(".",",","$row[$j]").$sep;
            } else {
                $schema_insert .= "$row[$j]".$sep;
            }
        }
        else
            $schema_insert .= "".$sep;
    }
    $schema_insert = str_replace($sep."$", "", $schema_insert);
    $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    $schema_insert .= "\t";
    print(trim($schema_insert));
    print "\n";

} 

MYSQL版本,完美运行:

$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";
$reals=array();
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
    $type = mysql_field_type($result,$i);
    echo mysql_field_name($result,$i) . "\t";
    if ($type == "real")
    {
        $reals[] = $i;
    }
}



print("\n");    
//end of printing column names  
//start while loop to get data
while($row = mysql_fetch_row($result))
{
    $schema_insert = "";
    for($j=0; $j<mysql_num_fields($result);$j++)
    {
        if(!isset($row[$j]))
            $schema_insert .= "NULL".$sep;
        elseif ($row[$j] != ""){
            if (in_array($j, $reals)){
                $schema_insert .= str_replace(".",",","$row[$j]").$sep;
            } else {
                $schema_insert .= "$row[$j]".$sep;
            }
        }
        else
            $schema_insert .= "".$sep;
    }
    $schema_insert = str_replace($sep."$", "", $schema_insert);
    $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    $schema_insert .= "\t";
    print(trim($schema_insert));
    print "\n";
} 

1 个答案:

答案 0 :(得分:0)

sqlsrv_fetch()使下一个结果集可供阅读,因此,如果您想要将数据读取为$row[$j],则应使用sqlsrv_get_field($result, $j)

while(sqlsrv_fetch($result))
{
   $schema_insert = "";
   for($j = 0; $j < sqlsrv_num_fields($result); $j++)
   {
      if (sqlsrv_get_field($result, $j) != "") {
         if (in_array($j, $reals)) {
            $schema_insert .= str_replace(".",",", sqlsrv_get_field($result, $j)) . $sep;
         } else {
            $schema_insert .= sqlsrv_get_field($result, $j) . $sep;
         }
      }
      else
         $schema_insert .= "" . $sep;
   }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
}

请注意,我已将if(isset($row[$j]))检查删除,因为它是不必要的。

使用sqlsrv_fetch_array()还有一种方法(而且我认为不太复杂)。如果不将字段值作为特定类型返回(例如字符串),则可以使用该函数将结果集作为数组返回:

while($row = sqlsrv_fetch_array($result))
{
   $schema_insert = "";
   for($j = 0; $j < sqlsrv_num_fields($result); $j++)
   {
      if ($row[$j] != "") {
         if (in_array($j, $reals)) {
            $schema_insert .= str_replace(".",",", $row[$j]) . $sep;
         } else {
            $schema_insert .= $row[$j] . $sep;
         }
      }
      else
         $schema_insert .= "" . $sep;
   }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
}

还要添加一件事,我还删除了$row[$j]左右的双引号但是我不确定你可以再添加它们。

您应该参考更多文档。它有非常明确的例子和非常好的解释。