SpreadSheetMR is a simple multiformat spreadsheet reader.
- Read XLSX, XLS, CSV or TXT files
- Combine header with data
- Ignore row or columns
This library uses shuchkin/SimpleXLS
and shuchkin/SimpleXLSX
for read Excel files.
First, you will need to install Composer. Then, run the following command:
$ composer install parkejunior/spreadsheetmr
Here is a basic example of using the library:
use SpreadSheetMR\SpreadSheetMR;
$path_to_file = "file.csv";
$file_extension = ".csv"; // or only "csv"
$import = new SpreadSheetMR($path_to_file, $file_extension);
$data = $import->getObject();
var_dump($data);
The getObject
method get data formatted using stdClass
.
Note that the file extension is passed as a separate property from the file path, because the path can be temporary like the superglobal $_FILES['file']['tmp_name']
.
You can use the verifyFile()
method by passing an array with some settings. Example:
...
$import->verifyFile(array(
"first_title" => "name", // check if first title on header is "name"
"last_title" => "phone", // check if last title on header is "phone"
"total_columns" => 4 // check if total columns on header is 4
));
$data = $import->getObject();
var_dump($data);
It is also possible to ignore columns or rows using the ignoreRow()
and ignoreColumn()
methods by passing the index offset as a parameter. Example:
...
$import->ignoreRow(3); // ignore 4th row
$import->ignoreColumn(0); // ignore first column
$data = $import->getObject();
var_dump($data);
You can define which line is the header by passing to the headerIndex
property the index offset of the line. Note that when the header is defined, the getObject ()
method returns a stdClass
combining the header as an association to the data of each line. Example:
...
$import->headerIndex = 0; // define first row as header
$data = $import->getObject();
var_dump($data);
If you find any incorrect English grammar or any suggestions on how to improve the library, I appreciate it.