简体   繁体   中英

phpspreadsheet opening excel template, writing lines over existing sheet lines and saving to new file

I am very new to phpspreadsheet and am carefully working through how to open an existing two sheet template, write over the data on one sheet, and then save the two sheet workbook to a new file.

Sheet one is "Raw Data" that feeds into "Chart Data" on sheet two. I need to over write all the data on sheet "Raw Data" (starting with Row 1, column A) with the hopes that that will simply update charts that exist on the "Chart Data" sheet.

So far, I can't really troubleshoot any thing because I can't even get the basics of being able to write to a new file. I keep getting the following error:

PHP Fatal error: Uncaught TypeError: Argument 1 passed to PhpOffice\PhpSpreadsheet\IOFactory::createWriter() must be an instance of PhpOffice\PhpSpreadsheet\Spreadsheet, instance of PhpOffice\PhpSpreadsheet\Worksheet\Worksheet given

My code was originally similar to the OP's code here: How to write on an exiting spreadsheet with PHPSpreadsheet? , his error was similar to mine, so I recoded to match the Answer in this same question but I am still getting the same error.

require "/home/myDir/php/vendor/autoload.php";

$baseDir = "/home/myDir" ;

$vendorTempName = "$baseDir/reports/VendorReportTemplate.xlsx" ;
$vendorTempReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx") ;
$vendorTempFile = $vendorTempReader->load($vendorTempName, \PhpOffice\PhpSpreadsheet\Reader\IReader::LOAD_WITH_CHARTS) ;

... gather all my data into a massive array, pass array to write function
... I am using this to create multiple files, some just manually created csv and text files
... one of which is outputting to a new XLSX file from the templatefile.

writeFiles($dataArray) ; // a multidimensional array

function writeFiles($dArray) {
   global $baseDir, $vendorTempFile ;

   // do some stuff
   foreach ($dArray as $d) {
      // do some more stuff
      if ($d["type"] == 1) {  // new vendor 
         $vendorTempSheet = $vendorTempFile->getSheetByName("Raw Data") ;
         $vendorTempSheet->fromArray($d["Chart1"]["Header"]) ;  // single line
         $vendorTempSheet->fromArray($d["Chart1"]["Data"]) ;    // single line
         $vendorTempSheet->fromArray(array()) ;  // empty line

         $vendorTempSheet->fromArray($d["Chart2"]["Header"]) ;   // single line
         $vendorTempSheet->fromArray($d["Chart2"]["Data"]) ;   // single line
         $vendorTempSheet->fromArray(array()) ;  // empty line

         ...

         $vendorTempSheet->fromArray($d["Chart10"]["Header"]) ;  // single line
         $vendorTempSheet->fromArray($d["Chart10"]["Data"]) ;   // single line
         $vendorTempSheet->fromArray(array()) ;  // empty line

         $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($vendorTempSheet, 'Xlsx');
         $writer->setIncludeCharts(true);
         $writer->save($baseDir.'files/vendor/' .$d['vendorID']. '/newReport.xlsx');
      }
      // do other stuff
   }
}

The charts on sheet two are binding the data as follows:

   Chart1 = 'Raw Data' Rows 1 & 2
   Chart2 = 'Raw Data' Rows 4 & 5
   Chart3 = 'Raw Data' Rows 7 & 8
   ...
   Chart 10 = 'Raw Data' Rows 28 & 29

The output file should be a new two sheet xlsx workbook, sheet one is the Raw Data for that vendor, and sheet two ( Chart Data ) should be all the updated, preformatted charts from the original template file. But I can't even get a file to write to see if any of my methods are working because of the above error.

A good nights rest can do the trick. I guess I kept getting my variables mixed up because they were all very similar. Either that or I kept glossing over the fact that the original referenced template/file needs to be written instead of the referred sheet.

I need to be writing the instance of the opened file, not the instance of the referred sheet.

In my code above I was writing $vendorTempSheet when I needed to be writing $vendorTempFile .

     $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($vendorTempFile, 'Xlsx');
     $writer->setIncludeCharts(true);
     $writer->save($baseDir.'files/vendor/' .$d['vendorID']. '/newReport.xlsx');

Now that I am passed that I can begin troubleshooting all the other things that are throwing errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM