Modifying and saving a workbook #3621
Replies: 14 comments
-
Instantiate a Writer, passing it the Spreadsheet as an argument, then use the save method, passing it a folder/filename |
Beta Was this translation helpful? Give feedback.
-
$directory = 'directoryname';
$outputFile = 'filename.xlsx';
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("$directoryname/$outputFile"); |
Beta Was this translation helpful? Give feedback.
-
load($importFile); $workbook->getActiveSheet()->setCellValue('B7', 'Hello World'); $outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $writer = IOFactory::createWriter($outboundWorkbook, "Xlsx"); $writer->save($exportFile); ?> |
Beta Was this translation helpful? Give feedback.
-
Sorry but I don't know how to format it properly so that is shows on separate lines |
Beta Was this translation helpful? Give feedback.
-
It saves as a blank workbook. Here is my code
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
$importFile = '/work/dosc/myworkbook.xlsx';
$exportFile = '/work/dosc/sent/myworkbook_revised.xlsx';
$reader = IOFactory::createReader("Xlsx");
$workbook = $reader->load($importFile);
$workbook->getActiveSheet()->setCellValue('B7', 'Hello World');
$outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$writer = IOFactory::createWriter($outboundWorkbook, "Xlsx");
$writer->save($exportFile);
?>
From: Mark Baker ***@***.***>
Sent: Monday, June 19, 2023 3:50 PM
To: PHPOffice/PhpSpreadsheet ***@***.***>
Cc: Don Proshetsky ***@***.***>; Author ***@***.***>
Subject: Re: [PHPOffice/PhpSpreadsheet] Modifying and saving a workbook (Discussion #3621)
Instantiate a Writer, passing it the Spreadsheet as an argument, then use the save method, passing it a folder/filename
—
Reply to this email directly, view it on GitHub <#3621 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AB24LSPTUQTKJJZS2OQJ6S3XMCUPXANCNFSM6AAAAAAZMKEVOQ> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/AB24LSMKIBVHSYMZGOXV7WTXMCUPXA5CNFSM6AAAAAAZMKEVOSWGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAL32QQ.gif> Message ID: ***@***.*** ***@***.***> >
|
Beta Was this translation helpful? Give feedback.
-
It saves as a blank workbook. Here is my code
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
$importFile = '/work/dosc/myworkbook.xlsx';
$exportFile = '/work/dosc/sent/myworkbook_revised.xlsx';
$reader = IOFactory::createReader("Xlsx");
$workbook = $reader->load($importFile);
$workbook->getActiveSheet()->setCellValue('B7', 'Hello World');
$outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$writer = IOFactory::createWriter($outboundWorkbook, "Xlsx");
$writer->save($exportFile);
?>
From: oleibman ***@***.***>
Sent: Monday, June 19, 2023 3:51 PM
To: PHPOffice/PhpSpreadsheet ***@***.***>
Cc: Don Proshetsky ***@***.***>; Author ***@***.***>
Subject: Re: [PHPOffice/PhpSpreadsheet] Modifying and saving a workbook (Discussion #3621)
$directory = 'directoryname';
$outputFile = 'filename.xlsx';
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("$directoryname/$outputFile");
—
Reply to this email directly, view it on GitHub <#3621 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AB24LSPSRSSTCY7SAH6TTBTXMCUTNANCNFSM6AAAAAAZMKEVOQ> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/AB24LSOF2HK6MXSJA7SZV3LXMCUTNA5CNFSM6AAAAAAZMKEVOSWGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAL32RA.gif> Message ID: ***@***.*** ***@***.***> >
|
Beta Was this translation helpful? Give feedback.
-
You're instantiating a new Spreadsheet object and passing that as the argument to the Writer. |
Beta Was this translation helpful? Give feedback.
-
That’s the confusing part. If I do that, it will overwrite the original file instead of creating a new file.
From: Mark Baker ***@***.***>
Sent: Monday, June 19, 2023 5:16 PM
To: PHPOffice/PhpSpreadsheet ***@***.***>
Cc: Don Proshetsky ***@***.***>; Author ***@***.***>
Subject: Re: [PHPOffice/PhpSpreadsheet] Modifying and saving a workbook (Discussion #3621)
You're instantiating a new Spreadsheet object and passing that as the argument to the Writer.
Pass in the Spreadsheet object that you loaded.
—
Reply to this email directly, view it on GitHub <#3621 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AB24LSMV5UQ3JSF6T3ACCB3XMC6Q7ANCNFSM6AAAAAAZMKEVOQ> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/AB24LSM2I2BZHP3HHQYZLWTXMC6Q7A5CNFSM6AAAAAAZMKEVOSWGG33NNVSW45C7OR4XAZNRIRUXGY3VONZWS33OINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAL33TC.gif> Message ID: ***@***.*** ***@***.***> >
|
Beta Was this translation helpful? Give feedback.
-
I see something about cloning a spreadsheet. Do I need to do that first before writing? How to write to a different location? |
Beta Was this translation helpful? Give feedback.
-
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
$importFile = '/work/dosc/myworkbook.xlsx';
$exportFile = '/work/dosc/sent/myworkbook_revised.xlsx';
$reader = IOFactory::createReader("Xlsx");
$workbook = $reader->load($importFile);
$workbook->getActiveSheet()->setCellValue('B7', 'Hello World');
// the next 2 statement are where you are going wrong
//$outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
//$writer = IOFactory::createWriter($outboundWorkbook, "Xlsx");
// use the following single statement in place of those 2
$writer = IOFactory::createWriter($workbook, "Xlsx");
$writer->save($exportFile); To format your code nicely, use a line with 3 back-ticks followed by the 3 characters php. |
Beta Was this translation helpful? Give feedback.
-
Ok thanks.I’ll repost my question with the code as a new post since I haven’t yet received a solution.Sent from my iPhoneOn Jun 19, 2023, at 6:21 PM, oleibman ***@***.***> wrote:
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
$importFile = '/work/dosc/myworkbook.xlsx';
$exportFile = '/work/dosc/sent/myworkbook_revised.xlsx';
$reader = IOFactory::createReader("Xlsx");
$workbook = $reader->load($importFile);
$workbook->getActiveSheet()->setCellValue('B7', 'Hello World');
// the next 2 statement are where you are going wrong
//$outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
//$writer = IOFactory::createWriter($outboundWorkbook, "Xlsx");
// use the following single statement in place of those 2
$writer = IOFactory::createWriter($workbook, "Xlsx");
$writer->save($exportFile);
To format your code nicely, use a line with 3 back-ticks followed by the 3 characters php.
Then include your code.
Then finish the operation by using a line with 3 back-ticks.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Oh O noticed you did respond with the fix, thank you! I will try tomorrow Sent from my iPhoneOn Jun 19, 2023, at 6:21 PM, oleibman ***@***.***> wrote:
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
$importFile = '/work/dosc/myworkbook.xlsx';
$exportFile = '/work/dosc/sent/myworkbook_revised.xlsx';
$reader = IOFactory::createReader("Xlsx");
$workbook = $reader->load($importFile);
$workbook->getActiveSheet()->setCellValue('B7', 'Hello World');
// the next 2 statement are where you are going wrong
//$outboundWorkbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
//$writer = IOFactory::createWriter($outboundWorkbook, "Xlsx");
// use the following single statement in place of those 2
$writer = IOFactory::createWriter($workbook, "Xlsx");
$writer->save($exportFile);
To format your code nicely, use a line with 3 back-ticks followed by the 3 characters php.
Then include your code.
Then finish the operation by using a line with 3 back-ticks.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hi, After making your modification, it is still throwing an error: Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception: Table Table26 for Structured Reference cannot be located in /home/httpd/www/my-directory/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Engine/Operands/StructuredReference.php:150 Code as follows
|
Beta Was this translation helpful? Give feedback.
-
I would need to see your workbook in order to attempt to debug this problem. I do not know much about Structured References. You can probably work around the problem by adding the following to your code before you save the file: $writer->setPreCalculateFormulas(false); |
Beta Was this translation helpful? Give feedback.
-
Hi. I've figured out how to load a workbook and write to cells in a spreadsheet. I would now like to save it as a ew file in a different directory than the file loaded. I can't figure out how to do this?
Beta Was this translation helpful? Give feedback.
All reactions