Create dynamic column in PHPExcel





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have many products and each of them have specifications. One product may have 1 specification, but other may be have 5 specification with difference name and values. How to make dynamical columns?
here is my code :
First i find all categories with products, than all products and write them in table.



  $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);
}
}
}


i do not understand where i am wrong ...










share|improve this question

























  • This link could be helpful for you stackoverflow.com/questions/27635604/…

    – Shahnewaz
    Jan 3 at 12:27


















0















I have many products and each of them have specifications. One product may have 1 specification, but other may be have 5 specification with difference name and values. How to make dynamical columns?
here is my code :
First i find all categories with products, than all products and write them in table.



  $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);
}
}
}


i do not understand where i am wrong ...










share|improve this question

























  • This link could be helpful for you stackoverflow.com/questions/27635604/…

    – Shahnewaz
    Jan 3 at 12:27














0












0








0








I have many products and each of them have specifications. One product may have 1 specification, but other may be have 5 specification with difference name and values. How to make dynamical columns?
here is my code :
First i find all categories with products, than all products and write them in table.



  $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);
}
}
}


i do not understand where i am wrong ...










share|improve this question
















I have many products and each of them have specifications. One product may have 1 specification, but other may be have 5 specification with difference name and values. How to make dynamical columns?
here is my code :
First i find all categories with products, than all products and write them in table.



  $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);
}
}
}


i do not understand where i am wrong ...







php phpexcel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:30









Shahnewaz

394312




394312










asked Jan 3 at 12:21









Jeni VasilevaJeni Vasileva

413115




413115













  • This link could be helpful for you stackoverflow.com/questions/27635604/…

    – Shahnewaz
    Jan 3 at 12:27



















  • This link could be helpful for you stackoverflow.com/questions/27635604/…

    – Shahnewaz
    Jan 3 at 12:27

















This link could be helpful for you stackoverflow.com/questions/27635604/…

– Shahnewaz
Jan 3 at 12:27





This link could be helpful for you stackoverflow.com/questions/27635604/…

– Shahnewaz
Jan 3 at 12:27












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022212%2fcreate-dynamic-column-in-phpexcel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022212%2fcreate-dynamic-column-in-phpexcel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith