public function importExcel($file){
// include(__DIR__.'/../../../vendor/PHPExcel/Classes/PHPExcel.php');
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize' => '16MB');
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);//文件缓存
//当前空间不用\,非当前空间要加\
$PHPExcel = new \PHPExcel();//创建一个excel对象
$PHPReader = new \PHPExcel_Reader_Excel2007(); //建立reader对象,excel—2007以后格式
if (!$PHPReader->canRead($file)) {
$PHPReader = new \PHPExcel_Reader_Excel5();//建立reader对象,excel—2007以前格式
if (!$PHPReader->canRead($file)) {
$msg = '不是excel格式文件';
$this->apiReturn(303,$msg);
}
}
$PHPExcel = $PHPReader->load($file); //加载excel对象
$sheet = $PHPExcel->getSheet(0); //获取指定的sheet表
$rows = $sheet->getHighestRow();//行数
$cols = $sheet->getHighestColumn();//列数
$data = array();
for ($i = 3; $i getValue();
$data[$i - 3][$count] = (string)$sheet->getCell($j . $i)->getValue() ?? "";
$count += 1;
if ($value) {
}
}
}
return $data;
}
public function index()
{
//接收上传文件
$file = $_FILES['file'];
if (!$file) {
$this->error(303, '缺少file');
}
//调用导入excel方法
$data = $this->importExcel($file['tmp_name']);
// dump($data);
// die;
$sql = [];
for ($i = 1; $i < count($data); $i++) {
$sql[$i]['measure_area'] = $data[$i][3];//建筑面积
$sql[$i]['building_id_id'] = $data[$i][4];//楼栋
$sql[$i]['unit'] = $data[$i][5];//单元
$sql[$i]['room_number'] = $data[$i][6];//房间号
$sql[$i]['fo_measure_area'] = $data[$i][7];//F1面积
$sql[$i]['fo_orientation'] = $data[$i][8];//F1房建方向
$sql[$i]['fo_rent'] = $data[$i][9];//F1租金
$sql[$i]['ft_orientation'] = $data[$i][10];//F2面积
$sql[$i]['ft_orientation'] = $data[$i][11];//F2房建方向
$sql[$i]['ft_rent'] = $data[$i][12];//F2租金
$sql[$i]['ft_orientation'] = $data[$i][13];//F3面积
$sql[$i]['ft_orientation'] = $data[$i][14];//F3房建方向
$sql[$i]['ft_rent'] = $data[$i][15];//F3租金
$sql[$i]['rent'] = (int)$data[$i][9] + (int)$data[$i][12] + (int)$data[$i][15];//租金
$sql[$i]['hr_name'] = "爱心嘉园";
$sql[$i]['community_id_id'] = Db::name('community_management')->where(['community_name'=>$sql[$i]['hr_name']])->value('id',0);
if ($sql[$i]['community_id_id'] == 0){
echo $sql[$i]['hr_name'];
die;
}
}
dump($sql);
die;
//写入数据库
Db::name('auth')->insertAll($sql);
}
composer require phpoffice/phpexcel