PHP读取xlsx Excel 2007文件

发布于 2024-08-26 23:05:02 字数 95 浏览 11 评论 0原文

我正在使用 oleread 读取上传的 xls 文件。但我无法读取以 excel-2007 格式保存的 xlsx 文件。有人可以帮助我如何在 PHP 中读取 xlsx 文件吗?

I am using oleread to read uploaded xls file. But I am not able to read xlsx files saved in excel-2007 format. can someone help me on how to read xlsx file in PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

誰ツ都不明白 2024-09-02 23:05:02
<?php
require_once 'SimpleXLSX.php';

if ( $xlsx = SimpleXLSX::parse('pricelist.xlsx') ) {
  print_r( $xlsx->rows() );
} else {
  echo SimpleXLSX::parseError();
}
?>

SimpleXLSX

<?php
require_once 'SimpleXLSX.php';

if ( $xlsx = SimpleXLSX::parse('pricelist.xlsx') ) {
  print_r( $xlsx->rows() );
} else {
  echo SimpleXLSX::parseError();
}
?>

SimpleXLSX

尐籹人 2024-09-02 23:05:02

PHPExcel (请参阅 GitHub 存储库)可能是您的最佳选择。它非常流畅且易于使用,我对新的 XLSX 格式没有任何问题。

更新:

PHPExcel - DEAD

PHPExcel 最新版本 1.8.1 于 2015 年发布。该项目于 2017 年正式弃用,并于 2019 年永久存档。

该项目尚未发布已维护多年,不得再使用。所有用户都必须迁移到其直接后继者 PhpSpreadsheet 或其他替代方案。

PHPExcel (see GitHub repository) is probably your best option. It's very slick and easy to use, I've had no problem with the new XLSX format.

Update :

PHPExcel - DEAD

PHPExcel last version, 1.8.1, was released in 2015. The project was officially deprecated in 2017 and permanently archived in 2019.

The project has not be maintained for years and must not be used anymore. All users must migrate to its direct successor PhpSpreadsheet, or another alternative.

绅刃 2024-09-02 23:05:02

XLSXReader(可在 GitHub 上获取)基于 SimpleXLSX(参见 Sergey Shuchkin 的回答)。

使用 XLSXReader::toUnixTimeStamp 从 xlsx 转换日期。

sample.xlsx

sample.xlsx

sample.php

require (__DIR__ . '/../xlsxreader/XLSXReader.php');

// entire workbook
$xlsx = new XLSXReader('sample.xlsx');
$sheetNames = $xlsx->getSheetNames();

// loop through worksheets
foreach ($sheetNames as $sheetName) {
    $sheet = $xlsx->getSheet($sheetName);

    // worksheet header
    echo('<h3>' . htmlentities($sheetName) . '</h3><table>' . PHP_EOL);
    $xlsx_data = $sheet->getData();
    $header_row_xlsx = array_shift($xlsx_data);

    // header row
    echo('<tr>' . PHP_EOL);
    echo("\t<th>row</th>" . PHP_EOL);
    for ($i = 0; $i < count($header_row_xlsx); $i++) {
        $xlsx_field_name = '' . $header_row_xlsx[$i];
        echo("\t<th>" . htmlentities("$xlsx_field_name") . '</th>' . PHP_EOL);
    }
    echo('</tr>' . PHP_EOL);

    // loop through data rows
    $row_number = 1;
    foreach ($xlsx_data as $row_xlsx) {

        // data row
        echo('<tr>' . PHP_EOL);
        echo("\t<td>" . htmlentities("$row_number") . '</td>' . PHP_EOL);
        for ($i = 0; $i < count($row_xlsx); $i++) {
            $xlsx_field_name = '' . ($i < count($header_row_xlsx) ? $header_row_xlsx[$i] : '');
            if ("$xlsx_field_name" === "DoB") {

                // date value
                $xlsx_field_value = DateTimeImmutable::
                        createFromFormat('U', XLSXReader::toUnixTimeStamp($row_xlsx[$i]))
                        ->format('Y-m-d');
            } else {

                // non-date value
                $xlsx_field_value = $row_xlsx[$i];

            }
            echo("\t<td>" . htmlentities("$xlsx_field_value") . '</td>' . PHP_EOL);
        }
        echo('</tr>' . PHP_EOL);

        $row_number++;
    }
    echo("</table><!-- end of $sheetName -->" . PHP_EOL);
}

