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;
}
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
add a comment |
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
This link could be helpful for you stackoverflow.com/questions/27635604/…
– Shahnewaz
Jan 3 at 12:27
add a comment |
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
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
php phpexcel
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
This link could be helpful for you stackoverflow.com/questions/27635604/…
– Shahnewaz
Jan 3 at 12:27