0

I'm trying to write an excel from a laravel controller where i have all the data in an array, and i managed to write it, but i have to format it now, this is the array that i have:

[2020-12-30 13:22:05] local.DEBUG: array (
  0 => 
  array (
    0 => '1',
    1 => '2',
  ),
  1 => 
  array (
    0 => 'Test Name 1',
    1 => 'Test Name 2',
  ),
  2 => 
  array (
    0 => 'user',
    1 => 'user2',
  ),
  3 => 
  array (
    0 => '1',
    1 => '2',
  ),
) 

This is the way i'm creating the Excel:

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

            $excel->sheet('Sheetname', function($sheet) use ($budgets) { //$budgets is the array i'm printing on Log

                $sheet->fromArray($budgets);

            });

        })->export('xls');

And this is the way that my excel is printing it:

1 2
Test Name 1 Test Name 2
user1 user2
1 2

And the way i want to print it is:

Code Name User Number
1 Test Name 1 user1 1
2 Test Name 2 user2 2

But i don't know how to achieve this. Can anyone help me with this?

//edits//

I added some code to re estructure the array, now i have this:

$users = ['Code', 'Name', 'User', 'Number'];
        for ($i=0; $i<count($code); $i++){
            array_push($users, array($code[$i], $name[$i], $user[$i], $number[$i]));
        }
        
        Log::debug($users);

And this is the Log:

[2020-12-30 15:17:40] local.DEBUG: array (
  0 => 'Code',
  1 => 'Name',
  2 => 'User',
  3 => 'Number',
  4 => 
  array (
    0 => '1',
    1 => 'Test Name 1',
    2 => 'user1',
    3 => '1',
  ),
  5 => 
  array (
    0 => '2',
    1 => 'Test Name 2',
    2 => 'user2',
    3 => '2',
  ),
) 

But i'm getting this error:

[message] => Row `Code` must be array.
            [class] => PHPExcel_Exception

2 Answers 2

1

You will could re-structure your array. To get the print you want array should look like:

$budget = [
    ['Code', 'Name', 'User', 'Number'],
    [1, 'Test Name 1', 'user1', 1],
    ...
];
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to do this now: $budgets = array('Code', 'Name', 'User', 'Number'); for ($i=0; $i<count($code); $i++){ array_push($users, array($code[$i], $name[$i], $user[$i], $number[$i])); } but i'm getting an error "Row Code must be array."
Is you variable $code instance of an array? Also you named "header" array $budgets, but in for loop you are pushing into $users.
Sorry, yes, i made a mistake with the array names, it's the same users array that i'm declaring with the "headers". I'm going to edit the post, so you can see it more clearly
0

Ok, so, i managed to re-structure the array and getting the Excel the way i wanted doing this:

public function generateExcel(Request $request)
    {
        $code = $request->input('code');
        $name = $request->input('name');
        $user = $request->input('user');
        $number = $request->input('number');

        $users = array();
        array_push($users, array('Code', 'Name', 'User', 'Number'));
        for ($i=0; $i<count($code); $i++){
            array_push($users, array($code[$i], $name[$i], $user[$i], $number[$i]));
        }
        
        
        Excel::create('Filename', function($excel) use ($users) {

            $excel->sheet('Sheetname', function($sheet) use ($users) {

                $sheet->fromArray($users);

            });

        })->export('xls');
        return true;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.