Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append data to existing excel? #208

Closed
vladoa opened this issue Sep 11, 2014 · 19 comments
Closed

Append data to existing excel? #208

vladoa opened this issue Sep 11, 2014 · 19 comments

Comments

@vladoa
Copy link

vladoa commented Sep 11, 2014

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?

@MaatwebsiteSupport
Copy link
Contributor

Examples of appending rows can be found inside our documentation:

http://www.maatwebsite.nl/laravel-excel/docs/export#rows

@vladoa
Copy link
Author

vladoa commented Sep 11, 2014

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.

@MaatwebsiteSupport
Copy link
Contributor

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.

@MaatwebsiteSupport
Copy link
Contributor

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() ...)

@vladoa
Copy link
Author

vladoa commented Sep 16, 2014

Thank you! That will be awesome! I really like how you maintain this package.

@MaatwebsiteSupport
Copy link
Contributor

You're welcome :)

@raju-aavudoddi
Copy link

raju-aavudoddi commented Jun 26, 2016

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
3. is there any option to create drop down list for a column
4. readonly cell/row/column

@michaeljcoyne
Copy link

I get this error with the code supplied: Illegal offset type in isset or empty.

@cubetsarath
Copy link

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

@koyanyaroo
Copy link

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"

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Oct 11, 2016

You need to select a sheet, how else does Excel know where to append the row?

$excel->sheet('sheetName', function()

@koyanyaroo
Copy link

koyanyaroo commented Oct 11, 2016

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 #502 (comment)

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);
        }

@ingenio-se
Copy link

This worked for me

  \Excel::load('master.xls', function($reader) {

  $reader->sheet('ReportsMaster',function($sheet) {
    $sheet->appendRow([
         'test1', 'test2',
     ]);
  });
  })->export('xls');

@hisway
Copy link

hisway commented Feb 11, 2018

@ingenio-se
I use your code and get this

Call to a member function appendRow() on null

@ingenio-se
Copy link

Did yo check the array you are using with the function?
Maybe paste your code to have a hint of what’s going on

@grimlock591
Copy link

@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.

@xee-beast
Copy link

can someone explain how to do this in in 3.0 ?

@GlennM
Copy link
Contributor

GlennM commented May 21, 2019

Please see #2068

@m-sonu
Copy link

m-sonu commented Nov 7, 2019

how can i append column in the existing column?? can anyone help me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests