public function actionImport()
{
$model = new CrmCustomerImportForm();
$ok = "";
if (Yii::$app->request->isPost) {
$file = UploadedFile::getInstance($model, 'file');
if ($file) {
$filename = time().'.'.$file->extension;
$filename = time();
$file->saveAs($filename.'.'.$file->extension);
if(in_array($file->extension,array('xls','xlsx','csv'))){
if($file->extension =='xlsx')
{
$objReader = new \PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader ->load($file);
}
else if ($file->extension =='xls')
{
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader ->load($file);
}
else if ($file->extension=='csv')
{
$PHPReader = new \PHPExcel_Reader_CSV();
$PHPReader->setInputEncoding('GBK');
$PHPReader->setDelimiter(',');
$objPHPExcel = $PHPReader->load($file);
}
else
{
die('文件格式不对!');
}
$objWorksheet = $objPHPExcel->getSheet(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
if($highestRow > 1){
$transaction=Yii::$app->db->beginTransaction();
try {
$tableData = [];
$err = [];
for($row = 2; $row <= $highestRow; $row++){
$data = [];
for($col = 'A'; $col <= $highestColumn; $col++){
$data[] = trim($objWorksheet->getCell($col.$row)->getValue());
}
$tableData[] = $data;
$customer = new CrmCustomerImportForm();
$customer->name = $data[0];
$customer->gender = $data[1];
$customer->get_way = $data[2];
$customer->phone = $data[3];
$customer->tel = $data[4];
$customer->wechat = $data[5];
$customer->email = $data[6];
$customer->qq = $data[7];
$customer->birthday = str_replace('/', '-', $data[8]);
$customer->caller = $data[9];
$customer->street = $data[10];
$customer->remark = $data[11];
if(!$customer->validate())
{
throw new Exception(reset($customer->getFirstErrors()));
}
$existUser = $customer->getCrmCustomer();
if ($existUser) {
if($file->extension =='xlsx')
{
$objReader = new \PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader ->load($filename.'.'.$file->extension);
$currentSheet = $objPHPExcel->getSheet(0);
$currentSheet->getStyle('A'.$row.':'.$col.$row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('D1E17E');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename.'.'.$file->extension);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'.xlsx"');
header('Content-Length:'.filesize($filename.'.'.$file->extension));
readfile($filename.'.'.$file->extension);
}
else if ($file->extension =='xls')
{
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader ->load($filename.'.'.$file->extension);
$currentSheet = $objPHPExcel->getSheet(0);
$currentSheet->getStyle('A'.$row.':'.$col.$row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('D1E17E');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($filename.'.'.$file->extension);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
header('Content-Length:'.filesize($filename.'.'.$file->extension));
readfile($filename.'.'.$file->extension);
}
else if ($file->extension=='csv')
{
$PHPReader = new \PHPExcel_Reader_CSV();
$objPHPExcel = $PHPReader->load($filename.'.'.$file->extension);
$currentSheet = $objPHPExcel->getSheet(0);
$currentSheet->getStyle('A'.$row.':'.$col.$row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('D1E17E');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($filename.'.'.$file->extension);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
header('Content-Length:'.filesize($filename.'.'.$file->extension));
readfile($filename.'.'.$file->extension);
}
}
if(!$customer->saveCustomer()){
throw new Exception('客户导入失败');
};
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
$err= $e->getMessage();
}
}
if ($ok == 1){
} else{
Yii::$app->session->setFlash('error', $err);
}
}
}
else
{
Yii::$app->session->setFlash('error', '请上传文件!');
}
}else{
return $this->render('import',[
'model'=>$model
]);
}
return $this->render('import',[
'model'=>$model
]);
}