Currently using template excel sheet to add data, the requirement is add new sheet too, so need to add new sheet with same template.
My starts with new tamplate :
$filePath = public_path('Template.xls');
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
Now, I have to create new sheet in same file with same tamplate without clone first sheet, because need to load fresh template without data.
You don't simply want to create a new sheet, you want to copy a sheet with all its existing styleing
So create a clone of the existing sheet, give it a new title (because worksheet titles must be unique), and attach that clone to the workbook.
$newSheet = clone $objPHPExcel->getActiveSheet();
$newSheet->setTitle('Worksheet 2');
$objPHPExcel->addSheet($newSheet);
You've modified the question since I posted the answer to your original question.
If you need to create a new worksheet from the template after you've already loaded the original template and populated it with data, then you'll need to reload the template as a new PHPExcel object, and inject the worksheet from that into your original workbook:
$filePath = public_path('Template.xls');
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
// fill with data
$template = PHPExcel_IOFactory::load($filePath);
$newSheet = clone $template->getActiveSheet();
$objPHPExcel->addExternalSheet($newSheet);
the addExternalSheet() method ensures that all styles and structure (merged cells) are copied cleanly with the worksheet when it is added. Simply using the addSheet() method isn't guaranteed to copy that information correctly
Related
I have a template excel sheet saved as template.xls.I'm trying to get it's copy of the template sheet into another workbook. below is my code ,
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::
load('report/dailyReport_Line'.$_REQUEST['line'].'_'.$year.'_'.$month.'.xls');
$template = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xls');
$clonedWorksheet = clone $template->getSheetByName('template');
$clonedWorksheet->setTitle($date);
$spreadsheet->addSheet($clonedWorksheet);
when i go to the corresponding $spreadsheet file,the copied excel file is not updated. I'll appreciate if someone could assist me to solve it
I have added a xlsx template loaded.
$Reader = new PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $Reader->load($template_path);
I would like to clone it after I have filled it with data.
PhpSpreadsheet Documentation explains how to clone a template with data, but I only need the template in a new sheet without data.
I use the asimlqt/php-google-spreadsheet-client to read google spreadsheets and to write in google spreadsheets.
There are functions to update and delete the actual worksheet but not to empty the worksheet before i write data in it.
Knows somebody who use this library a way to empty the worksheet before writing in? If not you can not be sure that old data is still available
My personal solution is delete the worksheet and recreate
// service
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
// retrieve a list of worksheets from a spreadsheet
$spreadsheet = $spreadsheetFeed->getByTitle('Spreadsheet');
$worksheetFeed = $spreadsheet->getWorksheetFeed();
// get the worksheet you want to empty
$worksheet = $worksheetFeed->getByTitle('EmptySheet');
// delete the worksheet
$worksheet->delete();
// recreate the worksheet to be sure it's empty
$spreadsheet->addWorksheet('EmptySheet', rows, column);
Edit
Or use the batchUpdate with empty param userEnteredValue. Look at https://developers.google.com/sheets/api/samples/sheet#clear_a_sheet_of_all_values_while_preserving_formats
I need to copy the content of one sheet in an Excel Workbook to a sheet in a new excel workbook. The issue is, I have no idea what the sheets contain or their formatting. However, it will only be the first sheet every time.
I've tried an approach but I run out of memory every time. So I thought I'd do it row by row for 100 000 rows. Anyway, I started with a row, and it gets the data, but no luck in writing it to a new sheet. How would I do that?
Here is what I have so far:
// Open the current worksheet
App::import('Vendor', 'PHPExcel');
if (!class_exists('PHPExcel')) {
throw new CakeException('Vendor class PHPExcel not found!');
$this->xls = PHPExcel_IOFactory::load($path);
// Read all content
$this->xls->getActiveSheet()->rangeToArray('A1:ZZZZ1');
// Close current worksheet
$this->xls->disconnectWorksheets();
// Delete worksheet
unlink($destination);
// Open new worksheet
$this->xls = new PHPExcel();
$newWorksheet = new PHPExcel_Worksheet($this->xls, 'Sheet 1');
$this->xls->addSheet($newWorksheet);
$this->xls->setActiveSheetIndexByName('Sheet 1');
// Write all the content
$this->xls->getActiveSheet()->fromArray($array,null,'A1');
// Save current worksheet
$objWriter = PHPExcel_IOFactory::createWriter($this->xls, 'Excel2007');
$objWriter->save($destination);
What am I doing wrong?
Then also, is there an easier way to do this? I don't want to write all this code and in the end it's all reinventing the wheel?
Thanks in advance
There's a built-in method that's specifically written to do this for you:
$objPHPExcel1 = PHPExcel_IOFactory::load($path);
$objPHPExcel2 = new PHPExcel();
// Copy active worksheet from $objPHPExcel1 to $objPHPExcel2
$worksheet = $objPHPExcel1->getActiveSheet();
$objPHPExcel2->addExternalSheet($worksheet)
and if you're running out of memory, building large arrays in memory to try and copy manually isn't going to help.
There are approaches designed to reduce memory such as cell caching, look to use those to reduce memory usage
I'm using PHPExcel to dynamically generate order receipts.
I'd like to be able to generate a "summary" Excel file, containing all the order receipts (one per worksheet).
Is there a way to "join" two (or more) Excel documents into one with PHPExcel ?
In Excel, "tabs" are known as "worksheets". You can create an Excel workbook with multiple worksheets in PHPExcel.
For reference, another answer on SO has an easy-to-follow guide on how to add additional Worksheets to an existing workbook.
This post on Codeplex has an example of adding an external sheet. I'm not sure all of the renaming dance is needed though. (The Codeplex link encourages you to clone the worksheet and copy the cloned sheet so as not to remove it from the original workbook, but I don't think that would be an issue unless you're writing output to the source workbook.) I think something like this should work:
function getReceiptWorksheet( $receiptNumber ) {
// Do something here to retrieve & return your existing receipt
}
function createMasterWorkbook( $filename, $receiptNumbers ) {
$workbook= new PHPExcel();
foreach( $receiptNumbers as $receiptNumber ){
$worksheet = getReceiptWorksheet( $receiptNumber )
$workbook->addExternalSheet( $worksheet );
}
$objWriter = new PHPExcel_Writer_Excel2007($workbook);
$objWriter->save($filename);
}
Then you could just call createMasterWorkbook( 'receipts.xlsx', array( 1, 2, 3 ) );.