Excel::create('File Name', function($excel) {
$excel->sheet('Sheet Name', function($sheet) {
$head = array(
'Title 1',
'Title 2',
'Title 3',
'Title 4'
);
$data = array($head);
$sheet->fromArray($data, null, 'A1', false, false);
}
});
})->download('xls');
Its just a basic code to create an excel file and insert titles.
Refer: http://www.maatwebsite.nl/laravel-excel/docs for more
Yeah, I know the docs. But I need to open an existing worksheet! ButI don't know it's name. There is a function for that (SheetByIndex) but I don't get it to work.
Did you try like this??
$objPHPExcel->getActiveSheetIndex(0)->setCellValue('A1' , 'Test String'); // 0 for first sheet
didnt try it out yet, waiting your feeback ;)
That is the function I meant, yes :) But I somehow doesnt work. My fully code:
Excel::load($source, function($file)
{
$file -> getActiveSheetIndex(0) -> setCellValue('C9', 'Test String');
}) -> download('xls');
Error: "Call to a member function setCellValue() on a non-object" in line 3 of the code above.
What is my problem?
Are you sure it's 0-based? When working with excel, first sheets is generally 1.
@Chris, well, right now i would have to check,can't remember if 0-basewd or not. It's documented on phpexcel docs, so...
But the problem here is something else, before that! Error: "Call to a member function setCellValue() on a non-object" means $file is not and instantiated Phpexcel object.
@Michael, in your closure, before line 3, do a dd($file); you will realize in not a phpexcel object!
Do you have the complete code available, github or something?
Try something like this
require_once('../vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php');
$objPHPExcel = PHPExcel_IOFactory::load($your_excel_file_path);
dd($file);
iboinas said: @Michael, in your closure, before line 3, do a dd($file); you will realize in not a phpexcel object!
It is an PHPExcel object. I tried it.
Excel::load($source, function($file)
{
$sheet = $file -> getActiveSheetIndex(1);
$sheet -> setCellValue('C9', 'Test String');
}) -> download('xls');
I get a valid result. It must be another problem. If I exclude the line from my code (line 4) I get a valid result (download). So most of the
And that is my full code! If you want to know more, check LaravelExcel. That the Component I use.
So the only thing I want to achive is, how to open, edit the first worksheet and then download the file. But I still can't get it to work.
For anyone that finds this thread in the future, here's a working example:
Excel::load('template.xls', function($doc) {
$sheet = $doc->setActiveSheetIndex(0);
$sheet->setCellValue('D5', 'Test');
})->download('xlsx');
It weird how they don't provide basic examples like this in the docs.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.