phpExcel import processing method for matching corresponding fields according to different Chinese

phpExcel import processing method for matching corresponding fields according to different Chinese

Without further ado, go directly to the hardcore

First, take out the field and name in the field table, save the name (Chinese) as the key into an array keyArray, and the field (field) as the value into the valueMap; get the Chinese title line of the table, and save the content of the obtained table body. Including Chinese rows are destroyed; two cycles are performed on the sheetData, the second row is the row to be processed, to determine whether the key of the second cycle is in the array keyArray, if it does not exist, the unset column, still It needs to be judged. For example, there are some first columns in the list, that is, [A] contains words such as "statistics", "total", and "mean". It does not need to be inserted into the data table to judge if [A] does not have these words array_column is assembled with value (field) as the key, and the value of each row of the table as the value, and then inserted into the table.

The following is the important part of the code can be seen

// This is the beginning of the processing ====================== "
Important content
/// This is the end of the processing" ========== =============== The
printed variables are also retained in the code. If you do n’t understand, you can let go of the comments and see the results of each step after step by step.

function excelImport2($post, $model, $model_field, $title_row)
{
    $m_leads = D($model);
    $shop_data = getShopInfo();
    $tyep_data = getTypeInfo();
    $types = getTypeData();

    if (isset($_FILES['excel']['size']) && $_FILES['excel']['size'] != null) {
        import('@.ORG.UploadFile');
        $upload = new UploadFile();
        $upload->maxSize = 20000000;
        $upload->allowExts = array('xls');
        $dirname = UPLOAD_PATH . date('Ym', time()) . '/' . date('d', time()) . '/';
        if (!is_dir($dirname) && !mkdir($dirname, 0777, true)) {
            alert('error', L('ATTACHMENTS TO UPLOAD DIRECTORY CANNOT WRITE'), $_SERVER['HTTP_REFERER']);
        }
        $upload->savePath = $dirname;
        if (!$upload->upload()) {
            alert('error', $upload->getErrorMsg(), $_SERVER['HTTP_REFERER']);
        } else {
            $info = $upload->getUploadFileInfo();
        }
    }

    if (is_array($info[0]) && !empty($info[0])) {
        $savePath = $dirname . $info[0]['savename'];
    } else {
        alert('error', L('UPLOAD FAILED'), $_SERVER['HTTP_REFERER']);
    };
    import("ORG.PHPExcel.PHPExcel");
    $PHPExcel = new PHPExcel();
    $PHPReader = new PHPExcel_Reader_Excel2007();
    if (!$PHPReader->canRead($savePath)) {
        $PHPReader = new PHPExcel_Reader_Excel5();
    }
    $PHPExcel = $PHPReader->load($savePath);
    $currentSheet = $PHPExcel->getSheet(0);
    $allRow = $currentSheet->getHighestRow();
    $highestColumn = $currentSheet->getHighestColumn();//how many columns
    $allColumn = PHPExcel_Cell::columnIndexFromString($highestColumn);

    $sheetData = $PHPExcel->getActiveSheet()->toArray(null, false, false, true);
    //删除导入的文件
    unlink($savePath);
//dump($sheetData);exit;
    $where2 = $keyArray = [];
    $where2['model'] = $model_field;
    //这是处理的开始======================》
    $field_list = M('NewFields')->where($where2)->order('order_id')->select();

    $modelData = array_column($field_list, 'field', 'name');
    if (isset($sheetData[$title_row])) {
        foreach ($sheetData[$title_row] as $key => $value) {
            if (isset($modelData[$value])) {
                $keyArray[] = $key;
                $valueMap[$modelData[$value]] = '';
            }
        }
    }
//    dump($keyArray);
//    dump($valueMap);exit;
    for ($i = 1; $i <= $title_row; $i++) {
        unset($sheetData[$i]);
    }
    $newData = [];
//    dump($sheetData);exit;
    foreach ($sheetData as $key => $value) {
        foreach ($value as $k => $v) {
            if (!in_array($k, $keyArray)) {
                unset($value[$k]);
            }
        }
        if (!($value['A'] == '总计') && !($value['A'] == '汇总') && !($value['A'] == '均值')) {
            $newData[] = array_combine(array_keys($valueMap), array_values($value));
        }
//            var_dump(array_keys($valueMap));
//            echo '-';
//            var_dump(array_values($value));
//            exit;
    }
//这是处理的结束《=======================================
    ········
    }
Published an original article · Likes0 · Visits0

Guess you like

Origin blog.csdn.net/weixin_43928139/article/details/105563343