spadeX
1
How can I use the following classes into controller ?
Their is no composer to use that classes
https://github.com/nuovo/spreadsheet-reader
I use the Use
use vendor\phpreader\excel_reader2;
use vendor\phpreader\SpreadsheetReader;
But get an error class not found
I included those file into same folder structure.
Better use PhpSpreadsheed, it has better support
1 Like
spadeX
3
Already used PhpSpreadsheed, but can’t save that array into database. Here is code
$contractor = $this->Contractors->newEntity();
$contractor = $this->Contractors->patchEntity($contractor,$namedDataArray);
what is $namedDataArray???
I have something like this
$columns = [
'name' => 1,
'title' => 2,
'slug' => 3,
];
foreach ($columns as $colName => $colPos) {
$article[$colName] = $worksheet
->getCellByColumnAndRow($colPos + 1, $i)
->getCalculatedValue();
}
$this->Articles->save($article);
The array is showing as
Array
(
[1] => Array
(
[user_name] => xyz@gmail.com
[password] => 1234
[company_name] => dell
[company_tin] => stack3
[addressline_1] => View street
[addressline_2] => mountain rd
[city] => PN
[state_id] => PN
[country_id] => 3
[State_id] => 5
[zip] => 78445
[pri_contact_fn] => Vinay
[pri_contact_ln] => Patil
[pri_contact_title] => President
[pri_contact_pn] => 25655644
[is_safety_sensitive] => 1
)
)
///// This is my code ///
public function readexcel()
{
$contractor = $this->Contractors->newEntity();
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load(EXPORT_IMG.'demoCont.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$header=true;
if ($header) {
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$headingsArray = $worksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach ($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
// pr($contractor);
}
}
}
} else {
//excel sheet with no header
$namedDataArray = $worksheet->toArray(null, true, true, true);
}
$contractor = $this->Contractors->patchEntity($contractor, $this->request->getData($namedDataArray));