Home > php教程 > php手册 > body text

PHPEXCEL导入excel表格生成数组

WBOY
Release: 2016-05-25 16:40:22
Original
2492 people have browsed it

本方法使用PHPEXCEL插件读取excel文件转化为数组了,后期还有没有完成的我们可以把转换成数组之后再保存到mysql数据库这个就非常的方便了,代码如下:

<?php 
/** 
 * @desc PHPEXCEL导入 
 * return array(); 
 */ 
function importExcel($file) 
{ 
    require_once &#39;PHPExcel.php&#39;; 
    require_once &#39;PHPExcel/IOFactory.php&#39;; 
    require_once &#39;PHPExcel/Reader/Excel5.php&#39;; 
    $objReader = PHPExcel_IOFactory::createReader(&#39;Excel5&#39;);//use excel2007 for 2007 format 
    $objPHPExcel = $objReader->load($file); 
    $sheet = $objPHPExcel->getSheet(0); 
    $highestRow = $sheet->getHighestRow(); // 取得总行数 
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数 
    $objWorksheet = $objPHPExcel->getActiveSheet(); 
 
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
    $excelData = array(); 
    for ($row = 1; $row <= $highestRow; $row++) { 
        for ($col = 0; $col < $highestColumnIndex; $col++) { 
            $excelData[$row][] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); 
        } 
    } 
    return $excelData; 
} 
//用法: 
importExcel(&#39;test.xsl&#39;);
Copy after login


永久地址:

转载随意~请带上教程地址吧^^

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template