Skip to content

Commit

Permalink
Add save() method with demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yidas committed Mar 10, 2018
1 parent a4afa3a commit 7333335
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions demo/save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

require __DIR__ . '/_config.php';

$filepath = \yidas\phpSpreadsheet\Helper::newSpreadsheet()
->addRow(['Add A1'])
->save("/tmp/save");

echo "Successful Wrtie to: {$filepath}";
30 changes: 28 additions & 2 deletions src/phpSpreadsheet/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public static function addRows($data)
}

/**
* Output an Excel file
* Output file to browser
*
* @param string $filename
* @param string $format
Expand All @@ -351,7 +351,7 @@ public static function output($filename='excel', $format='Xlsx')
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPhpSpreadsheet, $format);

/**
* @todo Type Mapping
* Type Mapping
*/
$inTypeList = isset(self::$_writerTypeInfo[$format]) ? true : false;
$extension = ($inTypeList) ? self::$_writerTypeInfo[$format]['extension'] : '';
Expand All @@ -368,6 +368,32 @@ public static function output($filename='excel', $format='Xlsx')
exit;
}

/**
* Save as file
*
* @param string $filename Support file path
* @param string $format
* @return string Filepath
*/
public static function save($filename='excel', $format='Xlsx')
{
$objPhpSpreadsheet = self::validExcelObj();

// Create Writer first
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPhpSpreadsheet, $format);

/**
* Type Mapping
*/
$inTypeList = isset(self::$_writerTypeInfo[$format]) ? true : false;
$extension = ($inTypeList) ? self::$_writerTypeInfo[$format]['extension'] : '';
$filepath = "{$filename}{$extension}";

// Save file
$objWriter->save($filepath);
return $filepath;
}

/**
* Get data of a row from the actived sheet of PhpSpreadsheet
*
Expand Down

0 comments on commit 7333335

Please sign in to comment.