2016-02-25 36 views
1

當我讀取CSV(使用PHPExcel,fgetcsv())時,我想在我的數據庫中導入行被切斷。使用PHP讀取Excel csv行被切斷

1 fields in line 1: 
    ProCatId;CatName;CatDescription;CatNameisactive;CatNameiswebactive;ProSubCatId;SubCatName;SubCatDesc;SubcatWebDiscription2;SubcatWebDiscription3;SubcatCatName;SubcatSequencenr;SubCatNameisactive;SubCatNameiswebactive;DisplayMode;ProductId;ProductName;ProductDescription;ProductCatName;ProductSubCatName;Express;NextDayDelivery;Sequencenr;Drukvorm;Afwerking;BreedteDrukwerk;HoogteDrukwerk;Papier;PrinterName;PrinterProductCode;WebDescription;WebDescription2;WebDescription3;DeliveryInfo;NextDayPrice;NextDayTransferPrice;NextDayCostPrice;Meta-Keywords;Meta-Description;URL-Trefwoorden;Titel;Filepath_icons;Format;MaterialType;NoOfPages;PrintFront;PrintBack;BleedTop;BleedBottom;BleedLeft;BleedRight;MINH;MAXH;MINW;MAXW;De 
1 fields in line 2: 
    liveryScheduleId;DeliveryScheduleName;CentralProductDetailId;ClaimsId;ClaimsName;DruktechniekenId;DruktechniekenName;Verzameld op;Vast;Variabel;vorm;"prijs 
1 fields in line 3: 
    drukken 

這些領域應該是所有第1行中 當我使用PHP函數file_get_contents()函數的內容看起來有效。 在Excel中加載文件也沒有問題。

我用下面的代碼(,分隔):

<?php 
if (($handle = fopen($inputFileName, "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $num = count($data); 
     echo "<p> $num fields in line $row: <br /></p>\n"; 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      echo $data[$c] . "<br />\n"; 
     } 
    } 
    fclose($handle); 
} 
?> 
+0

我嘗試盲評論...取代「;」與「,」 –

回答

0

讓我們看到了documentation說什麼:

array fgetcsv ( 
     resource $handle 
     [, int $length = 0 
     [, string $delimiter = "," 
     [, string $enclosure = '"' 
     [, string $escape = "\" ]]]]) 

你傳遞一個$handle,好。

您是說「切斷在1000個字符的行」 - 與您的數據,這可能會成爲一個問題,可以考慮更高的限制

而你說:「字段由,分離」(8000?) 。它看起來好像他們是實際上分開;。也許這也是問題的一部分?

(另外,fgetcsv不是二進制安全的,所以如果你的數據不是純ASCII,see this question。)

+0

當我使用下面的而不是fgetcsv()。 「while(($ raw_data = fgets($ handle,100000))!== FALSE){」,前兩行是一個。但它仍然切斷了第三個。 – Dyam