【发布时间】:2019-06-12 03:21:05
【问题描述】:
我目前有一个我构建的数组,可以像这样转储:
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
但我想将该信息作为标题获取,然后将该类别的每个后续产品作为一行获取,因此对于上面的示例,它看起来像:
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);
【问题讨论】:
-
Tom - 你发布的所有内容对我来说都是正确的代码。我认为问题可能在于您如何指示 LaravelExcel 将信息吐出到电子表格中,但您还没有发布那段代码。如果你坚持下去,也许有人可以帮助你。
-
也许这就是我的困惑所在。除了我下载为 excel 格式的行之外,我上面所拥有的一切。但我正在使用函数 fromArray 来转储它。即使使用该功能,我还应该构建特定的表单吗?
-
射击,我明白了。我只是看着那个包裹并同意。看起来这个包是超高级的(一条线就可以了!)。我已经使用 PHPExcel 多年了,尽管它很痛苦,但它完全可以控制。我假设在 LaravelExcel 中有一些方法可以变得更细化并告诉它数组的 N 级别是你需要显示的......但它看起来不像是为那种开箱即用的复杂性而设置的.抱歉,我帮不上忙。
-
谢谢,我可能也要看看php excel
-
请发布您使用的库的确切版本。对我来说,它看起来像
laravel excel 2.1,对吗?您是否关闭了自动标题生成?如果不将最后一行更改为$sheet->fromArray($allCategoryResult, null, 'A1', false, false);,否则它将尝试根据数组键设置标题,因此不会添加除相应索引之外的任何数据。不确定最后一部分,但无论如何都会有助于调试
标签: php arrays excel laravel laravel-excel