4

I am using PHP (laravel) for writing data dynamically in excel sheet from database. I have excel sheet in which I have filled data in first row with formula in few columns.

Now what I want, I just want to copy that formula in every respective column dynamically when I am writing data dynamically. I am using maatwebsite for writing the data in excel sheet. See below code.

Excel::selectSheetsByIndex(1)->load(public_path('uploads') . '/data.xlsx', function($reader) {
            $reader->sheet('Sheetname', function($sheet) {
                        // Append row as very last
                        $sheet->appendRow(array(
                            'appended', 'appended' // Here I am appending the dynamic data in my code.
                        ));
                    });
        }, 'UTF-8')->store('xlsx', true);

Suppose column "M" has formula in first row so whenever new record will fill in this column dynamically that column must contain the same formula. How can I achieve this? Anyone faced this before?

2 Answers 2

4
+50

you can use like $sheet->setCellValue('B5','=SUM(B2:B4)'); at the time of writing data on excel file... Hope this will help you...
UPDATED CODE

for($k=1;$k>n;$k++){ $sheet->setCellValue('AC' . $k, '=SUM(N' . $k . ',O' . $k . ',Q' . $k . ',S' . $k . ',T' . $k . ',V' . $k . ',Y' . $k . ',AB' . $k . ')'); }

Sign up to request clarification or add additional context in comments.

5 Comments

But I need to copy the formula of above column to next one.
you can generate formula dynamically for each row, check my updated answer.
So there is no way to copy the formula in next cell of excel?
Let me try for this
May be there is some other option to achieve this... but my above solution fulfill your requirement...
2

You can try something like this

$rule = ['appended', 'appended']; 
    Excel::selectSheetsByIndex(1)->load(public_path('uploads') . '/data.xlsx', function ($reader) use ($rule) {
        $reader->sheet('Sheetname', function ($sheet) use ($rule) {
            // Append row as very last
            $sheet->appendRow($rule);
        });
    }, 'UTF-8')->store('xlsx', true);

6 Comments

In this way formula will be copy in my column?
According to you code, you wanted to append codes ` $sheet->appendRow(array( 'appended', 'appended' // Here I am appending the dynamic data in my code. ));` Now you can append the rule dynamically ... assign the rule to the variable ...
Sorry for inconvenience but I just want to copy the formula of column in next dynamic columns data. See: Suppose column "M" has formula in first row so whenever new record will fill in this column dynamically that column must contain the same formula.
Any suggestion on this?
The formula of the column "M", are you getting it dynamically from the database????
|

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.