『Dcat Admin』如何获取$grid中的数据?

  • 使用第三方包 PhpSpreadsheet 导出excel

  • 导出方法如下

    public static function unitExport($data = null)
      {
          $file = public_path().'\static\excel\unit_export_template_001.xlsx';
    
          $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
    
          $worksheet = $spreadsheet->getActiveSheet();
    
          // data = [[],[],[],[]]
    
          // 设置数据
          // foreach ($data as $item){
          //
          // }
    
          $worksheet->getCell('A4')->setValue('John');
          $worksheet->getCell('A5')->setValue('Smith');
    
          header('Content-Type: application/vnd.ms-excel');
          header('Content-Disposition: attachment;filename="'.'基础数据清单-'.Date('Ymd-His').'.xls'.'"');
          header('Cache-Control: max-age=0');
    
          $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
          $writer->save('php://output');
      }
  • 我需要Grid中的数据传入方法,但是没办法获取所有或某页的数据

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

已经解决,在自定义导出中,$this->buildData()即可获取所有数据

class UnitExcelExporter extends AbstractExporter
{

    public function export()
    {
        $file = public_path().'\static\excel\unit_export_template_001.xlsx';

        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);

        $worksheet = $spreadsheet->getActiveSheet();

        // 获取数据
        $data = $this->buildData();
        // 过滤数据

        // 设置数据
        $worksheet->getCell('A4')->setValue('John');
        $worksheet->getCell('A5')->setValue('Smith');

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.'基础数据清单-'.Date('Ymd-His').'.xls'.'"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');
    }
}
1年前 评论
fishshadow (作者) (楼主) 1年前
讨论数量: 4

已经解决,在自定义导出中,$this->buildData()即可获取所有数据

class UnitExcelExporter extends AbstractExporter
{

    public function export()
    {
        $file = public_path().'\static\excel\unit_export_template_001.xlsx';

        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);

        $worksheet = $spreadsheet->getActiveSheet();

        // 获取数据
        $data = $this->buildData();
        // 过滤数据

        // 设置数据
        $worksheet->getCell('A4')->setValue('John');
        $worksheet->getCell('A5')->setValue('Smith');

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.'基础数据清单-'.Date('Ymd-His').'.xls'.'"');
        header('Cache-Control: max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');
    }
}
1年前 评论
fishshadow (作者) (楼主) 1年前

就是喜欢这种提问,解决了完还不忘贴上解决方案

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!