使用PHP

时间:2019-02-25 19:14:47

标签: php phpspreadsheet phpexcelreader

我正在尝试将Excel分成多个文件,例如this Github code

我的数据已被分割,但是在所有文件中它具有相同的信息,并且没有CHUNK ONE:LINE 1-1000 CHUNK TWO:LINE 1001-2000,有人可以帮助我吗?

下面是我的代码:

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

0 个答案:

没有答案