2017-11-01 11 views
0

PHPを使用してExcelファイルをMySQLにインポートしています。次のコードは、csvファイルでは機能しますが、xls/xlsxファイル形式では機能しません。 PHPでXLSファイルのxls/xlsxファイルをインポートできません

<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Import Excel to MySQL using PHP </title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    </head> 
    <body style="padding-top:50px;"> 

<div class="container"><!-- container class is used to centered the body of the browser with some decent width--> 
    <div class="row"><!-- row class is used for grid system in Bootstrap--> 
     <div class="col-md-4 col-md-offset-4"><!--col-md-4 is used to create the no of colums in the grid also use for medimum and large devices--> 
      <div class="login-panel panel panel-success"> 
       <div class="panel-heading"> 
        <h3 class="panel-title">Import here</h3> 
       </div> 
       <div class="panel-body"> 

        <form method="post" action="import.php" enctype="multipart/form-data"> 
         <fieldset> 
          <div class="form-group"> 
           <input type="file" name="file"/> 
          </div> 
         <input class="btn btn-success" type="submit" name="submit_file" value="Submit"/> 
         </fieldset> 
        </form> 

       </div> 
      </div> 
     </div> 
    </div> 
</div> 
</body> 
</html> 

Import.php

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 
/* 
Developer: Ehtesham Mehmood 
Site:  PHPCodify.com 
Script: Import Excel to MySQL using PHP and Bootstrap 
File:  import.php 
*/ 

// Including database connections 
require_once 'db_con.php'; 

if(isset($_POST["submit_file"])) 
{ 
$file = $_FILES["file"]["tmp_name"]; 

$file_open = fopen($file,"r"); 
while(($csv = fgetcsv($file_open, 1000, ",")) !== false) 
{ 
    $employee_name = $csv[0]; 
    $employee_designation = $csv[1]; 
    $employee_salary = $csv[2]; 

    $stmt = $DBcon->prepare("INSERT INTO employee(employee_name,employee_designation,employee_salary) VALUES(:employee_name,:employee_designation,:employee_salary)"); 

    $stmt->bindparam(':employee_name', $employee_name); 
    $stmt->bindparam(':employee_designation', $employee_designation); 
    $stmt->bindparam(':employee_salary', $employee_salary); 
    $stmt->execute(); 
} 
} 

echo "Imported Successfully"; 
?> 
+0

これはCSVファイルのみを使用しています。しかし、XLSファイルの場合、別の方法があります。 – kvk30

+0

私はタイトルを改善し、より多くの可視性のために質問に移動し、文法と繰り返しの性質を掃除しました – Shawn

答えて

0

異なる例。ここで私はコードサンプルを添付しています。

$xlsx = new SimpleXLSX('File_For_Import.xlsx'); 
     list($num_cols, $num_rows) = $xlsx->dimension(); 
     $f = 0; 
     foreach ($xlsx->rows() as $r) { 
      // Ignore the inital name row of excel file 
      if ($f == 0) { 
       $f++; 
       continue; 
      } 
      //sample column-names for your understanding. 
      for ($i = 0; $i < $num_cols; $i++) { 
       if ($i == 0) 
        $data['employee_name'] = $r[$i]; 
       else if($i == 1) 
        $data['employee_designation'] = $r[$i]; 
       else if ($i == 2) 
        $data['employee_salary'] = $r[$i];  
      //write code according to your DB. 
      } 
      $data['class_id'] = $this->input->post('class_id'); 
      $this->db->insert('student', $data); 
      //print_r($data); 
     } 
+0

あなたの表彰に感謝します。私はこのエラーがあります致命的なエラー:キャッチされていないエラー:クラス 'SimpleXLSX'が見つかりません –

+0

SimpleXLSXクラスをインポートしましたか? – kvk30

+0

もう一度エラーが出るCatchable fatal error:クラスSimpleXLSXのオブジェクトを文字列に変換できません –

0

Excelファイル、XLSまたはXLSXは、CSVファイルではないためである:行を解析

3つの異なるアプローチが必要な3つの異なるファイル形式があります。 importing Excel file into MySQL using PHPについて

$workbook = new COM("EasyXLS.ExcelDocument"); 
//for xls file 
$rows = $workbook->easy_ReadXLSActiveSheet_AsList("file.xls"); 
//or 
//for xlsx file 
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("file.xlsx"); 
for ($rowIndex=0; $rowIndex<$rows->size(); $rowIndex++) 
{ 
    $row = $rows->elementAt($rowIndex); 
    $employee_name = $row->elementAt(0); 
    $employee_designation = $row->elementAt(1); 
    $employee_salary = $row->elementAt(2); 

    $stmt = $DBcon->prepare("INSERT INTO employee(employee_name,employee_designation,employee_salary) VALUES(:employee_name,:employee_designation,:employee_salary)"); 

    $stmt->bindparam(':employee_name', $employee_name); 
    $stmt->bindparam(':employee_designation', $employee_designation); 
    $stmt->bindparam(':employee_salary', $employee_salary); 
    $stmt->execute(); 
} 

より: あなたは、例えばEasyXLSのように、PHPのためのExcelのライブラリを見つける必要があります。

+0

ありがとうございますがエラーです致命的なエラー:キャッチされていないエラー:クラス 'COM'が見つかりません –

+0

.NET Frameworkの拡張機能をPHPサーバーhttps://www.easyxls.com/manual/troubleshooting/classに追加する必要があります-com-not-found.html –

関連する問題