English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Exemple simple d'importation d'un tableau Excel dans une base de données MySQL via PHPExcel

如下所示:

<?php
define('BASE_URL', realpath(dirname(__FILE__)));
require_once BASE_URL . '';/PHPExcel/PHPExcel.php';//引入PHPExcel类文件
//excel文件的地址
$excel_fiel_path = '.';/phpexcel.xls';
$PHPExcel = new PHPExcel();// 实例化PHPExcel工具类
//分析文件获取后缀判断是2007版本还是2003
$extend = pathinfo(".");/. $excel_fiel_path);
$extend = strtolower($extend["extension"]); 
// 判断xlsx版本,如果是xlsx的就是2007版本的,否则就是2003
if ($extend == "xlsx") {
  new PHPExcel_Reader_Excel = $PHPReader2007();
  $PHPExcel = $PHPReader->load("./. $excel_fiel_path);
else {
  new PHPExcel_Reader_Excel = $PHPReader5();
  $PHPExcel = $PHPReader->load("./. $excel_fiel_path);
}
 /* Deuxième méthode*/
$objWorksheet = $PHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); 
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//Nombre total de colonnes
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br>";
$headtitle = array(); 
for ($row = 2; $row <= $highestRow; $row++) 
{
  $strs = array();
  //Attention, l'index de colonne de highestColumnIndex commence à 0
  for ($col = 0; $col < $highestColumnIndex; $col++)
  {
    $strs[$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  } 
  //à faire
  //Connecter à MySQL, écrire ligne par ligne
}

Ceci est la totalité de l'exemple simple que l'éditeur vous a apporté sur l'importation d'un tableau Excel dans une base de données MySQL via PHPExcel, j'espère que vous le soutenirez fortement le tutoriel~

Vous pourriez aussi aimer