【问题标题】:Simple Excel Export with laravel with maatwebsite使用 laravel 和 maatwebsite 进行简单的 Excel 导出
【发布时间】:2017-03-07 08:57:20
【问题描述】:

http://www.maatwebsite.nl/laravel-excel/docs/export
我用这个喜欢导出excel表格数组。

  1. excel 是导出的,但它从 A3 开始
  2. 我想开始 A1。

【问题讨论】:

    标签: laravel maatwebsite-excel


    【解决方案1】:

    试试这个代码:

    function convertToCSV($data, $options) {
    
            // setting the csv header
            if (is_array($options) && isset($options['headers']) && is_array($options['headers'])) {
                $headers = $options['headers'];
            } else {
                $headers = array(
                    'Content-Type' => 'text/csv',
                    'Content-Disposition' => 'attachment; filename="ExportFileName.csv"'
                );
            }
    
            $output = '';
    
            // setting the first row of the csv if provided in options array
            if (isset($options['firstRow']) && is_array($options['firstRow'])) {
                $output .= implode(',', $options['firstRow']);
                $output .= "\n"; // new line after the first line
            }
    
            // setting the columns for the csv. if columns provided, then fetching the or else object keys
            if (isset($options['columns']) && is_array($options['columns'])) {
                $columns = $options['columns'];
            } else {
                $objectKeys = get_object_vars($data[0]);
                $columns = array_keys($objectKeys);
            }
    
            // populating the main output string
            foreach ($data as $row) {
                foreach ($columns as $column) {
                    $output .= str_replace(',', ';', $row->$column);
                    $output .= ',';
                }
                $output .= "\n";
            }
    
            // calling the Response class make function inside my class to send the response.
            // if our class is not a controller, this is required.
            return Response::make($output, 200, $headers);
        }
    

    来源:http://www.amitavroy.com/justread/content/articles/creating-csv-output-database-query-result-laravel-4/

    【讨论】:

    • 感谢朋友。我从那个 maatwebsit excel 文档中找到了答案。
    【解决方案2】:
    1. myexcel 是 excel 的名称
    2. $response_array 这是一个数组,包含要在 excel 中导出的详细信息

      Excel::create('myexcel', function($excel) use($response_array) {
      
          $excel->sheet('Sheet 1', function($sheet) use($response_array) {
      
              $sheet->fromArray($response_array);
          });
      
      })->export('xls');
      

    【讨论】:

      【解决方案3】:

      如果有人觉得这有帮助

      Excel::create('myexcel', function($excel) use($response_array) {
      
          $excel->sheet('sheet1', function($sheet) use ($response_array) {
              $sheet->fromArray($response_array, null, 'A1', false, false);
          });
      });
      
      })->export('xls');
      

      但我更喜欢$sheet->LoadView(),它比$sheet->fromArray() 快

      【讨论】:

        猜你喜欢
        • 2016-11-23
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-15
        • 2020-07-09
        • 2021-12-10
        • 1970-01-01
        相关资源
        最近更新 更多