输出

<h3>Sheet1</h3><table>
<tr>
    <th>row</th>
    <th>Name</th>
    <th>Number</th>
    <th>DoB</th>
</tr>
<tr>
    <td>1</td>
    <td>David</td>
    <td>7</td>
    <td>1988-11-12</td>
</tr>
<tr>
    <td>2</td>
    <td>Walt</td>
    <td>8</td>
    <td>1972-01-19</td>
</tr>
</table><!-- end of Sheet1 -->

XLSXReader.php

<?php
/*
XLSXReader
Greg Neustaetter <[email protected]>
Artistic License

XLSXReader is a heavily modified version of:
    SimpleXLSX php class v0.4 (Artistic License)
    Created by Sergey Schuchkin from http://www.sibvision.ru - professional php developers team 2010-2011
    Downloadable from GitHub.

Key Changes include:
    Separation into two classes - one for the Workbook and one for Worksheets
    Access to sheets by name or sheet id
    Use of ZIP extension
    On-demand access of files inside zip
    On-demand access to sheet data
    No storage of XML objects or XML text
    When parsing rows, include empty rows and null cells so that data array has same number of elements for each row
    Configuration option for removing trailing empty rows
    Better handling of cells with style information but no value
    Change of class names and method names
    Removed rowsEx functionality including extraction of hyperlinks
*/

class XLSXReader {
    protected $sheets = array();
    protected $sharedstrings = array();
    protected $sheetInfo;
    protected $zip;
    public $config = array(
        'removeTrailingRows' => true
    );

    // XML schemas
    const SCHEMA_OFFICEDOCUMENT  =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument';
    const SCHEMA_RELATIONSHIP  =  'http://schemas.openxmlformats.org/package/2006/relationships';
    const SCHEMA_OFFICEDOCUMENT_RELATIONSHIP = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
    const SCHEMA_SHAREDSTRINGS =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
    const SCHEMA_WORKSHEETRELATION =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';

    public function __construct($filePath, $config = array()) {
        $this->config = array_merge($this->config, $config);
        $this->zip = new ZipArchive();
        $status = $this->zip->open($filePath);
        if($status === true) {
            $this->parse();
        } else {
            throw new Exception("Failed to open $filePath with zip error code: $status");
        }
    }

    // get a file from the zip
    protected function getEntryData($name) {
        $data = $this->zip->getFromName($name);
        if($data === false) {
            throw new Exception("File $name does not exist in the Excel file");
        } else {
            return $data;
        }
    }

    // extract the shared string and the list of sheets
    protected function parse() {
        $sheets = array();
        $relationshipsXML = simplexml_load_string($this->getEntryData("_rels/.rels"));
        foreach($relationshipsXML->Relationship as $rel) {
            if($rel['Type'] == self::SCHEMA_OFFICEDOCUMENT) {
                $workbookDir = dirname($rel['Target']) . '/';
                $workbookXML = simplexml_load_string($this->getEntryData($rel['Target']));
                foreach($workbookXML->sheets->sheet as $sheet) {                
                    $r = $sheet->attributes('r', true);
                    $sheets[(string)$r->id] = array(
                        'sheetId' => (int)$sheet['sheetId'],
                        'name' => (string)$sheet['name']
                    );

                }
                $workbookRelationsXML = simplexml_load_string($this->getEntryData($workbookDir . '_rels/' . basename($rel['Target']) . '.rels'));
                foreach($workbookRelationsXML->Relationship as $wrel) {
                    switch($wrel['Type']) {
                        case self::SCHEMA_WORKSHEETRELATION:
                            $sheets[(string)$wrel['Id']]['path'] = $workbookDir . (string)$wrel['Target'];
                            break;
                        case self::SCHEMA_SHAREDSTRINGS:
                            $sharedStringsXML = simplexml_load_string($this->getEntryData($workbookDir . (string)$wrel['Target']));
                            foreach($sharedStringsXML->si as $val) {
                                if(isset($val->t)) {
                                    $this->sharedStrings[] = (string)$val->t;
                                } elseif(isset($val->r)) {
                                    $this->sharedStrings[] = XLSXWorksheet::parseRichText($val);
                                }
                            }
                            break;
                    }
                }
            }
        }
        $this->sheetInfo = array();
        foreach($sheets as $rid=>$info) {
            $this->sheetInfo[$info['name']] = array(
                'sheetId' => $info['sheetId'],
                'rid' => $rid,
                'path' => $info['path']
            );
        }
    }

    // returns an array of sheet names, indexed by sheetId
    public function getSheetNames() {
        $res = array();
        foreach($this->sheetInfo as $sheetName=>$info) {
            $res[$info['sheetId']] = $sheetName;
        }
        return $res;
    }

    public function getSheetCount() {
        return count($this->sheetInfo);
    }

    // instantiates a sheet object (if needed) and returns an array of its data
    public function getSheetData($sheetNameOrId) {
        $sheet = $this->getSheet($sheetNameOrId);
        return $sheet->getData();
    }

    // instantiates a sheet object (if needed) and returns the sheet object
    public function getSheet($sheet) {
        if(is_numeric($sheet)) {
            $sheet = $this->getSheetNameById($sheet);
        } elseif(!is_string($sheet)) {
            throw new Exception("Sheet must be a string or a sheet Id");
        }
        if(!array_key_exists($sheet, $this->sheets)) {
            $this->sheets[$sheet] = new XLSXWorksheet($this->getSheetXML($sheet), $sheet, $this);

        }
        return $this->sheets[$sheet];
    }

    public function getSheetNameById($sheetId) {
        foreach($this->sheetInfo as $sheetName=>$sheetInfo) {
            if($sheetInfo['sheetId'] === $sheetId) {
                return $sheetName;
            }
        }
        throw new Exception("Sheet ID $sheetId does not exist in the Excel file");
    }

    protected function getSheetXML($name) {
        return simplexml_load_string($this->getEntryData($this->sheetInfo[$name]['path']));
    }

    // converts an Excel date field (a number) to a unix timestamp (granularity: seconds)
    public static function toUnixTimeStamp($excelDateTime) {
        if(!is_numeric($excelDateTime)) {
            return $excelDateTime;
        }
        $d = floor($excelDateTime); // seconds since 1900
        $t = $excelDateTime - $d;
        return ($d > 0) ? ( $d - 25569 ) * 86400 + $t * 86400 : $t * 86400;
    }

}

class XLSXWorksheet {

    protected $workbook;
    public $sheetName;
    protected $data;
    public $colCount;
    public $rowCount;
    protected $config;

    public function __construct($xml, $sheetName, XLSXReader $workbook) {
        $this->config = $workbook->config;
        $this->sheetName = $sheetName;
        $this->workbook = $workbook;
        $this->parse($xml);
    }

    // returns an array of the data from the sheet
    public function getData() {
        return $this->data;
    }

    protected function parse($xml) {
        $this->parseDimensions($xml->dimension);
        $this->parseData($xml->sheetData);
    }

    protected function parseDimensions($dimensions) {
        $range = (string) $dimensions['ref'];
        $cells = explode(':', $range);
        $maxValues = $this->getColumnIndex($cells[1]);
        $this->colCount = $maxValues[0] + 1;
        $this->rowCount = $maxValues[1] + 1;
    }

