简单的Excel导出与laravel与maatwebsite

时间:2016-10-25 10:38:05

标签: laravel maatwebsite-excel

请参阅http://www.maatwebsite.nl/laravel-excel/docs/export 我用它来导出excel表单数组。

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

3 个答案:

答案 0 :(得分:0)

试试这段代码:

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/

答案 1 :(得分:0)

  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');
    

答案 2 :(得分:0)

如果有人发现这个有用的

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()更快