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

前端 未结 3 1314
爱一瞬间的悲伤
爱一瞬间的悲伤 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 11:56

    I think You can also use "split by file size":

    $part = 1;
    
    $maxSize = 50;//50 Mb
    
    $fopen = fopen('filename.csv','r') or die ('ERROR');
    
    while (($line = fgetcsv($fopen, 10000, ";")) !== FALSE) {
    
        $ftowrite = fopen("Part_$part.csv",'a');
    
        fputcsv($ftowrite,$line);
    
        clearstatcache();
    
        $size = filesize ( "review_p$part.csv" ) / 1000000;
    
        if ($size  > $maxSize) {
    
            fclose($ftowrite);
    
            $part++;
    
        }
    }
    

提交回复
热议问题