    protected function parseData($sheetData) {
        $rows = array();
        $curR = 0;
        $lastDataRow = -1;
        foreach ($sheetData->row as $row) {
            $rowNum = (int)$row['r'];
            if($rowNum != ($curR + 1)) {
                $missingRows = $rowNum - ($curR + 1);
                for($i=0; $i < $missingRows; $i++) {
                    $rows[$curR] = array_pad(array(),$this->colCount,null);
                    $curR++;
                }
            }
            $curC = 0;
            $rowData = array();
            foreach ($row->c as $c) {
                list($cellIndex,) = $this->getColumnIndex((string) $c['r']);
                if($cellIndex !== $curC) {
                    $missingCols = $cellIndex - $curC;
                    for($i=0;$i<$missingCols;$i++) {
                        $rowData[$curC] = null;
                        $curC++;
                    }
                }
                $val = $this->parseCellValue($c);
                if(!is_null($val)) {
                    $lastDataRow = $curR;
                }
                $rowData[$curC] = $val;
                $curC++;
            }
            $rows[$curR] = array_pad($rowData, $this->colCount, null);
            $curR++;
        }
        if($this->config['removeTrailingRows']) {
            $this->data = array_slice($rows, 0, $lastDataRow + 1);
            $this->rowCount = count($this->data);
        } else {
            $this->data = $rows;
        }
    }

    protected function getColumnIndex($cell = 'A1') {
        if (preg_match("/([A-Z]+)(\d+)/", $cell, $matches)) {

            $col = $matches[1];
            $row = $matches[2];
            $colLen = strlen($col);
            $index = 0;

            for ($i = $colLen-1; $i >= 0; $i--) {
                $index += (ord($col{$i}) - 64) * pow(26, $colLen-$i-1);
            }
            return array($index-1, $row-1);
        }
        throw new Exception("Invalid cell index");
    }

    protected function parseCellValue($cell) {
        // $cell['t'] is the cell type
        switch ((string)$cell["t"]) {
            case "s": // Value is a shared string
                if ((string)$cell->v != '') {
                    $value = $this->workbook->sharedStrings[intval($cell->v)];
                } else {
                    $value = '';
                }
                break;
            case "b": // Value is boolean
                $value = (string)$cell->v;
                if ($value == '0') {
                    $value = false;
                } else if ($value == '1') {
                    $value = true;
                } else {
                    $value = (bool)$cell->v;
                }
                break;
            case "inlineStr": // Value is rich text inline
                $value = self::parseRichText($cell->is);
                break;
            case "e": // Value is an error message
                if ((string)$cell->v != '') {
                    $value = (string)$cell->v;
                } else {
                    $value = '';
                }
                break;
            default:
                if(!isset($cell->v)) {
                    return null;
                }
                $value = (string)$cell->v;

                // Check for numeric values
                if (is_numeric($value)) {
                    if ($value == (int)$value) $value = (int)$value;
                    elseif ($value == (float)$value) $value = (float)$value;
                    elseif ($value == (double)$value) $value = (double)$value;
                }
        }
        return $value;
    }

    // returns the text content from a rich text or inline string field
    public static function parseRichText($is = null) {
        $value = array();
        if (isset($is->t)) {
            $value[] = (string)$is->t;
        } else {
            foreach ($is->r as $run) {
                $value[] = (string)$run->t;
            }
        }
        return implode(' ', $value);
    }
}

虽然我建议获取最新代码来自 GitHub,整个 1 文件库不到 400 行代码,所以我包含了当前版本 (11/10/2018) 以上。

XLSXReader (available on GitHub) is based on SimpleXLSX (see answer by Sergey Shuchkin).

Convert dates from xlsx using XLSXReader::toUnixTimeStamp.

sample.xlsx

sample.xlsx

sample.php

