Laravel excel 导出格式不正确
Laravel excel not exporting in correct format
我目前有一个我构建的数组,它像这样转储:
0 => array:11 [▼
"category_code" => "123"
"category_name" => "Testing"
"category_description" => "This is a test category"
19738 => array:5 [▼
"identifier" => "720368842943"
"description" => Test Description One
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1129.00"
]
]
19739 => array:5 [▼
"identifier" => "720368844121"
"description" => "Test Description Two"
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1490.00"
]
]
我正在使用 laravel excel 以便将其导出为 excel 文件,但它并没有完全按照我的预期工作
当它导出到 excel 时,我只得到顶级信息:
123 | Testing | This is a test category
但我想将该信息作为 header 获取,然后将该类别的每个后续产品作为一行获取,因此对于上面的示例,它看起来像:
123 | Testing | This is a test category
====================================================================================================================
19738 | 720368842943 | Test Description One | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1129.00
19739 | 720368844121 | Test Description Two | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1490.00
这是我正在使用的数组的 excel 代码,上面转储了:
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["category_code"] = $category->category_code;
$categoryItem["category_name"] = $category->category_name;
$categoryItem["category_desc"] = $category->category_desc;
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->sku_info->identifier;
$skuItem["description"] = $sku->sku_info->item->description;
$skuItem["count"] = $sku->sku_info->item->item_type->count;
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}
$skuItem["prices"] = get_object_vars($sku->prices);
$itemCode = $sku->sku_info->item->item_code;
$categoryItem[$itemCode] = $skuItem;
}
$allCategoryResult[] = $categoryItem;
}
$name = 'Test Export';
$build = Excel::create($name, function ($excel) use ($allCategoryResult) {
$excel->setTitle('Test Export');
$excel->sheet('Test Export', function ($sheet) use ($allCategoryResult) {
$sheet->fromArray($allCategoryResult);
我猜(这只是一个猜测)header 一代让你失望了。
尝试操作您的数据,使每一列都具有相同的索引(注意:代码未经测试,您可能需要更正它):
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["column1"] = $category->category_code;
$categoryItem["column2"] = $category->category_name;
$categoryItem["column3"] = $category->category_desc;
array_push($allCategoryResult, $categoryItem);
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["column1"] = $sku->sku_info->identifier;
$skuItem["column2"] = $sku->sku_info->item->description;
$skuItem["column3"] = $sku->sku_info->item->item_type->count;
/* We leave that one out for the start
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}*/
$skuItem["column4"] = get_object_vars($sku->prices);
array_push($allCategoryResult, $skuItem);
}
}
这应该会为您提供一个包含如下数据的数组:
Array(
Array(
['column1'] = ...
['column2'] = ...
...
),
Array(
['column1'] = ...
['column2'] = ...
...
)
)
如果这对您的 excel 有任何改变,请通知我。那将是对图书馆的基本了解,这将有助于我们为您提供帮助。
要回答您的评论,可以在您的 sheet
和 excel
object 上调用本机 phpExcel
函数。所以你可以用它来格式化一行粗体:
$sheet->->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);
请阅读 phpExcel
以了解 laravel excel
的真正作用,这将对您有很大帮助
我目前有一个我构建的数组,它像这样转储:
0 => array:11 [▼
"category_code" => "123"
"category_name" => "Testing"
"category_description" => "This is a test category"
19738 => array:5 [▼
"identifier" => "720368842943"
"description" => Test Description One
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1129.00"
]
]
19739 => array:5 [▼
"identifier" => "720368844121"
"description" => "Test Description Two"
"count" => 4
"details" => array:2 [▼
0 => array:3 [▼
"detail_code" => "2751"
"detail_code2" => "43"
"detail_specifier" => "Detail One"
]
1 => array:3 [▼
"detail_code" => "2681"
"detail_code2" => "9"
"detail_specifier" => "Detail Two"
]
]
"prices" => array:1 [▼
"01" => "1490.00"
]
]
我正在使用 laravel excel 以便将其导出为 excel 文件,但它并没有完全按照我的预期工作
当它导出到 excel 时,我只得到顶级信息:
123 | Testing | This is a test category
但我想将该信息作为 header 获取,然后将该类别的每个后续产品作为一行获取,因此对于上面的示例,它看起来像:
123 | Testing | This is a test category
====================================================================================================================
19738 | 720368842943 | Test Description One | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1129.00
19739 | 720368844121 | Test Description Two | 4 | 2751 | 43 | Detail One | 2681 | 9 | Detail Two | 1490.00
这是我正在使用的数组的 excel 代码,上面转储了:
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["category_code"] = $category->category_code;
$categoryItem["category_name"] = $category->category_name;
$categoryItem["category_desc"] = $category->category_desc;
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["identifier"] = $sku->sku_info->identifier;
$skuItem["description"] = $sku->sku_info->item->description;
$skuItem["count"] = $sku->sku_info->item->item_type->count;
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}
$skuItem["prices"] = get_object_vars($sku->prices);
$itemCode = $sku->sku_info->item->item_code;
$categoryItem[$itemCode] = $skuItem;
}
$allCategoryResult[] = $categoryItem;
}
$name = 'Test Export';
$build = Excel::create($name, function ($excel) use ($allCategoryResult) {
$excel->setTitle('Test Export');
$excel->sheet('Test Export', function ($sheet) use ($allCategoryResult) {
$sheet->fromArray($allCategoryResult);
我猜(这只是一个猜测)header 一代让你失望了。 尝试操作您的数据,使每一列都具有相同的索引(注意:代码未经测试,您可能需要更正它):
$allCategoryResult= array();
foreach($prices->categories as $category){
$categoryItem = array();
$categoryItem["column1"] = $category->category_code;
$categoryItem["column2"] = $category->category_name;
$categoryItem["column3"] = $category->category_desc;
array_push($allCategoryResult, $categoryItem);
foreach($category->skus as $sku){
$skuItem = array();
$skuItem["column1"] = $sku->sku_info->identifier;
$skuItem["column2"] = $sku->sku_info->item->description;
$skuItem["column3"] = $sku->sku_info->item->item_type->count;
/* We leave that one out for the start
$skuItem["details"] = array();
foreach ($sku->sku_info->details as $details) {
$detailsItem = array();
$detailsItem["detail_code"] = $details->detail_code;
$detailsItem["detail_code2"] = $details->detail_code2;
$detailsItem["detail_specifier"] = $details->detail_specifier;
$skuItem["details"][] = $detailsItem;
}*/
$skuItem["column4"] = get_object_vars($sku->prices);
array_push($allCategoryResult, $skuItem);
}
}
这应该会为您提供一个包含如下数据的数组:
Array(
Array(
['column1'] = ...
['column2'] = ...
...
),
Array(
['column1'] = ...
['column2'] = ...
...
)
)
如果这对您的 excel 有任何改变,请通知我。那将是对图书馆的基本了解,这将有助于我们为您提供帮助。
要回答您的评论,可以在您的 sheet
和 excel
object 上调用本机 phpExcel
函数。所以你可以用它来格式化一行粗体:
$sheet->->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);
请阅读 phpExcel
以了解 laravel excel
的真正作用,这将对您有很大帮助