2016-08-20 111 views
0

我的代码是从网站上复制的。Laravel Excel无法下载和导出

Excel::create('Filename', function($excel) { 

// Set the title 
$excel->setTitle('Our new awesome title'); 

// Chain the setters 
$excel->setCreator('Maatwebsite') 
     ->setCompany('Maatwebsite'); 

    // Call them separately 
    $excel->setDescription('A demonstration to change the file properties'); 
})->download('xls'); 

我成功下载了一次。

但是,其他的尝试是错误的。

错误消息是。

Whoops, looks like something went wrong. 

FatalErrorException in LaravelExcelWriter.php line 263: 


Call to a member function getMergeCells() on a non-object 
in LaravelExcelWriter.php line 263 

回答

-1
• Write following in cmd 
composer require Maatwebsite/excel 
• after install /run upper composer........Maalwebsite/excel successfully check in composer.json that 

"require": { 
     "php": ">=5.5.9", 
     "laravel/framework": "5.2.*", 
     "laravelcollective/html": "5.2.*", 
     "Maatwebsite/excel": "^2.1" 
    }, 

• config/app.php/ in provides 
Maatwebsite\Excel\ExcelServiceProvider::class, 
• config/app.php/ in alias 
'Excel' => Maatwebsite\Excel\Facades\Excel::class, 
• php artisan vendor:publish 
• php artisan make:controller ExcelController 
• open excel then create first_name,last_name,sex,email,phone then store datas and save as .csv 
• in controlller for import 

use App\Customer; 
use Input; 
use DB; 
use Excel; 

class ExcelController extends Controller 
{ 
    // 
    public function getImport() 
    { 
     return view('excel.importCustomer'); 
    } 

    public function postImport() 
    { 
     Excel::load(Input::file('customer'),function($reader){ 

    $reader->each(function($sheet){ 
     Customer::firstOrCreate($sheet->toArray()); 
     }); 
    }); 
    } 
} 
• in routes for import 

Route::get('/getImport','[email protected]'); 
Route::post('/postImport','[email protected]'); 
• In controller for export 

    public function getExport() 
    { 
     $export=Customer::all(); 
     Excel::create('Export Data',function($excel) use ($export){ 
      $excel->sheet('Sheet 1',function($sheet) use ($export){ 
       $sheet->fromArray($export); 
      }); 
     })->export('xlsx'); 
    } 
+0

出口ü可以使用该公共功能getExport() { \t $ export = Customer :: all(); \t Excel中::创建( '导出数据',函数($ Excel文件)使用($出口){ \t \t $ excel->片( '表1',函数($片)使用($出口){ \t \t \t $ sheet-> fromArray($ export); \t \t}); \t}) - > export('xlsx'); } – Borna

+0

它仍然没有工作。 –

+0

是LaravelExcelWriter.php你的模型? – Borna

0

我知道为什么原因。

因为在我的情况下没有任何工作表。

然而,表是必要的。

谢谢。