我有一個大約95.500行和28列的xlsx文件存在內存問題。 要處理這樣的大文件(超過10 MB xlsx)我寫下面的代碼,但是當我執行代碼並調用加載方法時,即使只讀取一行,也會收到內存耗盡錯誤! (我只分配到128Mb的PHP解釋器)用128Mb內存耗盡的phpexcel內存只讀大文件的第一行
請考慮以下因素:
目前我嘗試只讀一個單列和接收有關內存耗盡的錯誤(見$ chunkFilter-> setRows (1,1);)
解決關於閱讀的第一行這個問題後,我需要閱讀所有其他線路如果您認爲有其他的庫加載內容數據庫表
或解決方案,請c onsider我更喜歡PHP作爲語言,因爲是用於此應用程序的主要語言,但我可以接受與其他語言的任何其他解決方案(如圍棋)
請,不要簡單地建議增加PHP的內存處理。我遺憾地知道這是可能的,但是這個代碼在最大隻有512Mb RAM的VPS共享服務器上運行,並且我需要保持儘可能最低的內存使用
有解決方案嗎?請看下面的代碼,我使用:
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter to read file in "chunks" */
class chunkReadFilter implements PHPExcel_Reader_IReadFilter {
private $_startRow = 0;
private $_endRow = 0;
/** Set the list of rows that we want to read */
public function setRows($startRow, $chunkSize) {
$this->_startRow = $startRow;
$this->_endRow = $startRow + $chunkSize;
}
public function readCell($column, $row, $worksheetName = '') {
// Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) {
return true;
}
return false;
}
}
function loadXLSFile($inputFile){
// Initiate cache
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_sqlite3;
if (!PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
echo date('H:i:s'), " Unable to set Cell Caching using ", $cacheMethod,
" method, reverting to memory", EOL;
}
$inputFileType = PHPExcel_IOFactory::identify($inputFile);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$chunkFilter = new chunkReadFilter();
// Tell the Read Filter, the limits on which rows we want to read this iteration
$chunkFilter->setRows(1,1);
// Tell the Reader that we want to use the Read Filter that we've Instantiated
$objReader->setReadFilter($chunkFilter);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFile);
}
UPDATE
下面的錯誤返回的要求pamelus
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 112 bytes) in /vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel2007.php on line 471
PHP Stack trace:
PHP 1. {main}() dataimport.php:0
PHP 2. loadFileToDb($inputFile = *uninitialized*, $tabletoupdate = *uninitialized*) dataimport.php:373
PHP 3. PHPExcel_Reader_Excel2007->load($pFilename = *uninitialized*) dataimport.php:231
您可以請發佈您收到的錯誤消息嗎?或者至少有一個行號? – pamelus
hi pamelus,謝謝你對我的問題的興趣,請在更新的問題上找到確切的erorr消息 – Marco