search
HomeBackend DevelopmentPHP TutorialHow to cut large excel files with PHP (complete code attached)

This article mainly talks about using phpspreadsheet to cut large excel files. It has certain reference value. Friends who are interested can learn about it. I hope it can inspire you.

Using phpspreadsheet, you can easily parse excel files, but the memory consumption of phpspreadsheet is also relatively large. I have tried to parse nearly 5M of pure text excel, and the memory usage will exceed the default maximum memory of php of 128M.
Of course this can be solved by adjusting the memory size, but it is more dangerous when the amount of concurrency is large. So today I will introduce the next method, using phpspreadsheet to cut excel files. This is a method of exchanging time for space, so it can generally be used for low timeliness requirements.

Method:

First put a function readCell provided by phpspreadsheet official website, we can use this function to perform cutting.

First, pre-read the excel file, mainly to obtain all worksheets and the number of data rows under the worksheet. At this stage, the readCell method always returns false. We only need to record the worksheet that readCell comes in. and the number of data rows.

The next step is to analyze the obtained records and determine how many rows of original excel data need to be loaded into each part of the data. It should be noted that in order to avoid content confusion, do not cut the contents of the two worksheets together.

The last step is to loop through the analyzed data and use readCell again to obtain each part of the data. Note that each time the file is read, the disconnectWorksheets method must be used to clean up the memory of phpspreadsheet.

After my own testing, I found that using this method to parse a 5M excel file only requires an average of 21M of memory!

Code

<?php    
namespace CutExcel;    
require_once &#39;PhpSpreadsheet/autoload.php&#39;;    
/**    
 * 预读过滤类    
 * @author wangyelou     
 * @date 2018-07-30    
 */    
class MyAheadreadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter    
{    
    public $record = array();    
    private $lastRow = &#39;&#39;;    
    public function readCell($column, $row, $worksheetName = &#39;&#39;)     
    {    
        if (isset($this->record[$worksheetName]) ) {    
            if ($this->lastRow != $row) {    
                $this->record[$worksheetName] ++;           
                $this->lastRow = $row;    
            }     
        } else {    
            $this->record[$worksheetName] = 1;           
            $this->lastRow = $row;    
        }    
        return false;    
    }    
}    
/**    
 * 解析过滤类    
 * @author wangyelou     
 * @date 2018-07-30    
 */    
class MyreadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter    
{    
    public $startRow;    
    public $endRow;    
    public $worksheetName;    
    public function readCell($column, $row, $worksheetName = &#39;&#39;)     
    {    
        if ($worksheetName == $this->worksheetName && $row >= ($this->startRow+1) && $row <= ($this->endRow+1)) {    
            return true;    
        }    
        return false;    
    }    
}    
/**    
 * 切割类    
 * @author wangyelou     
 * @date 2018-07-30    
 */    
class excelCut    
{    
    public $cutNum = 5;    
    public $returnType = &#39;Csv&#39;;    
    public $fileDir = &#39;/tmp/&#39;;    
    public $log;    
    /**    
     * 切割字符串    
     * @param $str    
     * @return array|bool    
     */    
    public function cutFromStr($str)    
    {    
        try {    
            $filePath = &#39;/tmp/&#39; . time() . mt_rand(1000, 9000) . $this->returnType;    
            file_put_contents($filePath, $str);    
            if (file_exists($filePath)) {    
                $result =  $this->cutFromFile($filePath);    
                unlink($filePath);    
                return $result;    
            } else {    
                throw new Exception(&#39;文件写入错误&#39;);    
            }    
        } catch (Exception $e) {    
            $this->log = $e->getMessage();    
            return false;    
        }    
    }    
    /**    
     * 切割文件    
     * @param $file    
     * @return array|bool    
     */    
    public function cutFromFile($file)    
    {    
        try {    
            $cutRules = $this->readaheadFromFile($file);    
            $dir = $this->getFileDir($file);    
            $returnType = $this->returnType ? $this->returnType : &#39;Csv&#39;;    
            $results = array();    
            //初始化读    
            $myFilter = new MyreadFilter();    
            $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file);    
            $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);    
            $reader->setReadDataOnly(true);    
            $reader->setReadFilter($myFilter);    
            foreach ($cutRules as $sheetName => $rowIndexRange) {    
                //读    
                list($myFilter->startRow, $myFilter->endRow, $myFilter->worksheetName) = $rowIndexRange;    
                $spreadsheetReader = $reader->load($file);    
                $sheetData = $spreadsheetReader->setActiveSheetIndexByName($myFilter->worksheetName)->toArray(null, false, false, false);    
                $realDatas = array_splice($sheetData, $myFilter->startRow, ($myFilter->endRow - $myFilter->startRow + 1));    
                $spreadsheetReader->disconnectWorksheets();    
                unset($sheetData);    
                unset($spreadsheetReader);    
                //写    
                $saveFile = $dir . $sheetName . &#39;.&#39; . $returnType;    
                $spreadsheetWriter = new \PhpOffice\PhpSpreadsheet\Spreadsheet();    
                foreach ($realDatas as $rowIndex => $row) {    
                    foreach ($row as $colIndex => $col) {    
                        $spreadsheetWriter->getActiveSheet()->setCellValueByColumnAndRow($colIndex+1, $rowIndex+1, $col);    
                    }    
                }    
                $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheetWriter, $returnType);    
                $writer->save($saveFile);    
                $spreadsheetWriter->disconnectWorksheets();    
                unset($spreadsheetWriter);    
                $results[] = $saveFile;    
            }    
            return $results;    
        } catch (Exception $e) {    
            $this->log = $e->getMessage();    
            return false;    
        }    
    }    
    /**    
     * 预读文件    
     */    
    public  function readaheadFromFile($file)    
    {    
        if (file_exists($file)) {    
            //获取统计数据    
            $myFilter = new MyAheadreadFilter();    
            $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file);    
            $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);    
            $reader->setReadDataOnly(true); //只读数据    
            $reader->setReadFilter($myFilter);    
            $spreadsheet = $reader->load($file);    
            //$sheetData = $spreadsheet->getActiveSheet()->toArray(null, false, false, false);    
            list($fileName,) = explode(&#39;.&#39;, basename($file));    
            $datas = array();    
            $averageNum = ceil(array_sum($myFilter->record) / $this->cutNum);    
            foreach ($myFilter->record as $sheetName => $count) {    
                for ($i=0; $i<ceil($count/$averageNum); $i++) {    
                    $datas[$fileName . &#39;_&#39; . $sheetName . &#39;_&#39; . $i] = array($i*$averageNum, ($i+1)*$averageNum-1, $sheetName);    
                }    
            }    
            return $datas;    
        } else {    
            throw new Exception($file . &#39; not exists&#39;);    
        }    
    }    
    /**    
     * 创建目录    
     * @param $file    
     * @return bool|string    
     */    
    protected function getFileDir($file)    
    {    
        $baseName = basename($file);    
        list($name) = explode(&#39;.&#39;, $baseName);    
        $fullName = $name .&#39;_&#39;. time() . &#39;_&#39; . mt_rand(1000, 9999);    
        $path = $this->fileDir . $fullName . &#39;/&#39;;    
        mkdir($path, 0777);    
        chmod($path, 0777);    
        if (is_dir($path)) {    
            return $path;    
        } else {    
            $this->log = "mkdir {$path} failed";    
            return false;    
        }    
    }    
}

Related tutorials: PHP video tutorial

The above is the detailed content of How to cut large excel files with PHP (complete code attached). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version