1

I'm trying to split an Excel into multiple files like in this Github code

My data has been split, but in all files it has the same information and not CHUNK ONE: LINE 1 - 1000 CHUNK TWO: LINE 1001 - 2000, someone can help me?

Below is my code:

        echo 'Loading file ',pathinfo($inputFilePath,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';

    /**  Create a new Reader of the type defined in $inputFileType  **/
    $reader = IOFactory::createReader($inputFileType);
    /**  Define how many rows we want to read for each "chunk"  **/
    $chunkSize = 50000;
    /**  Create a new Instance of our Read Filter  **/
    $chunkFilter = new Chunk();

    /**  Tell the Reader that we want to use the Read Filter  **/
    /**    and that we want to store it in contiguous rows/columns  **/
    $reader->setReadFilter($chunkFilter)
        ->setContiguous(true);

    /**  Instantiate a new Spreadsheet object manually  **/
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

    /**  Set a sheet index  **/
    $sheet = 0;
    /**  Loop to read our worksheet in "chunk size" blocks  **/
    /**  $startRow is set to 2 initially because we always read the headings in row #1  **/
    for ($startRow = 2; $startRow <= 200000; $startRow += $chunkSize) {
        // Tell the Read Filter, the limits on which rows we want to read this iteration
        $chunkFilter->setRows($startRow, $chunkSize);
        // Increment the worksheet index pointer for the Reader
        $reader->setSheetIndex($sheet);
        // Load only the rows that match our filter into a new worksheet in the PhpSpreadsheet Object
        $reader->loadIntoExisting($inputFilePath, $spreadsheet);
        // Set the worksheet title (to reference the "sheet" of data that we've loaded)
        // and increment the sheet index as well
        $spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet));
    }
    $loadedSheetNames = $spreadsheet->getSheetNames();
    foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
        $spreadsheet->setActiveSheetIndexByName($loadedSheetName);
        $sheetData = $spreadsheet->getActiveSheet()->toArray(null, false, false, true);
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
        $writer->save($_SERVER['DOCUMENT_ROOT'] . "/cabledet_att/" .$loadedSheetName. '.csv');
        echo($loadedSheetName);
        echo("<br>");
    }
Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • Hi Guilherme and welcome to StackOverflow. Is your output from this code a series of files all containing chunk one (lines 0-1000) or multiple files, each containing all the original data (lines 0 to end)? – Yvonne Aburrow Feb 25 '19 at 19:33
  • Hi @YvonneAburrow thanks for the answer, multiple files, containing the same lines (0 to $chunkSize). For exemplo, my "for" only stop in 200,000 so i needed 4 files, each containing 50,000 lines with the same information, but i wish 4 file with all information, file 1: 0 to 50,000, file 2 : 50,000 to 100,000. – Guilherme Prado Feb 25 '19 at 20:14
  • Looks to me as though the issue is in your for loop – Yvonne Aburrow Feb 25 '19 at 20:58

0 Answers0