require (__DIR__ . '/../xlsxreader/XLSXReader.php');

// entire workbook
$xlsx = new XLSXReader('sample.xlsx');
$sheetNames = $xlsx->getSheetNames();

// loop through worksheets
foreach ($sheetNames as $sheetName) {
    $sheet = $xlsx->getSheet($sheetName);

    // worksheet header
    echo('<h3>' . htmlentities($sheetName) . '</h3><table>' . PHP_EOL);
    $xlsx_data = $sheet->getData();
    $header_row_xlsx = array_shift($xlsx_data);

    // header row
    echo('<tr>' . PHP_EOL);
    echo("\t<th>row</th>" . PHP_EOL);
    for ($i = 0; $i < count($header_row_xlsx); $i++) {
        $xlsx_field_name = '' . $header_row_xlsx[$i];
        echo("\t<th>" . htmlentities("$xlsx_field_name") . '</th>' . PHP_EOL);
    }
    echo('</tr>' . PHP_EOL);

    // loop through data rows
    $row_number = 1;
    foreach ($xlsx_data as $row_xlsx) {

        // data row
        echo('<tr>' . PHP_EOL);
        echo("\t<td>" . htmlentities("$row_number") . '</td>' . PHP_EOL);
        for ($i = 0; $i < count($row_xlsx); $i++) {
            $xlsx_field_name = '' . ($i < count($header_row_xlsx) ? $header_row_xlsx[$i] : '');
            if ("$xlsx_field_name" === "DoB") {

                // date value
                $xlsx_field_value = DateTimeImmutable::
                        createFromFormat('U', XLSXReader::toUnixTimeStamp($row_xlsx[$i]))
                        ->format('Y-m-d');
            } else {

                // non-date value
                $xlsx_field_value = $row_xlsx[$i];

            }
            echo("\t<td>" . htmlentities("$xlsx_field_value") . '</td>' . PHP_EOL);
        }
        echo('</tr>' . PHP_EOL);

        $row_number++;
    }
    echo("</table><!-- end of $sheetName -->" . PHP_EOL);
}

output

<h3>Sheet1</h3><table>
<tr>
    <th>row</th>
    <th>Name</th>
    <th>Number</th>
    <th>DoB</th>
</tr>
<tr>
    <td>1</td>
    <td>David</td>
    <td>7</td>
    <td>1988-11-12</td>
</tr>
<tr>
    <td>2</td>
    <td>Walt</td>
    <td>8</td>
    <td>1972-01-19</td>
</tr>
</table><!-- end of Sheet1 -->

XLSXReader.php

<?php
/*
XLSXReader
Greg Neustaetter <[email protected]>
Artistic License

XLSXReader is a heavily modified version of:
    SimpleXLSX php class v0.4 (Artistic License)
    Created by Sergey Schuchkin from http://www.sibvision.ru - professional php developers team 2010-2011
    Downloadable from GitHub.

Key Changes include:
    Separation into two classes - one for the Workbook and one for Worksheets
    Access to sheets by name or sheet id
    Use of ZIP extension
    On-demand access of files inside zip
    On-demand access to sheet data
    No storage of XML objects or XML text
    When parsing rows, include empty rows and null cells so that data array has same number of elements for each row
    Configuration option for removing trailing empty rows
    Better handling of cells with style information but no value
    Change of class names and method names
    Removed rowsEx functionality including extraction of hyperlinks
*/

class XLSXReader {
    protected $sheets = array();
    protected $sharedstrings = array();
    protected $sheetInfo;
    protected $zip;
    public $config = array(
        'removeTrailingRows' => true
    );

    // XML schemas
    const SCHEMA_OFFICEDOCUMENT  =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument';
    const SCHEMA_RELATIONSHIP  =  'http://schemas.openxmlformats.org/package/2006/relationships';
    const SCHEMA_OFFICEDOCUMENT_RELATIONSHIP = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
    const SCHEMA_SHAREDSTRINGS =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
    const SCHEMA_WORKSHEETRELATION =  'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';

    public function __construct($filePath, $config = array()) {
        $this->config = array_merge($this->config, $config);
        $this->zip = new ZipArchive();
        $status = $this->zip->open($filePath);
        if($status === true) {
            $this->parse();
        } else {
            throw new Exception("Failed to open $filePath with zip error code: $status");
        }
    }

    // get a file from the zip
    protected function getEntryData($name) {
        $data = $this->zip->getFromName($name);
        if($data === false) {
            throw new Exception("File $name does not exist in the Excel file");
        } else {
            return $data;
        }
    }

    // extract the shared string and the list of sheets
    protected function parse() {
        $sheets = array();
        $relationshipsXML = simplexml_load_string($this->getEntryData("_rels/.rels"));
        foreach($relationshipsXML->Relationship as $rel) {
            if($rel['Type'] == self::SCHEMA_OFFICEDOCUMENT) {
                $workbookDir = dirname($rel['Target']) . '/';
                $workbookXML = simplexml_load_string($this->getEntryData($rel['Target']));
                foreach($workbookXML->sheets->sheet as $sheet) {                
                    $r = $sheet->attributes('r', true);
                    $sheets[(string)$r->id] = array(
                        'sheetId' => (int)$sheet['sheetId'],
                        'name' => (string)$sheet['name']
                    );

                }
                $workbookRelationsXML = simplexml_load_string($this->getEntryData($workbookDir . '_rels/' . basename($rel['Target']) . '.rels'));
                foreach($workbookRelationsXML->Relationship as $wrel) {
                    switch($wrel['Type']) {
                        case self::SCHEMA_WORKSHEETRELATION:
                            $sheets[(string)$wrel['Id']]['path'] = $workbookDir . (string)$wrel['Target'];
                            break;
                        case self::SCHEMA_SHAREDSTRINGS:
                            $sharedStringsXML = simplexml_load_string($this->getEntryData($workbookDir . (string)$wrel['Target']));
                            foreach($sharedStringsXML->si as $val) {
                                if(isset($val->t)) {
                                    $this->sharedStrings[] = (string)$val->t;
                                } elseif(isset($val->r)) {
                                    $this->sharedStrings[] = XLSXWorksheet::parseRichText($val);
                                }
                            }
                            break;
                    }
                }
            }
        }
        $this->sheetInfo = array();
        foreach($sheets as $rid=>$info) {
            $this->sheetInfo[$info['name']] = array(
                'sheetId' => $info['sheetId'],
                'rid' => $rid,
                'path' => $info['path']
            );
        }
    }

    // returns an array of sheet names, indexed by sheetId
    public function getSheetNames() {
        $res = array();
        foreach($this->sheetInfo as $sheetName=>$info) {
            $res[$info['sheetId']] = $sheetName;
        }
        return $res;
    }

    public function getSheetCount() {
        return count($this->sheetInfo);
    }

    // instantiates a sheet object (if needed) and returns an array of its data
    public function getSheetData($sheetNameOrId) {
        $sheet = $this->getSheet($sheetNameOrId);
        return $sheet->getData();
    }

    // instantiates a sheet object (if needed) and returns the sheet object
    public function getSheet($sheet) {
        if(is_numeric($sheet)) {
            $sheet = $this->getSheetNameById($sheet);
        } elseif(!is_string($sheet)) {
            throw new Exception("Sheet must be a string or a sheet Id");
        }
        if(!array_key_exists($sheet, $this->sheets)) {
            $this->sheets[$sheet] = new XLSXWorksheet($this->getSheetXML($sheet), $sheet, $this);

        }
        return $this->sheets[$sheet];
    }

