Hi, I saw a few people asking but I did not see a full code example (sorry if I missed it). Could you please provide a full code example of how to append data to existing Excel file?
Examples of appending rows can be found inside our documentation:
Thanks for the reply! Those are only two lines of code, so I am not sure how to use them. I am trying this:
Excel::load($path . '/exported.xls', function($reader){
$sheet = $reader->getActiveSheet();
// Manipulate third row
$sheet->row(3, array(
'test1', 'test2'
));
});
But I am getting Call to undefined method PHPExcel_Worksheet::row()
The file already exists on the server and I only want to append data to it.
Sorry, I misunderstood your question.
At this moment it's not possible to edit an existing file. Appending rows only works when creating a new Excel file.
Starting from version 1.2.* you will be able to edit existing files and export them, by using this syntax:
Excel::load($path . '/exported.xls', function($reader)
{
$reader->sheet(function($sheet)
{
$sheet->appendRow([
'test1', 'test2',
]);
});
})->export('xls');
If you want to edit a specific sheet, you can do: $reader->sheet(1, function() ...) or $reader->sheet('SheetTitle', function() ...)
Thank you! That will be awesome! I really like how you maintain this package.
You're welcome :)
hello,
can u plz help me out whether below is possible or not if yes how?.
by editing existing file
1.can we overwrite a particular cell
2.can we append data to a row which has already some data
I get this error with the code supplied: Illegal offset type in isset or empty.
When I using this code,
Excel::load(storage_path('exports') . '/report.csv', function($reader) {
$reader->sheet(function($sheet) {
$sheet->rows($data);
});
})->store('csv',storage_path('exports'),false);
I am getting this error,
Illegal offset type in isset or empty
Same here, I'm getting this error
exception 'ErrorException' with message 'Illegal offset type in isset or empty' in pathToVendor/vendor/phpoffice/phpexcel/Classes/PHPExcel.php:582
from this code
Excel::load($file_path, function ($excel) {
$excel->sheet(function($sheet) {
$sheet->appendRow([
'test1', 'test2',
]);
});
})->store($ext, storage_path('exports'), true);
I'm using version "~1.3.0"
You need to select a sheet, how else does Excel know where to append the row?
$excel->sheet('sheetName', function()
Thank you for your really fast answer @patrickbrouwers!
After I put 'sheetName' it getting another error,
Call to a member function appendRow() on null in path/to/myControllerName.php on line 200
I think my problems is same as this issue https://github.com/Maatwebsite/Laravel-Excel/issues/502#issue-97465111
or am I put the wrong 'sheetName' ? because I try to append the CSV file (Generated using same library)
so here's the example:
if(! File::exists($file_path)){
$excelFile = Excel::create($filename, function ($excel) use($data)
{
$excel->setTitle('Export');
$excel->setCreator('Meta System')->setCompany('Company Sdn Bhd.');
$excel->setDescription('Export Master Data');
$excel->sheet('Sheet1', function ($sheet) use($data)
{
$sheet->fromArray($data, null, 'A1', true, true);
});
})->store($ext, storage_path('exports'), true);
}else{
$excelFile = Excel::load($file_path, function ($reader) {
$reader->sheet('Sheet1', function($sheet) {
$sheet->appendRow([
'test1', 'test2',
]);
});
})->export($ext);
}
This worked for me
\Excel::load('master.xls', function($reader) {
$reader->sheet('ReportsMaster',function($sheet) {
$sheet->appendRow([
'test1', 'test2',
]);
});
})->export('xls');
@ingenio-se
I use your code and get this
Call to a member function appendRow() on null
Did yo check the array you are using with the function?
Maybe paste your code to have a hint of what鈥檚 going on
@hisway double check the name of the sheet. Its saying null because the sheet you are trying to append is incorrect. If anything copy the sheet name directly from the workbook sometimes there are leading or trailing spaces that you are not using.
can someone explain how to do this in in 3.0 ?
how can i append column in the existing column?? can anyone help me out
Most helpful comment
Starting from version
1.2.*you will be able to edit existing files and export them, by using this syntax:If you want to edit a specific sheet, you can do:
$reader->sheet(1, function() ...)or$reader->sheet('SheetTitle', function() ...)