2012-08-09 52 views
1

我需要到數據庫中創建一個CSV導入的數據,但我有3個表Php csv導入可能的方式?

所以如何分離,什麼是可能的方式像 1創建CSV)如果涉及一個混亂

我的CSV數據數據alredy表1因此檢查所需的數據插入或不 或更新 2)我也需要,如果數據不同時插入的結果也DISPLY插入數據未插入等等

+0

試試這個:http://stackoverflow.com/a/11956237/1598548使用mysql做這個工作似乎要快一點。 – Craigo 2012-08-14 16:06:00

回答

1

我實現這個代碼它是測試代碼。我覺得這是非常使用全

你必須遵循一些規則: -

貴國的CSV文件根據數據庫表名(例如:數據庫表名用戶,那麼CSV應該users.csv)

2.您的CSV文件的第一行應該是數據庫表字段名(例如:ID,姓名等)後,然後開始你的數據錄入

3.you可以從數據源類: - http://code.google.com/p/php-csv-parser/,因爲我有以下要求代碼:require_once'CSV/DataSource.php';

<?php 
ini_set('memory_limit','512M'); 
$dbhost = "localhost"; 
$dbname = "excel_import"; 
$dbuser = "root"; 
$dbpass = ""; 

$conn=mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); 
mysql_select_db($dbname) or die("Unable to select database because: " . mysql_error()); 


require_once 'CSV/DataSource.php'; 

$filename = "Book1.csv"; 
//$filename = $_POST['filename']; 
$ext = explode(".",$filename); 

$path = "uploads/".$filename; 

$dbtable = $ext[0]; 


import_csv($dbtable, $path); 


function import_csv($dbtable, $csv_file_name_with_path) 
{ 
    $csv = new File_CSV_DataSource; 
    $csv->load($csv_file_name_with_path); 

    $csvData = $csv->connect(); 

    $res=''; 
    foreach($csvData as $key) 
    { 
     $myKey =''; 
     $myVal=''; 
     foreach($key as $k=>$v) 
     { 


      $myKey .=$k.','; 
      $myVal .="'".$v."',"; 

     } 
     $myKey = substr($myKey, 0, -1); 
     $myVal = substr($myVal, 0, -1); 




     $query="insert into ".$dbtable." ($myKey)values($myVal)"; 
     $res= mysql_query($query); 

    } 

    if($res ==1) 
    { 

    echo "record successfully Import."; 
    }else{ 

    echo "record not successfully Import."; 
    } 
} 

?> 
0
$row = 1; 

if (($handle = fopen("path to your csv file/data.csv", "r")) !== FALSE) 
{ 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    { 
     $row++; 
     print_r($data); 
    } 

    fclose($handle); 
} 

基於你可以做進一步的步驟打印數據。

+0

我在yii中需要這個csv。 – user1542274 2012-08-13 05:31:25

+0

我在這個應用程序中開發一款葡萄酒,釀酒廠,復古應用程序,我使用yii來提升csv真正的葡萄酒,葡萄酒,釀酒廠,因此需要您對葡萄酒csv的建議,需要酒莊編號或名稱哪一個更好,如果輸入錯誤的酒莊編號或名稱,我採取的行動請給我建議 – user1542274 2012-08-13 05:33:12

2

通過使用phpexcel您可以輕鬆地在Yii框架從http://phpexcel.codeplex.com提取物導入Excel/CSV

1.download最新phpexcel到擴展文件夾

2.創建動作控制器安給予適當的基本路徑和phpexcelpath

public function actionImport() 
      { 
        $message = ''; 
        if (!empty($_POST)) 
        { 
        $file = CUploadedFile::getInstanceByName('import'); 
        $spec = Yii::app()->basePath.'/data/imports/'.$file->name; 
        $file->saveAs($spec); 



        spl_autoload_unregister(array('YiiBase','autoload')); 




        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel.vender'); 

         include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php'); 

        spl_autoload_register(array('YiiBase', 'autoload')); 


        try { 



         $inputFileType = PHPExcel_IOFactory::identify($spec); 
         $objReader = PHPExcel_IOFactory::createReader($inputFileType); 

         if ($inputFileType != 'xls') 
          { 
          $objReader->setReadDataOnly(true); 
          } 
         $objPHPExcel = $objReader->load($spec); 
         $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 
         $highestRow = $objWorksheet->getHighestRow(); 
         for ($row = 1;$row < $highestRow+1; $row++) 
          { 
          $model= new ExamScores; // Yii AR model 
          $model->id = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue(); 
          $model->student_id = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue(); 
           $model->exam_id = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue(); 
           $model->marks = $objWorksheet->getCellByColumnAndRow(4, $row)->getValue(); 
          $model->save(false); 
          $model->detachBehaviors(); // PHP < 5.3 memory management 
          unset($model); 
          Yii::app()->user->setFlash('sucess','Mark saved Successfully'); 
          } 


         } 
        catch (Exception $e) 
         { 
         $message = 'There was a problem handling your file. Technical details: '.$e->getMessage(); 

         } 
        if (! empty($message)) 
         { 
         Yii::app()->user->setFlash('error',$message); 

         } 
        }  
        $this->render('import'); 
      } 

3.in您的視圖文件

<!--<div class="formCon"> 
<div class="formConInner">--> 

<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td width="247" valign="top"> 

    <?php $this->renderPartial('left_side');?> 

    </td> 
    <td valign="top"> 




<h1>IMPORT EXCEL SHEET</h1> 

<?php if(Yii::app()->user->hasFlash('sucess')): ?> 

<div class="flash-success"> 
    <?php echo Yii::app()->user->getFlash('sucess'); ?> 
</div> 

<?php endif; ?> 

<div style="background-color: #E6E4E4;"> 
<table width="100%" cellspacing="2" cellpadding="2"> 

<tr> 
<?php echo CHtml::beginForm('', 'post', array('enctype'=>'multipart/form-data')); ?> 
<tr><td>&nbsp;</td></tr> 
<td><?php echo CHtml::label('File to Import', 'fti'); ?> 
<?php echo CHtml::fileField('import', '', array('id'=>'fti')); ?></td> 
</tr> 


<tr><td>&nbsp;</td></tr> 
<tr><td>&nbsp;</td></tr> 
<tr><td>&nbsp;</td></tr> 


<tr> 

<td><?php echo CHtml::submitButton('Submit'); ?> </td> 


</tr> 

<?php echo CHtml::endForm(); ?> 
</tr> 
</table> 


</div> 
    </td> 

</table> 


<!--</div> 
</div>--> 
+0

非常好的做法..保存我的一天。 – 2014-05-26 09:51:35