首页 > 代码库 > phalcon之PHPExcel批量导入
phalcon之PHPExcel批量导入
网上好多资料,但运行起来,总是有问题,这次好保存下来。
phalcon下:
第一步:在index.php里面添加
include __DIR__ . "/../app/extensions/phpexcel/PHPExcel.php";
第二步:写下面function
/*
* 批量导入用户
*/
public function batchImportUsersAction()
{
$filePath = dirname(__FILE__).‘/../data/users.xls‘;
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
echo ‘no Excel‘;
exit;
}
}else{
echo ‘can read‘;exit;
}
$PHPExcel = $PHPReader->load($filePath);
// echo is_object($PHPExcel).‘mmm‘;exit; //查看是否初始化成功
$currentSheet = $PHPExcel->getSheet(0); //读取excel文件中的第一个工作表
$allColumn = $currentSheet->getHighestColumn();//取得所有列
$allRow = $currentSheet->getHighestRow();//取得一共有多少行
// $choose_row = $allRow; //遍历所有行
$choose_row = 205;
for($currentRow = 177;$currentRow <= $choose_row;$currentRow++){ //遍历所有行或指定行,177是excel的行号
for($currentColumn= ‘A‘;$currentColumn<= $allColumn; $currentColumn++){ //从第A列开始输出
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()将字符转为十进制数*/
// if($currentColumn == ‘B‘){
// echo iconv(‘gb2312‘,‘utf-8‘, $val);// 转成utf-8格式
// }
// echo $val;
echo $val;
}
echo ‘<br/>‘;
//插入表
// $use = new Users();
// $use->employee_id = $currentSheet->getCellByColumnAndRow(ord(‘A‘) - 65,$currentRow)->getValue();
// $use->user_name = $currentSheet->getCellByColumnAndRow(ord(‘B‘) - 65,$currentRow)->getValue();
// $use->email = $currentSheet->getCellByColumnAndRow(ord(‘D‘) - 65,$currentRow)->getValue();
// $use->password = 22;
// $use->role = 1;
// $use->depart_id = 1;
// $use->status = 1;
// $use->position = 1;
// $use->update_time = time();
// $use->record_time = time();
//
// if($use->save() == false){
// echo $currentRow.‘行出错‘;
// }
}
}