-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcel.php
More file actions
70 lines (64 loc) · 2.84 KB
/
Excel.php
File metadata and controls
70 lines (64 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Framework\Utilities;
/**
* This class provided functions for working with Excel files
* It uses PhpExcel library (https://github.com/PHPOffice/PHPExcel)
*
* @category UtilityClass
* @author Nadir Latif <nadir@pakjiddat.pk>
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General public License, version 2
*/
final class Excel
{
/** @var Excel $instance The single static instance */
protected static $instance;
/**
* Used to return a single instance of the class
*
* Checks if instance already exists. If it does not exist then it is created. The instance is returned
*
* @param array $parameters an array containing class parameters
*
* @return Excel static::$instance name the instance of the correct child class is returned
*/
public static function GetInstance(array $parameters) : Excel
{
if (static::$instance == null) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Reads the contents of an excel file
*
* This function reads the contents of the active excel file worksheet
* It reads the contents of the cells given by the start_cell and end_cell parameters
* It returns the data as an array
*
* @param string $file_path absolute path to the excel file to be read
* @param string $start_cell the cell co-ordinates of start cell. e.g A1
* @param string $end_cell the cell co-ordinates of end cell. e.g F6
*
* @return array returns the contents of the excel file
*/
public function ReadExcelFile(string $file_path, string $start_cell, string $end_cell) : array
{
$input_file_type = 'Excel5';
$input_file_name = $file_path;
/** Create a new Reader of the type defined in $inputFileType */
$excel_reader = \PHPExcel_IOFactory::createReader($input_file_type);
/** Advise the Reader that we only want to load cell data */
$excel_reader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object */
$excel_file_obj = $excel_reader->load($input_file_name);
/** Read data from worksheet */
$excel_data = $excel_file_obj->getActiveSheet()->rangeToArray($start_cell . ':' . $end_cell, // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
TRUE, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
TRUE, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
TRUE // Should the array be indexed by cell row and cell column
);
return $excel_data;
}
}