请参阅http://www.maatwebsite.nl/laravel-excel/docs/export 我用它来导出excel表单数组。
答案 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);
}
答案 1 :(得分:0)
$ 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()更快