4

I have used maatwebsite excel library for Import and Exporting data. i want to set cell wise data like.

    A          B         C          D          E          F
1   heading1   heading2  heading3   heading4   heading5   heading6
2   data1      data2     data3      data4      data5      data6
3   data12     data22    data32     data42     data52     data62
4              data23    data33     data43     data53     
5                        data34     data44     data54
6                        data35     data45     data54
7                                   data46     data56
8                                   data47     data57
9                                   data48
10                                  data49
11                                  data50
12                                  data51

I want to export data something like that i try a lot but not success.

below is my code.

$sheet->fromArray($dataHeading);

I used this function for adding value to excel file. but the problem is this function add value raw wise I want to add value cell wise.

Like first of all I adding value to A then B etc...

I can't find any function that allowed me to adding value to cell wise.

please help how can I manage this. i have data as php array like.

array(
   'heading1' => array(
       'data1','data12'
    ),
    'heading2' => array(
        'data2','data22','data23'
    ),
    'heading3' => array(
        'data3','data32','data33','data34','data35'
    ),
    'heading4' => array(
        'data4','data42','data43','data44','data45','data46','data47','data47','data49','data50','data51'
    ),
    'heading5' => array(
         'data5','data52','data53','data54','data56','data57'
    ),
    'heading6' => array(
         'data6','data62'
    )
)

How can I enter this array to excel file. if I need to change in array please suggest me. i able to change my array.

Thanks

Renish Khunt
  • 5,620
  • 18
  • 55
  • 92

2 Answers2

6

Not sure if it addresses your problem, but seen the title some people trying to just set the value of a single cell might end up here anyway :)

It is not documented but looking in the sourcecode I found it is possible:

$sheet->setCellValue('A1', 'some value');

or

$sheet->cell('A1', function($cell) {
    $cell->setValue('some value');
});

A bit off-topic but I hope it helps some people anyway.

Arno van Oordt
  • 2,912
  • 5
  • 33
  • 63
0

Used this function and change the extension of file(xls,csv etc) in the last and you will get your required file with the correct data.

public function exportCSV(Request $request){
    $data = array();
    $messageemail           =   '';
    $data['usertotal']      =   User::select('name','email','mobile')->orderBy('name','ASC')->get();
    $data['total-user']     =   array();
    $percounting            =   0;
        foreach($data['usertotal'] as $datanew){
            $data['total-user'][$percounting]['email']                  = $datanew->email;
            $data['total-user'][$percounting]['mobile']                 = $datanew->mobile;
            $data['total-user'][$percounting]['name']                   = $datanew->name;
            $percounting++;
        }
        /* Start Exporting Data */
        Excel::create(time(), function($excel) use($data) {
             $excel->sheet('Sheetname', function($sheet) use($data) {
             $datasheet = array();
             $datasheet[0]  =   array('email','mobile','fullname');
             $i=1;
             foreach($data['total-user'] as $datanew){
                        $datasheet[$i] = array( @$datanew['email'],
                        $datanew['mobile'],  
                        $datanew['name'],
                    );
                $i++;
             }
            $sheet->fromArray($datasheet);
            });

        })->export('xls');

    return Redirect::back();


}

Note: This function is required maatwebsite package. If you don't want to use this package then you can also do this via core php library.

waseem asgar
  • 664
  • 8
  • 20
  • Sorry this code is not working for me thanks. just assume i have 10 mobile number and 5 email address and 3 names now your function is working without getting any error? – Renish Khunt May 26 '16 at 19:04
  • of-course this function is working fine because i created this function before 2 year ago and continue used this function till now. If this function is not working for you then you have to check your code. If you are unable to do this then send the code and database to my email address. – waseem asgar May 27 '16 at 05:37
  • please stored my array to `$data['usertotal']` and then check i got error. – Renish Khunt May 27 '16 at 06:09