您的位置 首页 php技术

fastadmin-导入excel重写

{:build_toolbar(‘refresh,edit,import’)} // 初始化表格参数配置 Ta…

{:build_toolbar('refresh,edit,import')}
// 初始化表格参数配置
Table.api.init({
    extend: {
        index_url: 'subject/logistics/index',
        edit_url: 'subject/logistics/edit',
        import_url: 'subject/logistics/import',
        table: 'logistics',
    }
});
//重写导入
public function import()
{
    $file = $this->request->request('file');
    if (!$file) {
        $this->error(__('Parameter %s can not be empty', 'file'));
    }
    $filePath = ROOT_PATH . DS . 'public' . DS . $file;
    if (!is_file($filePath)) {
        $this->error(__('No results were found'));
    }
    //实例化reader
    $ext = pathinfo($filePath, PATHINFO_EXTENSION);
    if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
        $this->error(__('Unknown data format'));
    }
    if ($ext === 'csv') {
        $file = fopen($filePath, 'r');
        $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
        $fp = fopen($filePath, "w");
        $n = 0;
        while ($line = fgets($file)) {
            $line = rtrim($line, "\n\r\0");
            $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
            if ($encoding != 'utf-8') {
                $line = mb_convert_encoding($line, 'utf-8', $encoding);
            }
            if ($n == 0 || preg_match('/^".*"$/', $line)) {
                fwrite($fp, $line . "\n");
            } else {
                fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
            }
            $n++;
        }
        fclose($file) || fclose($fp);
        $reader = new Csv();
    } elseif ($ext === 'xls') {
        $reader = new Xls();
    } else {
        $reader = new Xlsx();
    }

    //加载文件
    try {
        if (!$PHPExcel = $reader->load($filePath)) {
            $this->error(__('Unknown data format'));
        }
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
        $fields = [];
        for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
            for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                $fields[] = $val;
            }
        }
        $status = true;
        Db::startTrans();
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $values = [];
            for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                $values[] = is_null($val) ? '' : $val;
            }
            //此处为存入sql,根据自己的需求进行修改
            $temp = array_combine($fields, $values);
            $in_trade_no = $temp['in_trade_no'];
            unset($temp['in_trade_no']);
            $temp['status'] = 3;
            $res = db('sp_log_award')->where('in_trade_no',$in_trade_no)->update($temp);
            if (!$res) $status = false;
        }
        if ($status) {
            Db::commit();
            $this->success('上传成功');
        }else{
            Db::rollback();
            $this->error('上传失败');
        }
    } catch (Exception $exception) {
        $this->error($exception->getMessage());
    }
}
本文来自网络,不代表MuKe网站资源立场,转载请注明出处:https://www.somke.cn/archives/84

作者: delon

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

在线咨询: QQ交谈

邮箱: lon_mail@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部