Using PHPExcel to parse Excel tables

Keywords: PHP Excel JSON github

Install class library

Download PHPExcel class library from GitHub
Address: https://github.com/PHPOffice/PHPExcel

After unzipping, move the Classes folder to the extend directory of ThinkPHP and rename it phpexcel

Add references where needed in the project

import('phpexcel.PHPExcel', EXTEND_PATH);

code implementation

<?php
namespace app\index\controller;

use think\Controller;

class Excel extends Controller
{

    // Test http://127.0.0.1:8083/index/excel/index
    public function index()
    {
        $path = "G:/student.xlsx";
        $data = self::readExcel($path);
        if (empty($data)) {
            $json = json_encode([
                'errcode'   => '20001',
                'errmsg'    => 'Data not resolved',
                'data'      => []
            ], JSON_UNESCAPED_UNICODE);
        } else {
            $json = json_encode([
                'errcode'   => '10000',
                'errmsg'    => 'Read data successfully',
                'data'      => $data
            ], JSON_UNESCAPED_UNICODE);
        }
        return $json;
    }

    //PHPExcel parsing Excel table
    protected function readExcel($filePath)
    {
        // Introducing class library
        import('phpexcel.PHPExcel', EXTEND_PATH);

        // Instanced object
        if (strstr($filePath, '.xlsx')) {
            // The corresponding file type is. xlsx
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        } elseif (strstr($filePath, '.xls')) {
            // The corresponding file type is. xls
            $PHPReader = new \PHPExcel_Reader_Excel5();
        } else {
            // Unrecognized file type
            return false;
        }

        // Load Excel file
        $PHPExcel = $PHPReader->load($filePath);
        // Get sheet1
        $sheet = $PHPExcel->getActiveSheet(0);
        // The maximum number of rows in the current sheet
        $highestRow = $sheet->getHighestRow();
        // Get Excel data
        $arr = $sheet->toArray();

        // analysis
        $data = [];
        $length = count($arr);
        for ($i = 1; $i < $length; $i++) {
            // Why do i start at 1? Because i=0 is the column title!
            $data[$i-1] = [
                'stuNo' => $arr[$i][0],
                'name' => $arr[$i][1],
                'class' => $arr[$i][2]
            ];
        }

        // Return data
        return empty($data) ? [] : $data;
    }
}

View run results

The content in Excel is

Visit in the browser and view the results (json data is obtained here)

After formatting the json data (via the web front-end helper plug-in)

You can see the parsed data

Add: Chrome installation [Web front end assistant]

Click to download web front end assistant

Open Chrome extensions

Drag the downloaded. crx plug-in into the browser and install it

Link to this article: https://www.cnblogs.com/connect/p/php-read-excel.html

Posted by tlawless on Fri, 06 Dec 2019 12:37:45 -0800