【问题标题】:How to generate dynamic columns using Maatwebsite Excel in Laravel 5.1如何在 Laravel 5.1 中使用 Maatwebsite Excel 生成动态列
【发布时间】:2016-12-30 09:53:23
【问题描述】:

我可以像这样动态生成行

$row_count = 2;
for($i=0; $i < count($student_data); $i++) {
  $sheet->Row($row_count, array(
    $student_data[$i]->stud_number, 
    $student_data[$i]->first_name .' ' .$student_data[$i]->last_name 
  ));
  $row_count++;
}

$sheet->cells('A1:I'.$row_count, function($cells) {
  $cells->setAlignment('center');
}); 

但我不能动态生成列,我想做这样的事情

$current_column = 'C';
for($i=0; $i < count($subject_data); $i++) {
  //I want here my column incremented by 1 like D, E, F, G....
  //Do something..
}

【问题讨论】:

    标签: laravel maatwebsite-excel


    【解决方案1】:

    您也可以按字母顺序递增:

    $current_column = 'C';
    
    for($i=0; $i < count($subject_data); $i++) {
        print $current_column; // Will be C, D, E, etc...
        $current_column++; // Increment letter
    }
    

    【讨论】:

      【解决方案2】:

      根据doc,您可以按单元格名称操作任何单元格

      $sheet->cell('A1', function($cell) {
      
          // manipulate the cell
          $cell->setValue('data1');
      
      });
      

      【讨论】:

        【解决方案3】:

        我正在使用用户模型。所以你可以检查这个解决方案。

         $data =  User::get();  // Model 
        
          Excel::create('Project_sheet', function($excel) use($data)  {
                $excel->sheet('user_list', function($sheet) use( $data ) {
        
                    $from = "A1"; // or any value
                    $to = "G1"; // or any value
        
                    //$sheet->getActiveSheet()->mergeCells('A1:G1');
                    //$sheet->getActiveSheet()->setCellValue('A1','The quick brown fox.');
        
        
                    $sheet->getStyle("$from:$to")->getFont()->setBold( true );
                    $sheet->getStyle("$from:$to")->getFont()->setSize( '12' );
                    $sheet->setBorder("$from:$to", 'thin' );
                    // Set background color for a specific cell
                    $sheet->getStyle("$from:$to")->applyFromArray(array(
                        'fill' => array(
                            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
                            'color' => array('rgb' => 'A5D9FF')
                        )
                    ));
                    $sheet->fromArray($data);
                });
            })->export('xls');
        

        【讨论】:

          猜你喜欢
          • 2019-11-06
          • 1970-01-01
          • 1970-01-01
          • 2018-06-23
          • 2018-02-28
          • 2019-01-28
          • 2017-11-12
          • 2020-09-04
          • 1970-01-01
          相关资源
          最近更新 更多