mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 14:58:42 +00:00
bug fix spreadsheet IO
This commit is contained in:
parent
5256e40989
commit
fd768a208c
|
|
@ -48,28 +48,14 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param ConnectionAbstract $con Database connection
|
* @param ConnectionAbstract $con Database connection
|
||||||
|
* @param string $path File path
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public function __construct(ConnectionAbstract $con)
|
public function __construct(ConnectionAbstract $con, string $path)
|
||||||
{
|
|
||||||
$this->con = $con;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add path
|
|
||||||
*
|
|
||||||
* This is the path of the source data in case of inserting/updating data or the destination file for selecting data.
|
|
||||||
*
|
|
||||||
* @param string $path File path
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
public function setPath(string $path) : void
|
|
||||||
{
|
{
|
||||||
|
$this->con = $con;
|
||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,9 +68,9 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
||||||
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
||||||
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
||||||
$sheet = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
|
||||||
} else {
|
} else {
|
||||||
$sheet = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
||||||
}
|
}
|
||||||
|
|
||||||
$reader->setReadDataOnly(true);
|
$reader->setReadDataOnly(true);
|
||||||
|
|
@ -100,8 +86,9 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
|
|
||||||
// get column titles
|
// get column titles
|
||||||
$column = 1;
|
$column = 1;
|
||||||
while (!empty($value = $workSheet->getCellByColmnAndRow($column, 1)->getValue())) {
|
while (!empty($value = $workSheet->getCellByColumnAndRow($column, 1)->getCalculatedValue())) {
|
||||||
$titles[] = $value;
|
$titles[] = $value;
|
||||||
|
++$column;
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns = \count($titles);
|
$columns = \count($titles);
|
||||||
|
|
@ -111,12 +98,14 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
$query->insert(...$titles)->into($table);
|
$query->insert(...$titles)->into($table);
|
||||||
|
|
||||||
$line = 2;
|
$line = 2;
|
||||||
while (!empty($row = $workSheet->getCellByColumnAndRow(1, $line)->getValue())) {
|
while (!empty($row = $workSheet->getCellByColumnAndRow(1, $line)->getCalculatedValue())) {
|
||||||
$cells = [];
|
$cells = [];
|
||||||
for ($j = 1; $j <= $columns; ++$j) {
|
for ($j = 1; $j <= $columns; ++$j) {
|
||||||
$cells[] = $workSheet->getCellByColumnAndRow(j, $line)->getValue();
|
$cells[] = $workSheet->getCellByColumnAndRow($j, $line)->getCalculatedValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++$line;
|
||||||
|
|
||||||
$query->values(...$cells);
|
$query->values(...$cells);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,7 +118,7 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
*/
|
*/
|
||||||
public function select(array $queries) : void
|
public function select(array $queries) : void
|
||||||
{
|
{
|
||||||
$sheet = new Spreadsheet();
|
$sheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||||
$sheet->getProperties()
|
$sheet->getProperties()
|
||||||
->setCreator('Orange-Management')
|
->setCreator('Orange-Management')
|
||||||
->setLastModifiedBy('Orange-Management')
|
->setLastModifiedBy('Orange-Management')
|
||||||
|
|
@ -158,23 +147,28 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
|
|
||||||
// set column titles
|
// set column titles
|
||||||
for ($i = 1; $i <= $colCount; ++$i) {
|
for ($i = 1; $i <= $colCount; ++$i) {
|
||||||
$workSheet->setCellValueByColumnAndRow($i, 1, $columns[0][$i - 1]);
|
$workSheet->setCellValueByColumnAndRow($i, 1, $columns[$i - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set data
|
// set data
|
||||||
foreach ($results as $key => $result) {
|
$row = 2;
|
||||||
for ($i = 1; $i <= $colCount; ++$i) {
|
foreach ($results as $result) {
|
||||||
$workSheet->setCellValueByColumnAndRow($i, $key + 1, $result[$i - 1]);
|
$col = 1;
|
||||||
|
foreach ($result as $value) {
|
||||||
|
$workSheet->setCellValueByColumnAndRow($col, $row, $value);
|
||||||
|
++$col;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++$row;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
||||||
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx())->save($this->path);
|
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($sheet))->save($this->path);
|
||||||
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
||||||
(new \PhpOffice\PhpSpreadsheet\Writer\Ods())->save($this->path);
|
(new \PhpOffice\PhpSpreadsheet\Writer\Ods($sheet))->save($this->path);
|
||||||
} else {
|
} else {
|
||||||
(new \PhpOffice\PhpSpreadsheet\Writer\Xls())->save($this->path);
|
(new \PhpOffice\PhpSpreadsheet\Writer\Xls($sheet))->save($this->path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -187,9 +181,9 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
if (StringUtils::endsWith($this->path, '.xlsx')) {
|
||||||
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
||||||
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
} elseif (StringUtils::endsWith($this->path, '.ods')) {
|
||||||
$sheet = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
|
||||||
} else {
|
} else {
|
||||||
$sheet = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
||||||
}
|
}
|
||||||
|
|
||||||
$reader->setReadDataOnly(true);
|
$reader->setReadDataOnly(true);
|
||||||
|
|
@ -205,25 +199,27 @@ class ExcelDatabaseMapper implements IODatabaseMapper
|
||||||
|
|
||||||
// get column titles
|
// get column titles
|
||||||
$column = 1;
|
$column = 1;
|
||||||
while (!empty($value = $workSheet->getCellByColmnAndRow($column, 1)->getValue())) {
|
while (!empty($value = $workSheet->getCellByColumnAndRow($column, 1)->getCalculatedValue())) {
|
||||||
$titles[] = $value;
|
$titles[] = $value;
|
||||||
|
++$column;
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns = \count($titles);
|
$columns = \count($titles);
|
||||||
|
|
||||||
// insert data
|
// update data
|
||||||
$line = 2;
|
$line = 2;
|
||||||
while (!empty($row = $workSheet->getCellByColumnAndRow(1, $line)->getValue())) {
|
while (!empty($row = $workSheet->getCellByColumnAndRow(1, $line)->getCalculatedValue())) {
|
||||||
$query = new Builder($this->con);
|
$query = new Builder($this->con);
|
||||||
$query->update(...$titles)->into($table);
|
$query->update($table)->into($table);
|
||||||
|
|
||||||
$cells = [];
|
for ($j = 2; $j <= $columns; ++$j) {
|
||||||
for ($j = 1; $j <= $columns; ++$j) {
|
$query->sets($titles[$j - 1], $workSheet->getCellByColumnAndRow($j, $line)->getCalculatedValue());
|
||||||
$cells[] = $workSheet->getCellByColumnAndRow(j, $line)->getValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->values(...$cells)->where($titles[0], '=', $cells[0]);
|
$query->where($titles[0], '=', $workSheet->getCellByColumnAndRow(1, $line)->getCalculatedValue());
|
||||||
$query->execute();
|
$query->execute();
|
||||||
|
|
||||||
|
++$line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user