ThinkPHP基于PHPExcel导入Excel文件的方法(2)
复制代码 代码如下: $rowCount=$sheet0-getHighestRow();//excel行数 $data=array(); for ($i = 2; $i = $rowCount; $i++){ $item['name']=$this-getExcelValue($sheet0,'A'.$i); $item['sex']=$this-getE
复制代码 代码如下:
$rowCount=$sheet0->getHighestRow();//excel行数
$data=array();
for ($i = 2; $i <= $rowCount; $i++){
$item['name']=$this->getExcelValue($sheet0,'A'.$i);
$item['sex']=$this->getExcelValue($sheet0,'B'.$i);
$item['contact']=$this->getExcelValue($sheet0,'C'.$i);
$item['remark']=$this->getExcelValue($sheet0,'D'.$i);
$item['addtime']=$this->getExcelValue($sheet0,'E'.$i);
$data=array();
for ($i = 2; $i <= $rowCount; $i++){
$item['name']=$this->getExcelValue($sheet0,'A'.$i);
$item['sex']=$this->getExcelValue($sheet0,'B'.$i);
$item['contact']=$this->getExcelValue($sheet0,'C'.$i);
$item['remark']=$this->getExcelValue($sheet0,'D'.$i);
$item['addtime']=$this->getExcelValue($sheet0,'E'.$i);
$data[]=$item;
}
三、最后保存到数据库
复制代码 代码如下:
$success=0;
$error=0;
$sum=count($data);
foreach($data as $k=>$v){
if(M('temp_area3')->data($v)->add()){
$success++;
}else {
$error++;
}
}
$error=0;
$sum=count($data);
foreach($data as $k=>$v){
if(M('temp_area3')->data($v)->add()){
$success++;
}else {
$error++;
}
}
echo "总{$sum}条,成功{$success}条,失败{$error}条。";
至此大功告成!希望本文所述对大家的ThinkPHP框架程序设计有所帮助。
精彩图集
精彩文章