Advertisement
Guest User

Example PHPExcel Template

a guest
Nov 27th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Mozart
  5.  * Date: 28.11.2014
  6.  * Time: 0:18
  7.  */
  8.  
  9. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  10.  
  11. //$template = new PHPExcel_Template("1.xlsx");
  12. //$template->setValue('111', '33333');
  13. //$template->setValue('222', '44444');
  14. //$template->saveAs("test.xlsx");
  15.  
  16. $variables = array(
  17.     'value1' => 'foo',
  18.     'value2' => 'bar',
  19.     'value3' => 'hoge',
  20. );
  21.  
  22. $templateFile = '1.xlsx';
  23. $documentFile = 'test.xlsx';
  24.  
  25. copy($templateFile, $documentFile);
  26.  
  27. $zipArchive = new PHPExcel_Shared_ZipArchive();
  28.  
  29. $zipArchive->open($documentFile);
  30.  
  31. $sharedStrings = $zipArchive->getFromName('xl/sharedStrings.xml');
  32.  
  33. foreach ($variables as $name => $value) {
  34.     $sharedStrings = preg_replace('/(' . preg_quote('${' . $name . '}') . ')/', $value, $sharedStrings);
  35. }
  36.  
  37. $zipArchive->addFromString('xl/sharedStrings.xml', $sharedStrings);
  38.  
  39. $zipArchive->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement