Laravel Maatwebsite / excel随机添加额外列中的数据

时间:2017-04-27 12:43:03

标签: php excel laravel

我正在使用Laravel中的maatwebsite / excel构建一个excel文件。然而,它似乎是在行中添加数据而不是我期望的方式。我错过了可能造成这种情况的事吗?它也不一致,例如对于新的工作表,即使工作表左侧可能有不同的数字,它也会添加相同的数据。

enter image description here

作为一种解决方法,我不得不为每一行添加一堆空单元格......

                $sheet->row( 1, array(
                    'From: '.$from_date,
                    'To: '.$to_date,
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    ''
                ) );

我的代码没有解决方法:

    $rows[$product_type] = array (
        'Received'              => $received,
        'Visually Inspected'    => $visually_inspected,
        'Visually Passed'       => $visually_passed,
        'Visually Failed'       => $visually_failed,
        'Technically Inspected' => $technically_inspected,
        'Technically Passed'    => $technically_passed,
        'Technically Failed Phase 1' => $technically_failed_1,
        'Technically Failed Phase 2' => $technically_failed_2,
        'Technically Failed Phase 3' => $technically_failed_3,
        'Total Technically Failed'    => $failed
    );
    Excel::create( 'KGdfmg564fdsin4', function ( $excel ) use ( $rows, $product_types, $to_date, $from_date) {

        // Set the title
        $excel->setTitle( 'Company Dashboard Report Data' );

        // Chain the setters
        $excel->setCreator( 'Company ' )
              ->setCompany( 'Company ' );
        foreach($product_types as $key => $val) {
            $excel->sheet( $key, function ( $sheet ) use ( $rows, $key, $to_date, $from_date ) {

                $sheet->fromArray( $rows );
                $sheet->setWidth( array(
                    'A' => 50,
                    'B' => 50
                ) );
                $sheet->row( 1, array(
                    'From: '.$from_date,
                    'To: '.$to_date,

                ) );
                $sheet->row( 2, array(
                    'Received',
                    $rows[$key]['Received'],

                ) );
                $sheet->row( 3, array(
                    'Visually Inspected',
                    $rows[$key]['Visually Inspected'],
                ) );
                $sheet->row( 4, array(
                    'Visually Passed',
                    $rows[$key]['Visually Passed'],
                ) );
                $sheet->row( 5, array(
                    'Visually Failed',
                    $rows[$key]['Visually Failed'],
                ) );
                $sheet->row( 6, array(
                    'Technically Inspected',
                    $rows[$key]['Technically Inspected'],
                ) );
                $sheet->row( 7, array(
                    'Technically Passed',
                    $rows[$key]['Technically Passed'],
                ) );
                $sheet->row( 8, array(
                    'Technically Failed Phase 1',
                    $rows[$key]['Technically Failed Phase 1'],
                ) );
                $sheet->row( 9, array(
                    'Technically Failed Phase 2',
                    $rows[$key]['Technically Failed Phase 2'],
                ) );
                $sheet->row( 10, array(
                    'Technically Failed Phase 3',
                    $rows[$key]['Technically Failed Phase 3'],
                ) );
                $sheet->row( 11, array(
                    'Units Technically Failed',
                    $rows[$key]['Total Technically Failed'],
                ) );


            } );
        }

        // Call them separately
        $excel->setDescription( 'Dashboard Report Data' );

    } )->save( 'xlsx' );

1 个答案:

答案 0 :(得分:0)

从代码中删除$sheet->fromArray( $rows );可以帮助我试一试。

enter image description here