多维数组和创建分组数据的 PHPExcel 问题
PHPExcel issue with multidimensional array and creating grouped data
我正在循环的当前数组转储此结构
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"
]
]
但是当我导出到 excel 它只显示三个顶级属性
123 | Testing | This is a test category
我正在尝试以某种方式导出它,以便前 3 个值是一行(如 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
我正在使用 maatwebsite 的 Laravel Excel,它只是 laravel 中 PHPExcel 的包装器,但我只想做只是将类别信息作为一行,随后的产品信息作为其下面的行。
这是我正在使用的数组的 excel 代码,上面转储了(项目代码是 19738,19739 值)
$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);
})->download('xlsx');
方法fromArray()
需要一个二维数组
$data=(
array(2) (
[0] => array(3) (
[0] => 19738
[1] => ...
[2] => ...
)
[1] => array(4) (
[0] => 19739
[1] => ...
[2] => ...
[3] => ...
数组$data 的每个元素都是一行。每个子元素都是一列的值。重组数组的创建以适应此结构,您将开展业务。
这段代码未经测试,只是想举个例子。我不确定你用 get_object_vars($sku->prices);
做什么。我相信这必须改变。
$excelRows = [];
foreach($prices->categories as $category){
$excelRows[] = [
$category->category_code,
$category->category_name,
$category->category_desc
]
foreach($category->skus as $sku){
$row = [
$sku->sku_info->identifier,
$sku->sku_info->item->description,
$sku->sku_info->item->item_type->count
]
foreach ($sku->sku_info->details as $details) {
$row[] = $details->detail_code;
$row[] = $details->detail_code2;
$row[] = $details->detail_specifier;
}
$row[] = get_object_vars($sku->prices);
$row[] = $sku->sku_info->item->item_code;
$excelRows[] = $row;
}
}
我正在循环的当前数组转储此结构
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"
]
]
但是当我导出到 excel 它只显示三个顶级属性
123 | Testing | This is a test category
我正在尝试以某种方式导出它,以便前 3 个值是一行(如 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
我正在使用 maatwebsite 的 Laravel Excel,它只是 laravel 中 PHPExcel 的包装器,但我只想做只是将类别信息作为一行,随后的产品信息作为其下面的行。
这是我正在使用的数组的 excel 代码,上面转储了(项目代码是 19738,19739 值)
$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);
})->download('xlsx');
方法fromArray()
需要一个二维数组
$data=(
array(2) (
[0] => array(3) (
[0] => 19738
[1] => ...
[2] => ...
)
[1] => array(4) (
[0] => 19739
[1] => ...
[2] => ...
[3] => ...
数组$data 的每个元素都是一行。每个子元素都是一列的值。重组数组的创建以适应此结构,您将开展业务。
这段代码未经测试,只是想举个例子。我不确定你用 get_object_vars($sku->prices);
做什么。我相信这必须改变。
$excelRows = [];
foreach($prices->categories as $category){
$excelRows[] = [
$category->category_code,
$category->category_name,
$category->category_desc
]
foreach($category->skus as $sku){
$row = [
$sku->sku_info->identifier,
$sku->sku_info->item->description,
$sku->sku_info->item->item_type->count
]
foreach ($sku->sku_info->details as $details) {
$row[] = $details->detail_code;
$row[] = $details->detail_code2;
$row[] = $details->detail_specifier;
}
$row[] = get_object_vars($sku->prices);
$row[] = $sku->sku_info->item->item_code;
$excelRows[] = $row;
}
}