Fastadmin自定义按钮服务端导出

View

##如果需要传参或者需要做按钮事件可去相应的JS页面进行按钮绑定事件。

<div class="panel panel-default panel-intro">
    {:build_heading()}

    <div class="panel-body">
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="one">
                <div class="widget-body no-padding">
                    <div id="toolbar" class="toolbar">
                        <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
                        <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('unsellable/unsellabledata/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
                        <a href="javascript:;" class="btn btn-success btn-export {:$auth->check('unsellable/unsellabledata/export')?'':'hide'}" title="{:__('Export')}" id="btn-export-file"  onClick="window.location.href='unsellabledata/export'" ><i class="fa fa-download"></i> {:__('Export')}</a>

                    </div>
                    <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
                           data-operate-edit="{:$auth->check('unsellable/unsellabledata/edit')}"
                           width="100%">
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>

控制器

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\IOFactory;

public function export()
    {
       // $has_fnsku = Db::name('fba_ralations')->group('fnsku')->column('fnsku');
        //$data = Db::name('unsellable_data')->field("sku,'',fnsku")->where('fnsku','not in',$has_fnsku)->group('sku,fnsku')->select();
        set_time_limit(0);
//进行输出数据的处理
$data = [
];

//列名
        $title = ['seller_sku','asin','fnsku','cost(产品成本)','item_name(产品名称,选填)','产品型号'];

        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();

        //设置工作表标题名称
        $worksheet->setTitle('表名');

        //表头
        //设置单元格内容
        foreach ($title as $key => $value) {
            $worksheet->setCellValueByColumnAndRow($key+1, 1, $value);
        }

        $row = 2; //从第二行开始
        foreach ($data as $item) {
            $column = 1;

            foreach ($item as $value) {
                $worksheet->setCellValueByColumnAndRow($column, $row, $value);
                $column++;
            }
            $row++;
        }


        $fileName = '表名';
        //$fileType = 'Xlsx';

        //1.下载到服务器
        //$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        //$writer->save($fileName.'.'.$fileType);

        //2.输出到浏览器
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); //按照指定格式生成Excel文件
        $this -> excelBrowserExport($fileName, 'Xlsx');
        $writer->save('php://output');
}

/**
 * 输出到浏览器(需要设置header头)
 * @param string $fileName 文件名
 * @param string $fileType 文件类型
 */
public function excelBrowserExport($fileName, $fileType) {

    //文件名称校验
    if(!$fileName) {
        trigger_error('文件名不能为空', E_USER_ERROR);
    }

    //Excel文件类型校验
    $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
    if(!in_array($fileType, $type)) {
        trigger_error('未知文件类型', E_USER_ERROR);
    }

    if($fileType == 'Excel2007' || $fileType == 'Xlsx') {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');
        header('Cache-Control: max-age=0');
    } else { //Excel5
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
        header('Cache-Control: max-age=0');
    }
}

##至此,即完成一个简单的自定义导出功能啦,原创不易,有用的点个关注呗

猜你喜欢

转载自blog.csdn.net/Fillthepit/article/details/122106227