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

前端 未结 3 1284
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 11:25

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 12:00

    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);
    

提交回复
热议问题