    public function getSheetNameById($sheetId) {
        foreach($this->sheetInfo as $sheetName=>$sheetInfo) {
            if($sheetInfo['sheetId'] === $sheetId) {
                return $sheetName;
            }
        }
        throw new Exception("Sheet ID $sheetId does not exist in the Excel file");
    }

    protected function getSheetXML($name) {
        return simplexml_load_string($this->getEntryData($this->sheetInfo[$name]['path']));
    }

    // converts an Excel date field (a number) to a unix timestamp (granularity: seconds)
    public static function toUnixTimeStamp($excelDateTime) {
        if(!is_numeric($excelDateTime)) {
            return $excelDateTime;
        }
        $d = floor($excelDateTime); // seconds since 1900
        $t = $excelDateTime - $d;
        return ($d > 0) ? ( $d - 25569 ) * 86400 + $t * 86400 : $t * 86400;
    }

}

class XLSXWorksheet {

    protected $workbook;
    public $sheetName;
    protected $data;
    public $colCount;
    public $rowCount;
    protected $config;

    public function __construct($xml, $sheetName, XLSXReader $workbook) {
        $this->config = $workbook->config;
        $this->sheetName = $sheetName;
        $this->workbook = $workbook;
        $this->parse($xml);
    }

    // returns an array of the data from the sheet
    public function getData() {
        return $this->data;
    }

    protected function parse($xml) {
        $this->parseDimensions($xml->dimension);
        $this->parseData($xml->sheetData);
    }

    protected function parseDimensions($dimensions) {
        $range = (string) $dimensions['ref'];
        $cells = explode(':', $range);
        $maxValues = $this->getColumnIndex($cells[1]);
        $this->colCount = $maxValues[0] + 1;
        $this->rowCount = $maxValues[1] + 1;
    }

    protected function parseData($sheetData) {
        $rows = array();
        $curR = 0;
        $lastDataRow = -1;
        foreach ($sheetData->row as $row) {
            $rowNum = (int)$row['r'];
            if($rowNum != ($curR + 1)) {
                $missingRows = $rowNum - ($curR + 1);
                for($i=0; $i < $missingRows; $i++) {
                    $rows[$curR] = array_pad(array(),$this->colCount,null);
                    $curR++;
                }
            }
            $curC = 0;
            $rowData = array();
            foreach ($row->c as $c) {
                list($cellIndex,) = $this->getColumnIndex((string) $c['r']);
                if($cellIndex !== $curC) {
                    $missingCols = $cellIndex - $curC;
                    for($i=0;$i<$missingCols;$i++) {
                        $rowData[$curC] = null;
                        $curC++;
                    }
                }
                $val = $this->parseCellValue($c);
                if(!is_null($val)) {
                    $lastDataRow = $curR;
                }
                $rowData[$curC] = $val;
                $curC++;
            }
            $rows[$curR] = array_pad($rowData, $this->colCount, null);
            $curR++;
        }
        if($this->config['removeTrailingRows']) {
            $this->data = array_slice($rows, 0, $lastDataRow + 1);
            $this->rowCount = count($this->data);
        } else {
            $this->data = $rows;
        }
    }

    protected function getColumnIndex($cell = 'A1') {
        if (preg_match("/([A-Z]+)(\d+)/", $cell, $matches)) {

            $col = $matches[1];
            $row = $matches[2];
            $colLen = strlen($col);
            $index = 0;

            for ($i = $colLen-1; $i >= 0; $i--) {
                $index += (ord($col{$i}) - 64) * pow(26, $colLen-$i-1);
            }
            return array($index-1, $row-1);
        }
        throw new Exception("Invalid cell index");
    }

