在PHPExcel中创建动态列

时间:2019-01-03 12:21:25

标签: php phpexcel

我有很多产品,每个都有规格。一种产品可能具有1个规格,而其他产品可能具有5个规格,但名称和值有所不同。如何制作动态列? 这是我的代码: 首先,我找到所有产品类别,而不是所有产品,并将它们写在表中。

  $mainCats = Page::find()->where('is_cat = 1 AND excel_cat = 1')->orderBy(['sort' => SORT_ASC])->all();
        if($mainCats){
            foreach($mainCats as $i => $mainCat){

                //add style for heading row of table
                $styleArray = array(
                    'font'  => array(
                        'bold'  => true,
                        'color' => array('rgb' => 'FFFFFF'),
                        'size'  => 10,
                        'name'  => 'Verdana'
                    ));
                // Add new sheet
                $objWorkSheet = $objPHPExcel->createSheet($i); //Setting index when creating

                $objWorkSheet->getStyle("A1:Z300")->getFont()->setSize(10);
                $objWorkSheet->getStyle('A2:W2')->applyFromArray($styleArray);
                $objWorkSheet->getStyle("A1:Z300")->getFont()->setName('Verdana');
                //Write cells
                $objWorkSheet->mergeCells('A1:D1');
                $objWorkSheet->getStyle('A2:W2')->getFill()
                    ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('C00000');
                $objWorkSheet->setCellValue('A1', $mainCat->title);

                $objWorkSheet->getStyle('A1')->getFill()->getStartColor()->setRGB('C00000');

                $objWorkSheet->setCellValue('A2', "Номер")
                    ->setCellValue('B2', "Модел")
                    ->setCellValue('C2', "Категория");


                $letter = "D";
                if(isset($_POST['attributes']) and $_POST['attributes'] != ""){
                    foreach($_POST['attributes'] as $k => $attribute_id){
                        $k++;
                        $attribute = Attributes::findOne($attribute_id);
                        if($attribute){
                            $objWorkSheet->setCellValue($letter.'2', $attribute->name);
                            $letter++;
                        }
                    }
                }
                $objWorkSheet->setCellValue($letter.'2', "Цена 3");
                $objWorkSheet->setCellValue(++$letter.'2', "Цена");

                $products = Product::find()->joinWith('translation')->where('product.active = 1 AND product.quantity > 0 AND product.edit_mw = 1 AND product.excel_category_id = '.$mainCat->id.' ')->orderBy('product.price_online ASC')->all();
                $key = 3;
                if($products){
                    foreach($products as $product){
                        $objWorkSheet->setCellValue('A'.$key, $product->artic_number2)
                            ->setCellValue('B'.$key, $product->title)
                            ->setCellValue('C'.$key, $mainCat->title);
                        //change the data type of the cell
                        $url = ((strpos( $_SERVER['SERVER_NAME'], 'pclife.bg' ) !== false ) ? 'https://' : 'http://').$_SERVER['SERVER_NAME'].'/bg' .$this->myUrlEncode(Yii::$app->makeUrl->makeProductUrl($product->id));
                        $objWorkSheet->getCell('B'.$key)->getHyperlink()->setUrl($url);
                        $objWorkSheet->getCell('B'.$key)->setDataType(\PHPExcel_Cell_DataType::TYPE_STRING2);
                        ///now set the link
                        $letter = "D";
                        if(isset($_POST['attributes']) and $_POST['attributes'] != ""){
                            foreach($_POST['attributes'] as $attribute_id){
                                $attribute = Attributes::findOne($attribute_id);
                                if($attribute){
                                    $objWorkSheet->setCellValue($letter.$key, $product->getAttributeValue($attribute_id));
                                    $letter++;
                                    $objWorkSheet->setCellValue($letter.$key, $attribute->name);
                                }
                            }
                        }
                        $objWorkSheet->setCellValue($letter.$key, sprintf("%.2f", (($product->isPromoPriceThree()) ? $product->getPromoPriceThree() : $product->price_online_gr3)).' лв.');
                        $objWorkSheet->setCellValue(++$letter.$key, sprintf("%.2f", (($product->isPromoPriceOne()) ? $product->getPromoPriceOne() : $product->price_online)).' лв.');
                        $key++;
                    }
                }
                // Rename sheet
                $objWorkSheet->setTitle("$mainCat->title");

                $endLetter = 'A';
                if(isset($_POST['attributes']) and $_POST['attributes'] != ""){
                    for($startLetter = 0; $startLetter !== (5 + count($_POST['attributes'])); $startLetter++) {
                        $endLetter++;
                    }
                }else{
                    for($startLetter = 0; $startLetter !== 5; $startLetter++) {
                        $endLetter++;
                    }
                }

                for($col = 'A'; $col !== $endLetter; $col++) {
                    $objWorkSheet->getColumnDimension($col)
                        ->setAutoSize(true);
                }
            }
        }

我不明白我在哪里错了...

0 个答案:

没有答案