建表
CREATE TABLE `fa_app_freight_company` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cn_name` varchar(50) DEFAULT NULL COMMENT '名称',
`code` varchar(30) DEFAULT NULL COMMENT '编码',
`letter` varchar(10) DEFAULT NULL COMMENT '字母',
`used` tinyint(1) unsigned DEFAULT '0' COMMENT '常用:0=否,1=是',
`country_cat` tinyint(1) unsigned DEFAULT '1' COMMENT '国家:1=国内,2=国外',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=637 DEFAULT CHARSET=utf8 COMMENT='快递公司';
下载phpexcel
解压后放到extend目录
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Config;
use think\Cache;
use PHPExcel_IOFactory;
use PHPExcel;
use think\Loader;
class Community extends Api
{
/**
* 导入excel
*/
public function read_excel() {
Loader::import('PHPExcel.Classes.PHPExcel');
$excelobj = new \PHPExcel();
$readobj = PHPExcel_IOFactory::createReader('excel2007');
$file = "kd.xlsx";
$res = $readobj->load($file);
$arr = $res->getSheet(0)->toArray();
$insert_arr = [];
foreach( $arr as $k => $v ){
$temp_arr = [];
$used = 0;
$letter = '';
$country_cat = 1;
if( $k < 2 ){
continue;
}elseif( $k > 2 && $k < 15 ){
$used = 1;
}elseif( $k > 14 && $k < 324 ){
$used = 0;
}elseif( $k > 323 ){
$used = 0;
$country_cat = 2;
}
$temp_arr = [
"cn_name" => $v[2],
"code" => $v[3],
"letter" => $v[1],
"used" => $used,
"country_cat" => $country_cat,
];
$insert_arr[] = $temp_arr;
}
Db::name( "app_freight_company" )->insertAll( $insert_arr );
echo "<pre>";var_dump( $arr );die;
}
}