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

Simple Example of Importing Excel Tables into MySQL Database Using PHPExcel

As shown below:

<?php
define('BASE_URL', realpath(dirname(__FILE__)));
require_once BASE_URL ./PHPExcel/PHPExcel.php';//Include the PHPExcel class file
//address of the excel file
$excel_fiel_path = './phpexcel.xls';
$PHPExcel = new PHPExcel();// Instance the PHPExcel utility class
//Analyze the file to obtain the suffix and determine whether it is2007version or2003
$extend = pathinfo("./. $excel_fiel_path);
$extend = strtolower($extend["extension"]); 
// Determine the xlsx version, if it is xlsx, it is2007version, otherwise it is2003
if ($extend=="xlsx") {
  $PHPReader = new PHPExcel_Reader_Excel2007();
  $PHPExcel = $PHPReader->load("./. $excel_fiel_path);
}
  $PHPReader = new PHPExcel_Reader_Excel5();
  $PHPExcel = $PHPReader->load("./. $excel_fiel_path);
}
 /* Second method*/
$objWorksheet = $PHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); 
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//Total number of columns
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br>";
$headtitle = array(); 
for ($row = 2; $row <= $highestRow; $row++) 
{
  $strs = array();
  //Note that the column index of highestColumnIndex starts from 0
  for ($col = 0; $col < $highestColumnIndex; $col++)
  {
    $strs[$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  } 
  //todo
  //Connect to mysql and write one by one
}

This is the full content of the simple example brought to you by the editor on how to import Excel tables into MySQL database using PHPExcel, I hope everyone will support and cheer for the tutorial~

You May Also Like