0

I have to upload excel and read data then writing to DB. I need to get data from 2mb excel file. I have done coding up to reading excel data. While uploading excel there was shown some issues

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20 bytes)

I have solved this by using

 ini_set('memory_limit', '-1');

and also execution time issues that also somehow managed by

set_time_limit(0);

the problem is when I'm trying to upload excel size more than 300kb, it taking too much to complete the execution. Can anyone suggest me best way to read data from excel ? Is there any performance issue if I'm storing the values to an array before inserting to DB ?

Adding my codes here

<?php

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
include('../includes/class_read_xl.php');
$obj_read_xl=new class_read_xl();
if(isset($_POST["upload"])) {
    move_uploaded_file($_FILES["ufile"]["tmp_name"], "../uploads/xlsheet/" . $_FILES["ufile"]["name"]);
    $file = $_FILES['ufile']['name'];
    $inputFileName = '../uploads/xlsheet/'.$file;
    //$inputFileName = "test.xls";
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objReader->setReadDataOnly(false);
    // Load $inputFileName to a PHPExcel Object
    $objPHPExcel = $objReader->load($inputFileName);
    $total_sheets=$objPHPExcel->getSheetCount(); // here 4
    $allSheetName=$objPHPExcel->getSheetNames(); // array ([0]=>'student',[1]=>'teacher',[2]=>'school',[3]=>'college')
    //print_r($allSheetName);
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); // first sheet
    $highestRow = $objWorksheet->getHighestRow(); // here 5
    $highestColumn = $objWorksheet->getHighestColumn(); // here 'E'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);  // here 5
    //exit();
    $arr_data=array();
    for ($row = 1; $row <= $highestRow; ++$row) {
    for ($col = 0; $col <= $highestColumnIndex; ++$col) {
        $value = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
         if(is_array($arr_data) ) {
            $arr_data[$row-1][$col]=$value;
         }
        }
    }

    print_r($arr_data);     
}
?>
4

1 回答 1

0

看起来您没有使用任何推荐的内存保存方法,例如 PHPExcel 文档中描述的单元缓存或“分块” - 请参阅开发人员文档的第 4.2.1 节(标题为“单元缓存”,以及阅读电子表格文件的用户文档的各个部分。

如果您实际上只是从文件中的一个工作表中读取数据,只需加载该工作表,而不是全部四个

将值存储到数组将显着增加脚本的内存需求,因为您的数组也会占用内存。但是,您可以对每一行使用 rangeToArray() 方法,而不是单独读取一行中的每个单元格,这不会是很大的内存开销,并且比读取每个单独的单元格要快

于 2013-09-06T12:00:43.460 回答