| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Helpers;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
- class ExcelHelper
- {
- public static function copyRows( Worksheet $sheet, $srcRange, $dstCell, Worksheet $destSheet = null): void
- {
- if( !isset($destSheet)) {
- $destSheet = $sheet;
- }
- if( !preg_match('/^([A-Z]+)(\d+):([A-Z]+)(\d+)$/', $srcRange, $srcRangeMatch) ) {
- // Invalid src range
- return;
- }
- if( !preg_match('/^([A-Z]+)(\d+)$/', $dstCell, $destCellMatch) ) {
- // Invalid dest cell
- return;
- }
- $srcColumnStart = $srcRangeMatch[1];
- $srcRowStart = $srcRangeMatch[2];
- $srcColumnEnd = $srcRangeMatch[3];
- $srcRowEnd = $srcRangeMatch[4];
- $destColumnStart = $destCellMatch[1];
- $destRowStart = $destCellMatch[2];
- $srcColumnStart = Coordinate::columnIndexFromString($srcColumnStart);
- $srcColumnEnd = Coordinate::columnIndexFromString($srcColumnEnd);
- $destColumnStart = Coordinate::columnIndexFromString($destColumnStart);
- $rowCount = 0;
- for ($row = $srcRowStart; $row <= $srcRowEnd; $row++) {
- $colCount = 0;
- for ($col = $srcColumnStart; $col <= $srcColumnEnd; $col++) {
- $cell = $sheet->getCell([$col, $row]);
- $style = $sheet->getStyle([$col, $row]);
- $dstCell = Coordinate::stringFromColumnIndex($destColumnStart + $colCount) . (string)((int)$destRowStart + (int)$rowCount);
- $destSheet->setCellValue($dstCell, $cell->getValue());
- $destSheet->duplicateStyle($style, $dstCell);
- // Set width of column, but only once per column
- if ($rowCount === 0) {
- $w = $sheet->getColumnDimensionByColumn($col)->getWidth();
- $destSheet->getColumnDimensionByColumn ($destColumnStart + $colCount)->setAutoSize(false);
- $destSheet->getColumnDimensionByColumn ($destColumnStart + $colCount)->setWidth($w);
- }
- $colCount++;
- }
- $h = $sheet->getRowDimension($row)->getRowHeight();
- $destSheet->getRowDimension((int)$destRowStart + $rowCount)->setRowHeight($h);
- $rowCount++;
- }
- foreach ($sheet->getMergeCells() as $mergeCell) {
- $mc = explode(":", $mergeCell);
- $mergeColSrcStart = Coordinate::columnIndexFromString(preg_replace("/[0-9]*/", "", $mc[0]));
- $mergeColSrcEnd = Coordinate::columnIndexFromString(preg_replace("/[0-9]*/", "", $mc[1]));
- $mergeRowSrcStart = ((int)preg_replace("/[A-Z]*/", "", $mc[0]));
- $mergeRowSrcEnd = ((int)preg_replace("/[A-Z]*/", "", $mc[1]));
- $relativeColStart = $mergeColSrcStart - $srcColumnStart;
- $relativeColEnd = $mergeColSrcEnd - $srcColumnStart;
- $relativeRowStart = $mergeRowSrcStart - $srcRowStart;
- $relativeRowEnd = $mergeRowSrcEnd - $srcRowStart;
- if (0 <= $mergeRowSrcStart && $mergeRowSrcStart >= $srcRowStart && $mergeRowSrcEnd <= $srcRowEnd) {
- $targetColStart = Coordinate::stringFromColumnIndex($destColumnStart + $relativeColStart);
- $targetColEnd = Coordinate::stringFromColumnIndex($destColumnStart + $relativeColEnd);
- $targetRowStart = (int)$destRowStart + $relativeRowStart;
- $targetRowEnd = (int)$destRowStart + $relativeRowEnd;
- $merge = (string)$targetColStart . (string)($targetRowStart) . ":" . (string)$targetColEnd . (string)($targetRowEnd);
- //Merge target cells
- $destSheet->mergeCells($merge);
- }
- }
- }
- public static function copyStyleXFCollection(Spreadsheet $sourceSheet, Spreadsheet $destSheet) {
- $collection = $sourceSheet->getCellXfCollection();
- foreach ($collection as $key => $item) {
- $destSheet->addCellXf($item);
- }
- }
- }
|