简体   繁体   English

在csv文件中获取列名称

[英]Get Column name in csv file

Working on creating a CSV file and wondering how I can get the column name in the first row. 正在创建一个CSV文件,并想知道如何在第一行中获取列名。 Right now I think it's trying to echo the Column name but getting 0'nulls. 现在我认为它试图回应列名称,但得到0'nulls。 I am also wondering if it's possible to put each of the values in each cell, I can do it manually in excel but that is troublesome. 我也想知道是否可以将每个值放在每个单元格中,我可以在excel中手动完成,但这很麻烦。

在此输入图像描述

This is the code: 这是代码:

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

$file_ending = "csv";
$reals=array();
//header info for browser
header("Content-Type: application/csv");    
header("Content-Disposition: attachment; filename=test.csv");  
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

$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_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 = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
}

Would this work ? 这会有用吗? Since sqlsrv_fetch_array return an associative array with the name of the field as key we can take that to input as the first row. 由于sqlsrv_fetch_array返回一个关联数组,并将字段名称作为键,我们可以将其作为第一行输入。

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

$file_ending = "csv";
$reals=array();
//header info for browser
header("Content-Type: application/csv");    
header("Content-Disposition: attachment; filename=test.csv");  
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

$firstRow = true;


//start while loop to get data
while($row = sqlsrv_fetch_array($result))
{
  if($firstRow)
  {
    $names = array_keys($row);
    $namesToPrint = '';

    foreach($names as $idx => $name)
    {
        if($idx % 2 != 0)
        {
            $namesToPrint .= $name.',';
        }
    }

    $namesToPrint = substr($namesToPrint, 0, -1);

    print $namesToPrint."\n";

    $firstRow = false;
  }

   $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 = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM