如何上传使用PHPExcel库大量的Excel文件?

问题描述 投票:-1回答:1

当我上传使用的数据库(phpMySql)PHP的Excel文件,我得到错误的

错误加载文件“”:无法打开您的阅读!文件不存在

而且几乎都超过40000行Excel中。如果我上传它有400-1000行,然后它的工作Excel文件。

我使用PHPExcel库。

这里是我的代码:

<html>
    <head>
        <title>Import Excel</title>
        <style>
            table {
                border-collapse: collapse;
                width: 100%;
            }

            th, td {
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even) {background-color: #f2f2f2;}
    </style>

    </head>

    <?php
        require 'Classes/PHPExcel/IOFactory.php';

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "office";

        if(isset($_POST['upload'])){
            $inputfilename = $_FILES['file']['tmp_name'];
            $exceldata = array();

            $conn = mysqli_connect($servername, $username, $password, $dbname);

            if(!$conn){
                die("Connection Failed: " . mysqli_connect_error());
            }

            try {
                $inputfiletype = PHPExcel_IOFactory::identify($inputfilename);
                $objReader = PHPExcel_IOFactory::createReader($inputfiletype);
                $objPHPExcel = $objReader->load($inputfilename);
            } catch(Exception $e){
                die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage());
            }

        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

        for($row = 2; $row <= $highestRow; $row++) {
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);

            $sql = "INSERT INTO allsec (emp_code, emp_full_name, request_type, leave_status, leave_from, leave_to, days, year, month, pl, pm, lpm, status, emp_name, wg, leave_type)
        VALUES ('".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."', '".$rowData[0][11]."', '".$rowData[0][12]."', '".$rowData[0][13]."', '".$rowData[0][14]."', '".$rowData[0][15]."')";

            if(mysqli_query($conn, $sql)){
                $exceldata[] = $rowData[0];
            } else{
                echo "Error: " .$sql . "<br>" . mysqli_error($conn);
            }

        }


        echo "<table border='1'>";
        foreach($exceldata as $index => $excelraw){
            echo "<tr>";
            foreach($excelraw as $excelcolumn){
                echo "<td>".$excelcolumn."</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

        mysqli_close($conn);

    }
    ?>
    <body>
        <form action="" method="POST" enctype="multipart/form-data">
                <input type="file" name="file" >
                <input type="submit" name ="upload" value="upload">
        </form> 
    </body>
</html>
php phpexcel
1个回答
1
投票

如果文件大小太大,那么你就需要修改php.ini文件。默认情况下,你只能上传8MB的文件。尝试改变下列值40米,并重新启动服务器。

;允许的最大尺寸为上传文件。的upload_max_filesize = 40M

;必须大于或等于的upload_max_filesize的post_max_size = 40M

另一个原因可能是您的数据(40000擅长行)正在采取更多的时间比分配给服务器上的脚本执行的最大时间来执行。要更改默认的最大时间,您需要更改的Apache服务器上的max_input_time设置设置(搜索类似的设置,如果你使用的是不同的服务器)。

此链接将帮助您更改服务器的超时设置 - https://stackoverflow.com/a/8744184/10602679

© www.soinside.com 2019 - 2024. All rights reserved.