【问题标题】:Excel to MySQL using PHP imports into a single columnExcel 到 MySQL 使用 PHP 导入单个列
【发布时间】:2015-02-07 15:33:11
【问题描述】:

我正在尝试使用 php 将 excel 文件上传到我的 mysql 数据库中,但我有两个问题:第一个问题,我收到 .csv 文件每一行的“通知:未定义偏移量:”警告,第二个问题是它将 .csv 文件的所有三列都导入到一个 db 中。

我的代码如下:

<?php 
if(isset($_POST["Import"]))
{

    $conexion=mysql_connect("localhost","root","") or die("Problemas en la conexion");
    mysql_select_db("kontor",$conexion) or die("Problemas en la seleccion de la base de datos");

    echo $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        $count = 0; 
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
            $count++; 
            if($count>1){  
            mysql_query("INSERT into stock (nombre,prneto,descr) values ('$emapData[0]','$emapData[1]','$emapData[2]')", $conexion) 
or die("Problemas en el select".mysql_error());

            }       
        }
        fclose($file);
        echo 'Archivo importado';
        //header('Location: index.php');
    }
    else
        echo 'Formato de archivo incorrecto';
}
?>

【问题讨论】:

    标签: php mysql excel csv


    【解决方案1】:

    看一下 csv 文件...用什么分隔符和外壳?

    Excel 经常使用 ;和 " 用于分隔符和外壳。

    大多数情况下,其中一个可以完成这项工作:

    while (($emapData = fgetcsv($file, 10000, ";", '"')) !== FALSE)
    
    // or
    
    while (($emapData = fgetcsv($file, 10000, ",", '"')) !== FALSE) 
    

    【讨论】:

      【解决方案2】:

      Excel 通常不喜欢一致地引用 CSV 列值;它仅在认为有必要时才这样做(至少对于某些版本的 excel)。出于这个原因,我通常不建议使用 Excel 生成的 CSV,而是选择使用 PHPExcel 以本机格式读取 excel 文件

      // this is the older version of excel for .xls
      // if the file is xlsx you would use PHPExcel_Reader_Excel2007()
      $objReader = new PHPExcel_Reader_Excel5(); 
      
      $objPHPExcel = $objReader->load($_FILES["file"]["tmp_name"]);
      $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
      
      // you should also be using PDO or mysqli for your db interactions
      // NOT mysql - this example will use pdo
      
      $db = new PDO($dsn, $user, $pass, array(
         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
      ));
      
      
      
      try {
        // presumebaly you want this operation to be atomic
        // ie. if any one row fails don't insert any of the rows
        // so we will use a transaction
        $db->beginTransaction();
      
        // prepare the query
        $stmt = $db->prepare('INSERT into stock (nombre,prneto,descr) values (?,?,?)');
      
       foreach ($sheetData as $row) {
          // execute the insert for a row of csv
          $stmt->execute(array_values($row));  
       }
      
       // attempt to commit the transaction
       $db->commit();
      
      
      } catch (Exception $e) {
         // we had an error somewhere, roll back the transaction
         $db->rollBack();
      
         // retrhow the error
         throw $e;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2012-08-07
        • 2017-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多