    protected function parseCellValue($cell) {
        // $cell['t'] is the cell type
        switch ((string)$cell["t"]) {
            case "s": // Value is a shared string
                if ((string)$cell->v != '') {
                    $value = $this->workbook->sharedStrings[intval($cell->v)];
                } else {
                    $value = '';
                }
                break;
            case "b": // Value is boolean
                $value = (string)$cell->v;
                if ($value == '0') {
                    $value = false;
                } else if ($value == '1') {
                    $value = true;
                } else {
                    $value = (bool)$cell->v;
                }
                break;
            case "inlineStr": // Value is rich text inline
                $value = self::parseRichText($cell->is);
                break;
            case "e": // Value is an error message
                if ((string)$cell->v != '') {
                    $value = (string)$cell->v;
                } else {
                    $value = '';
                }
                break;
            default:
                if(!isset($cell->v)) {
                    return null;
                }
                $value = (string)$cell->v;

                // Check for numeric values
                if (is_numeric($value)) {
                    if ($value == (int)$value) $value = (int)$value;
                    elseif ($value == (float)$value) $value = (float)$value;
                    elseif ($value == (double)$value) $value = (double)$value;
                }
        }
        return $value;
    }

    // returns the text content from a rich text or inline string field
    public static function parseRichText($is = null) {
        $value = array();
        if (isset($is->t)) {
            $value[] = (string)$is->t;
        } else {
            foreach ($is->r as $run) {
                $value[] = (string)$run->t;
            }
        }
        return implode(' ', $value);
    }
}

While I recommend getting the latest code from GitHub, the entire 1-file library is less than 400 lines of code, so I included the current version (11/10/2018) above.

预谋 2024-09-02 23:05:02

是的,使用 phpspreadsheet :

  include 'vendor/autoload.php';
        if($_FILES["import_excel"]["name"] != '')
        {
            $allowed_extension = array('xls', 'csv', 'xlsx');
            $file_array = explode(".", $_FILES["import_excel"]["name"]);
            $file_extension = end($file_array);
            if(in_array($file_extension, $allowed_extension))
            {
                $file_name = time() . '.' . $file_extension;
                move_uploaded_file($_FILES['import_excel']['tmp_name'], $file_name);
                $file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_name);
                $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);
                $spreadsheet = $reader->load($file_name);
                unlink($file_name);
                $data = $spreadsheet->getActiveSheet()->toArray();
                foreach($data as $row)
                {
                    $insert_data = array(
                        ':test1'          =>  $row[0],
                        ':test2'          =>  $row[1],
                        ':test3'          =>  $row[2],
                        ':test4'          =>  $row[3]
                    );
                 };
                $query = "
                    INSERT INTO post
                    (  test1, test2, test3, test4)
                    VALUES
                    ( :test1, :test2, :test3, :test4)
                ";
                $statement = $connect->prepare($query);
                $statement->execute($insert_data);
             }
             echo "succes";
        }else{
           echo "only xls,csv,xlsx are allowed";
        }

Yes with phpspreadsheet :

  include 'vendor/autoload.php';
        if($_FILES["import_excel"]["name"] != '')
        {
            $allowed_extension = array('xls', 'csv', 'xlsx');
            $file_array = explode(".", $_FILES["import_excel"]["name"]);
            $file_extension = end($file_array);
            if(in_array($file_extension, $allowed_extension))
            {
                $file_name = time() . '.' . $file_extension;
                move_uploaded_file($_FILES['import_excel']['tmp_name'], $file_name);
                $file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_name);
                $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);
                $spreadsheet = $reader->load($file_name);
                unlink($file_name);
                $data = $spreadsheet->getActiveSheet()->toArray();
                foreach($data as $row)
                {
                    $insert_data = array(
                        ':test1'          =>  $row[0],
                        ':test2'          =>  $row[1],
                        ':test3'          =>  $row[2],
                        ':test4'          =>  $row[3]
                    );
                 };
                $query = "
                    INSERT INTO post
                    (  test1, test2, test3, test4)
                    VALUES
                    ( :test1, :test2, :test3, :test4)
                ";
                $statement = $connect->prepare($query);
                $statement->execute($insert_data);
             }
             echo "succes";
        }else{
           echo "only xls,csv,xlsx are allowed";
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文