Split large Excel/Csv file to multiple files on PHP or Javascript

calendar_today Asked May 24, 2013
thumb_up 9 upvotes
history Updated April 16, 2026

Question posted 2013 · +3 upvotes

I have excel(file.xls)/csv(file.csv) file that contains/will contain hundreds of thousands of entry, even millions I guess. Is it possible to split this one to multiple file? Like file.xls to file1.xls, file2.xls, file3.xls and so on.

Are there any libraries to use? Is this possible on PHP? or how about javascript? On where I can specify how many rows to be included on each file?

Thanks

Accepted answer +9 upvotes

Quick and dirty way of splitting a CSV file into several CSV files

$inputFile = 'input.csv';
$outputFile = 'output';

$splitSize = 10000;

$in = fopen($inputFile, 'r');

$rowCount = 0;
$fileCount = 1;
while (!feof($in)) {
    if (($rowCount % $splitSize) == 0) {
        if ($rowCount > 0) {
            fclose($out);
        }
        $out = fopen($outputFile . $fileCount++ . '.csv', 'w');
    }
    $data = fgetcsv($in);
    if ($data)
        fputcsv($out, $data);
    $rowCount++;
}

fclose($out);

Top excel Q&A (6)

+9 upvotes ranks this answer #91 out of 167 excel solutions on this site .