Home > Backend Development > PHP Tutorial > A simple example of php importing Excel table into MySQL database through PHPExcel

A simple example of php importing Excel table into MySQL database through PHPExcel

墨辰丷
Release: 2023-03-28 15:24:01
Original
1615 people have browsed it

The above is the entire content of this article. PHP uses PHPExcel to import an Excel table into a MySQL database. I hope it will be helpful to everyone's learning. Related recommendations:

is as follows:

<?php

define(&#39;BASE_URL&#39;, realpath(dirname(__FILE__)));
require_once BASE_URL . &#39;/PHPExcel/PHPExcel.php&#39;;//引入PHPExcel类文件

//excel文件的地址
$excel_fiel_path = &#39;./phpexcel.xls&#39;;


$PHPExcel = new PHPExcel();// 实例化PHPExcel工具类
//分析文件获取后缀判断是2007版本还是2003
$extend = pathinfo("./" . $excel_fiel_path);
$extend = strtolower($extend["extension"]); 
// 判断xlsx版本,如果是xlsx的就是2007版本的,否则就是2003
if ($extend=="xlsx") {
  $PHPReader = new PHPExcel_Reader_Excel2007();
  $PHPExcel = $PHPReader->load("./" . $excel_fiel_path);
}else{
  $PHPReader = new PHPExcel_Reader_Excel5();
  $PHPExcel = $PHPReader->load("./" . $excel_fiel_path);
}

 /* 第二种方法*/
$objWorksheet = $PHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); 
echo &#39;highestRow=&#39;.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo &#39;highestColumnIndex=&#39;.$highestColumnIndex;
echo "<br>";
$headtitle=array(); 
for ($row = 2;$row <= $highestRow;$row++) 
{
  $strs=array();
  //注意highestColumnIndex的列数索引从0开始
  for ($col = 0;$col < $highestColumnIndex;$col++)
  {
    $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();

  } 
  //todo
  //连接mysql ,一条条写入
 
}
Copy after login

The above is the entire content of this article , I hope it will be helpful to everyone’s study.


Related recommendations:

php Implement curl upload, download, https login

PHP implementation of APP WeChat payment case analysis

phpHow to use magic functions and magic constants

The above is the detailed content of A simple example of php importing Excel table into MySQL database through PHPExcel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template