我在laravel 5中完成了导入,但在导出文件时遇到了一些问题。 代码不正确,不提供任何逻辑。 我尝试过类似文档
public function exportData(Test $test,Excel $excel){
$test=$test->all();
Excel::create('Test excel', function($test) {
$test->sheet('sheet1', function($sheet) {
$sheet->setOrientation('landscape');
});
})->export('xls');
}
如何从mysql导出csv文件? 我正在使用" maatwebsite / excel":" ~2.0.0"封装
答案 0 :(得分:1)
您可以在Laravel中使用INTO OUTFILE
public function exportData($file_name) {
$file = public_path()."/downloads/".$file_name; //CSV file is store this path
$query = sprintf("select * from tests INTO OUTFILE '%s' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n'",$file);
\DB::connection()->getpdo()->exec($query);
}
答案 1 :(得分:0)
您可以考虑使用league/csv
包。他们有an example of exporting a database query to CSV。在您的情况下,它看起来像:
public function exportData() {
// get the data
$data = Test::all();
// get a Writer object for writing to CSV
$csv = League\Csv\Writer::createFromFileObject(new SplTempFileObject());
// add the data to the writer
$csv->insertAll($data);
// output the file
$csv->output('testdata.csv');
}