Support the ongoing development of Laravel.io →
Eloquent Packages
Last updated 2 years ago.
0
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

0

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.

0

Did you try like this??

$objPHPExcel->getActiveSheetIndex(0)->setCellValue('A1' , 'Test String'); // 0 for first sheet

didnt try it out yet, waiting your feeback ;)

Last updated 9 years ago.
0

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?

0

Are you sure it's 0-based? When working with excel, first sheets is generally 1.

Last updated 9 years ago.
0

@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);
Last updated 9 years ago.
0

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.

Last updated 9 years ago.
0

Hi @Michael - We're you able to resolve this issue? Need to your thoughts on this.

0

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.

0

Really weird. Thanks mcuk. That's exactly what I was looking for.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

LukasMa lukasma Joined 26 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.