Laravel Excel无法以正确的格式导出

时间:2019-01-17 20:57:29

标签: php arrays excel laravel laravel-excel

我目前有一个构建好的数组,可以像这样转储:

photos <- list.files(
  path = "photos", 
  recursive = TRUE,
  full.names = TRUE
)

for (p in photos) {
  id <- basename(p)
  id <- gsub(".jpg$", "", id)
  name <- df$name_lower[match(id, basename(df$uri))]
  fname <- paste0(dirname(p), "/", name, ".jpg")
  file.rename(p, fname)

# optional
  cat(
    "renaming", 
    basename(p), 
    "to", 
    name, 
    "succesful:", 
    ifelse(success, "Yes", "No"),
    "\n"
  )
}

我正在使用laravel excel来将其导出为ex​​cel文件,但是它并没有按照我的意图正常工作

当它导出到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"
    ]
  ]

但是我想将该信息作为标题,然后将该类别的每个后续产品作为一行,因此在上面的示例中,它看起来像:

123  |  Testing  |  This is a test category

这是我正在使用的数组的excel代码,该代码转储在上面:

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

1 个答案:

答案 0 :(得分:1)

我猜(只是一个猜测),头生成在这里使您失败。 尝试对数据进行处理以使每一列具有相同的索引(注意:未测试代码,您可能需要更正它):

$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有所影响,请通知我。那将是对该库的基本了解,这将帮助我们为您提供帮助。

要回答您的评论,可以在phpExcelsheet对象上调用本机excel函数。因此,您可以使用它来设置行粗体格式:

$sheet->->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);

请阅读phpExcel以了解laravel excel的实际用途,它将为您带来很多帮助