php 使用 PHPExcel 读取电子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21507898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:12:27  来源:igfitidea点击:

Reading spreadsheet using PHPExcel

phpphpexcel

提问by baarkerlounger

I'm trying to upload a spreadsheet and read it into a MySQL database using PHPExcel.

我正在尝试上传电子表格并使用 PHPExcel 将其读入 MySQL 数据库。

For .xlsx files it works fine but whenever I try to upload a .ods file it throws the error: PHP Fatal error: Call to a member function getNamespaces() on a non-object in PHPExcel_1.7.9/Classes/PHPExcel/Reader/OOCalc.php on line 341

对于 .xlsx 文件,它工作正常,但每当我尝试上传 .ods 文件时,它都会抛出错误: PHP 致命错误:在 PHPExcel_1.7.9/Classes/PHPExcel/Reader/ 中的非对象上调用成员函数 getNamespaces()第 341 行的 OOCalc.php

What's going wrong?

怎么了?

HTML Form:

HTML 表单:

<form method="post" enctype="multipart/form-data">
Upload File: <input type="file" name="spreadsheet"/>
<input type="submit" name="submit" value="Submit" />
</form>

PHP (In same file):

PHP(在同一个文件中):

//Check valid spreadsheet has been uploaded
if(isset($_FILES['spreadsheet'])){
if($_FILES['spreadsheet']['name']){
    if(!$_FILES['spreadsheet']['error'])
    {

        $inputFile = $_FILES['spreadsheet']['name'];
        $extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));
        if($extension == 'XLSX' || $extension == 'ODS'){

            //Read spreadsheeet workbook
            try {
                 $inputFile = $_FILES['spreadsheet']['tmp_name'];
                 $inputFileType = PHPExcel_IOFactory::identify($inputFile);
                 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                 $objPHPExcel = $objReader->load($inputFile);
            } catch(Exception $e) {
                    die($e->getMessage());
            }

            //Get worksheet dimensions
            $sheet = $objPHPExcel->getSheet(0); 
            $highestRow = $sheet->getHighestRow(); 
            $highestColumn = $sheet->getHighestColumn();

            //Loop through each row of the worksheet in turn
            for ($row = 1; $row <= $highestRow; $row++){ 
                    //  Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                    //Insert into database
            }
        }
        else{
            echo "Please upload an XLSX or ODS file";
        }
    }
    else{
        echo $_FILES['spreadsheet']['error'];
    }
}
}

?>

回答by Nicolas Racine

When a file is uploaded to your webserver, The file will be saved in the temporary folder of your system with a random name.

当文件上传到您的网络服务器时,该文件将以随机名称保存在您系统的临时文件夹中。

What you were trying to do was giving the actual nameof the file you uploaded, But since the file was created with a random name in the tmp folder. You will need to use tmp_nameinstead, Which actually point the that random named file.

您试图做的是提供name您上传的文件的实际内容,但是由于该文件是在 tmp 文件夹中使用随机名称创建的。您将需要使用它tmp_name来代替,它实际上指向那个随机命名的文件。

Also note, in nameYou only have the name of the file that was uploaded and not the path, But with tmp_nameyou have the actual path to the file.

另请注意,在name您只有上传文件的名称而不是路径,但tmp_name您有文件的实际路径。

See the following example of a file upload you would get.

请参阅以下您将获得的文件上传示例。

array(
 [UploadFieldName]=>array(
    [name] => MyFile.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/php/php6hst32
    [error] => UPLOAD_ERR_OK
    [size] => 98174
  )
)

change your code to this instead

改为将您的代码更改为此

 //Check valid spreadsheet has been uploaded
if(isset($_FILES['spreadsheet'])){
if($_FILES['spreadsheet']['tmp_name']){
if(!$_FILES['spreadsheet']['error'])
{

    $inputFile = $_FILES['spreadsheet']['tmp_name'];
    $extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));
    if($extension == 'XLSX' || $extension == 'ODS'){

        //Read spreadsheeet workbook
        try {
             $inputFileType = PHPExcel_IOFactory::identify($inputFile);
             $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                 $objPHPExcel = $objReader->load($inputFile);
        } catch(Exception $e) {
                die($e->getMessage());
        }

        //Get worksheet dimensions
        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow(); 
        $highestColumn = $sheet->getHighestColumn();

        //Loop through each row of the worksheet in turn
        for ($row = 1; $row <= $highestRow; $row++){ 
                //  Read a row of data into an array
                $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                //Insert into database
        }
    }
    else{
        echo "Please upload an XLSX or ODS file";
    }
}
else{
    echo $_FILES['spreadsheet']['error'];
}
}
}

?>

回答by Fábio Rodrigues

In my case there was an error detecting the extension in this line

在我的情况下,检测到此行中的扩展时出错

$extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));

if you need to solve just check from the name parameter

如果您需要解决,只需从 name 参数中检查

$extension = strtoupper(explode(".", $_FILES['spreadsheet']['name'])[1]);

The rest is working thanks :)

其余的工作谢谢:)