-4

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

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

3 回答 3

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/

于 2016-10-25T11:20:45.853 回答
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() 快

于 2018-03-20T17:47:08.343 回答
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');
    
于 2016-11-24T11:04:22.003 回答