«

laravel导出excel代码示例

时间:2024-2-18 10:12     作者:韩俊     分类: PHP


/**
 * 导出订单商品列表数据
 * @param array $data_list
 * @return bool
 * @throws ActionException
 */
private static function exportOrderProductListToExcel(array &$data_list) {
    if (!$data_list) {
        throw new ActionException('没有数据不需要导出', 101);
    }

    $result = \Excel::create(date('订单商品数据列表Y-m-d H:i:s导出'), function ($excel) use ($data_list) {

        $excel_table_header = [
            ['column' => 'A', 'width' => 25, 'field' => '商品名称'],
            ['column' => 'B', 'width' => 10, 'field' => '原价'],
            ['column' => 'C', 'width' => 10, 'field' => '订购数量'],
            ['column' => 'D', 'width' => 10, 'field' => '退款数量'],
            ['column' => 'E', 'width' => 10, 'field' => '退款金额'],
            ['column' => 'F', 'width' => 10, 'field' => '交易金额'],
            ['column' => 'G', 'width' => 10, 'field' => '分成比例'],
            ['column' => 'H', 'width' => 10, 'field' => '分成金额'],
            ['column' => 'I', 'width' => 10, 'field' => '实收金额'],
            ['column' => 'J', 'width' => 20, 'field' => '开课机构/学校'],
            ['column' => 'K', 'width' => 10, 'field' => '商品类型']
        ];

        $list = [];
        foreach ($data_list as $key => &$data) {
            $list[$key]['product_name'] = $data['product_name'];
            $list[$key]['original_price'] = $data['original_price'];
            $list[$key]['sell_num'] = $data['sell_num'];
            $list[$key]['refund_num'] = $data['refund_num'];
            $list[$key]['refund_money'] = $data['refund_money'];
            $list[$key]['transaction_money'] = $data['transaction_money'];
            $list[$key]['commission_scale'] = $data['commission_scale'] . '%';
            $list[$key]['divided_money'] = $data['divided_money'];
            $list[$key]['income_money'] = $data['income_money'];
            $list[$key]['org_name'] = $data['org_name'];
            $list[$key]['type'] = $data['type_text'];
        }

        array_unshift($list, array_column($excel_table_header, 'field'));

        $excel->sheet('商品', function ($sheet) use ($list, $excel_table_header) {
            $sheet->fromArray($list, '', 'A1', false, false)->setWidth(
                array_combine(
                    array_column($excel_table_header, 'column'),
                    array_column($excel_table_header, 'width')
                )
            );
        });
    })->export('xls');

    return $result ? true : false;
}

标签: php php教程

热门推荐