thinkphp PHPEXCEL 扩展包下载和导入excel示例

正文开始

LR主页 回首页 thinkphp PHPEXCEL 扩展包下载和导入excel示例

时间: 2021-03-04 16:13:44
分类: php
浏览: 654

正文开始

依赖2个列互转函数 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13, 'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26, 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26 );

    //  We also use the language construct isset() rather than the more costly strlen() function to match the length of $pString
    //      for improved performance
    if (isset($pString{0})) {
        if (!isset($pString{1})) {
            $_indexCache[$pString] = $_columnLookup[$pString];
            return $_indexCache[$pString];
        } elseif(!isset($pString{2})) {
            $_indexCache[$pString] = $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}];
            return $_indexCache[$pString];
        } elseif(!isset($pString{3})) {
            $_indexCache[$pString] = $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}];
            return $_indexCache[$pString];
        }
    }
    throw new PHPExcel_Exception("Column string index can not be " . ((isset($pString{0})) ? "longer than 3 characters" : "empty"));
}

}

file_put_contents($move_to_file, $fileBase64);

    //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
    vendor('PHPExcel.PHPExcel');
    //如果excel文件后缀名为.xls,导入这个类
    if ($geshi == 'xls') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel5');
        $PHPReader = new \PHPExcel_Reader_Excel5();
    } elseif ($geshi == 'xlsx') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $PHPReader = new \PHPExcel_Reader_Excel2007();
    } else {
        return $this->error('file no support:' . $geshi);
    }

//载入文件 $PHPExcel = $PHPReader->load($move_to_file); //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 $sheet = $PHPExcel->getSheet(0); // 取得总行数 $highestRow = $sheet->getHighestDataRow(); // 取得总列数 $highestColumn = $sheet->getHighestDataColumn(); $highestColumnNum = Excel::columnIndexFromString($highestColumn); //循环读取excel文件,读取一条,插入一条 $data=array(); //从第一行开始读取数据 for($j=1;$j<=$highestRow;$j++){ //从A列读取数据 for($k=0; $k<=$highestColumnNum; $k++){ $cellName = Excel::stringFromColumnIndex($k); // 读取单元格 $data[$j][] = $PHPExcel->getActiveSheet()->getCell("$cellName$j")->getValue(); } } print_r(json_encode($data)); exit;

/upload/post_files/3wyltaJgRppq0AZy.txt

vendor扩展下载后 .txt格式改为.zip 解压到/vendor/目录下 如: \vendor\PHPExcel\PHPExcel.php \vendor\PHPExcel\PHPExcel

正文结束 PHP接口(interface)和抽象类(abstract) php excel的列名字:字母和数字互转 ©2007

正文结束

PHP接口(interface)和抽象类(abstract) php excel的列名字:字母和数字互转