Exemple #1
0
 private static function ExcelApplyValues($sheet, $rows, $params = [])
 {
     /* Границы таблицы */
     $ramka = array('borders' => array('allborders' => array('style' => \PHPExcel_Style_Border::BORDER_THIN)));
     /* Жирный шрифт для шапки таблицы */
     $font = array('font' => array('bold' => true), 'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER));
     $Matlog = new Matlog();
     $Employeelog = new Employeelog();
     $Traflog = new Traflog();
     $attributeslabels = array_merge($Matlog->attributeLabels(), $Employeelog->attributeLabels(), $Traflog->attributeLabels());
     if (count($rows) > 0) {
         $col = 0;
         foreach (array_keys($rows[0]) as $attr) {
             if (!in_array($attr, ['idmatlog', 'idemployeelog'])) {
                 $sheet->setCellValueByColumnAndRow($col, 1, $attributeslabels[$attr]);
                 $sheet->getStyleByColumnAndRow($col, 1)->applyFromArray($ramka);
                 $sheet->getStyleByColumnAndRow($col, 1)->applyFromArray($font);
                 $col++;
             }
         }
     }
     if (count($rows) > 0) {
         foreach ($rows as $i => $row) {
             $col = 0;
             foreach ($row as $attr => $value) {
                 if (!in_array($attr, ['idmatlog', 'idemployeelog'])) {
                     if (isset($params['date']) && in_array($attr, $params['date']) && $value !== NULL) {
                         $rows[$i][$attr] = date('d.m.Y', strtotime($value));
                     }
                     if (isset($params['datetime']) && in_array($attr, $params['datetime']) && $value !== NULL) {
                         $rows[$i][$attr] = date('d.m.Y H:i:s', strtotime($value));
                     }
                     if (isset($params['case']) && in_array($attr, array_keys($params['case']))) {
                         $rows[$i][$attr] = $params['case'][$attr][$value];
                     }
                     $sheet->getStyleByColumnAndRow($col, $i + 2)->applyFromArray($ramka);
                     if (isset($params['string']) && in_array($attr, $params['string'])) {
                         $sheet->getStyleByColumnAndRow($col, $i + 2)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                         $sheet->setCellValueExplicitByColumnAndRow($col, $i + 2, $rows[$i][$attr], \PHPExcel_Cell_DataType::TYPE_STRING);
                     } else {
                         $sheet->setCellValueByColumnAndRow($col, $i + 2, $rows[$i][$attr]);
                     }
                     $col++;
                 }
             }
         }
     }
     if (count($rows) > 0) {
         $c = count(array_keys($rows[0]));
         // ??????????????????
         /* Авторазмер колонок Excel */
         \PHPExcel_Shared_Font::setAutoSizeMethod(\PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
         foreach (range(0, $c) as $col) {
             $sheet->getColumnDimensionByColumn($col)->setAutoSize(true);
             $sheet->calculateColumnWidths();
             if ($sheet->getColumnDimensionByColumn($col)->getWidth() > 70) {
                 $sheet->getColumnDimensionByColumn($col)->setAutoSize(false);
                 $sheet->getColumnDimensionByColumn($col)->setWidth(70);
             }
         }
     }
 }