生成省市区数据(不包含港澳台)
获取省市区数据
*行政区划代码:http://www.mca.gov.cn/article/sj/xzqh/
从以上网址找到最新行政区编码复制到excel
备注:因四个直辖市(北京、天津、上海、重庆)无市级划分、故加入同级市并将区划代码后三位改为100.
生成所需数据excel函数
生成对应等级(省1、市2、区/县3):=IF(MID(A2,1,6)="100000",0,IF(MID(A2,3,4)="0000",1,IF(MID(A2,5,2)="00",2,3)))
生成上级ID:=IF(C2=0,C2,IF(C2=1,"100000",IF(C2=2,MID(A2,1,2)&"0000",IF(C2=3,MID(A2,1,4)&"00",))))
备注:因为phpexcel获取到数据为函数,故需要将数据重新复制为纯文本
生成数组数据
- php7.4及以上
composer require phpoffice/phpspreadsheet
public function import(string $file_path, int $start_row = 1): array
{
try {
# 创建读操作
$reader = IOFactory::createReaderForFile($file_path);
# 打开文件、载入excel表格
$spreadsheet = $reader->load($file_path);
# 获取活动工作薄
$sheet = $spreadsheet->getActiveSheet();
//获取总列数
$total_column = $sheet->getHighestColumn();
//获取总行数
$total_row = $sheet->getHighestRow();
$data = [];
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($current_row = $start_row; $current_row <= $total_row; $current_row++) {
//从哪列开始,A表示第一列
for ($current_column = 'A'; $current_column <= $total_column; $current_column++) {
//数据坐标
$address = $current_column . $current_row;
//去除空格,将获取到的数组转化为字符串
$string_value = preg_replace('/^[(\xc2\xa0)|\s]+/', '', $sheet->getCell($address)->getValue());
//去除为空的数据
if ($string_value == ''){
break;
}
//读取到的数据,保存到数组中
$data[$current_row][$current_column] = $string_value;
}
}
}catch (Exception $exception){
return [
'error' => $exception->getMessage()
];
}
return $data;
}
- php7.3及以下
composer require phpoffice/phpexcel
public function import(string $file_path, int $start_row = 1): array
{
try {
# 创建读操作
$PHPReader = \PHPExcel_IOFactory::createReaderForFile($file_path);
# 打开文件、载入excel表格
$PHPExcel = $PHPReader->load($file_path);
# 获取活动工作薄
$currentSheet = $PHPExcel->getSheet();
//获取总列数
$total_column = $currentSheet->getHighestColumn();
//获取总行数
$total_row = $currentSheet->getHighestRow();
$data = [];
//循环获取表中的数据,$current_row表示当前行,从哪行开始读取数据,索引值从0开始
for ($current_row = $start_row; $current_row <= $total_row; $current_row++) {
//从哪列开始,A表示第一列
for ($current_column = 'A'; $current_column <= $total_column; $current_column++) {
//数据坐标
$address = $current_column . $current_row;
//去除空格,将获取到的数组转化为字符串
$string_value = preg_replace('/^[(\xc2\xa0)|\s]+/', '', $sheet->getCell($address)->getValue());
//去除为空的数据
if ($string_value == ''){
break;
}
//读取到的数据,保存到数组中
$data[$current_row][$current_column] = $string_value;
}
}
}catch (Exception $exception){
return [
'error' => $exception->getMessage()
];
}
return $data;
}
拼装导入数据
有能力自己拼装sql、没有能力利用框架。
已生成好的sql:https://www.51job.top/provinces/sql