簡體   English   中英

PHP csv 文件以列和行的形式導出

[英]PHP csv file exported with results in columns and rows

我正在嘗試使用此 PHP 腳本讀取特定代碼,該代碼根據其位置中的數字具有不同的結果。
所以代碼從 txt 文件中的 position 709 開始,看起來像: FRNARUFY16UEAE5QC
這一切都很好,我得到了我希望我的問題出現在導出的 CSV 文件中的結果。
我設法用 \r\n 將每個結果顯示在新行上,但我不知道如何讓每個結果顯示在新列上。
理想情況下,我希望做出這樣的最終結果在此處輸入圖像描述

我嘗試了一些我發現的解決方案,但沒有一個有效,我懷疑這是我的代碼的錯,而不是解決方案本身的錯,所以任何幫助都將不勝感激。

<?php

$files = glob('C:/Users/X/Desktop/php/in/file*.{txt}', GLOB_BRACE);

$vinsToUpdate = array();

if ( $files !== false )
{
    $filecount = count( $files );

    foreach($files as $file)
    {
            $handle = fopen($file, "r");
            if ($handle)
            {
                while (($line = fgets($handle)) !== false)
                {
                  if(strlen($line) > 1)
                  {
                      $csv = str_getcsv($line);
                      if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'Y')
                      {
                            echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1211 1211 uk \r\n";

                      }
                      if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'L')
                                {
                                  echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1159 1159 uk \r\n";
                                }
                    if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE16')
                                          {
                                            echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 40kwh Electric 8703801000 1515 1515 UK \r\n";
                                          }
                        if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE18')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 60kwh Electric 8703801000 1708 1708 UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'Y')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000   1466    1466    UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000 1511 1511 UK \r\n";
                                            }
                        if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
                                            {
                                                echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3   Petrol 8703221000 1570 1570 UK \r\n";
                                            }
                  }
                }
                    fclose($handle);
            } else {
                // error opening the file.
            }
    }
}

// Excel file name for download
$fileName = "Spec" . ".csv";

// Headers for download
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Type: application/vnd.ms-excel");


exit;




?>

你快到了,

CSV 文件是超級基本的,它實際上代表逗號,分隔,值。

因此,對於 go 到下一列,您只需添加一個逗號以分隔成列。

echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16, 1, Petrol, 8703211000, 1211, 1211, uk";

ETC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM