1.在项目中创建一个与public同级的extend文件夹
2.下载一个PHPExcel插件放置到项目中的extend文件夹下(下载地址https://github.com/PHPOffice/PHPExcel)
3.调用下面的方法(数据库什么的自行设置)
public function into()
{
include_once(EXTEND_PATH . "phpexcel/PHPExcel.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/Writer/Excel2007.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/Writer/Excel5.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/IOFactory.php");
$file_type = $_FILES['myfile']['type'];
if ($file_type != 'application/vnd.ms-excel') {
echo "";
exit();
}
//判断表格是否上传成功
if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
//以上三步加载phpExcel的类
$objReader = \PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
//接收存在缓存中的excel表格
$filename = $_FILES['myfile']['tmp_name'];
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
//循环读取excel表格,读取一条,插入一条
//j表示从哪一行开始读取 从第二行开始读取,因为第一行是标题不保存
for ($j = 2; $j <= $highestRow; $j++) {
$id = $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue();//获取A列的值
$order_id = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();//获取B列的值
$title = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue();//获取C列的值
$refund_money = $objPHPExcel->getActiveSheet()->getCell("D" . $j)->getValue();//获取D列的值
$return_money = $objPHPExcel->getActiveSheet()->getCell("E" . $j)->getValue();//获取E列的值
$status = $objPHPExcel->getActiveSheet()->getCell("F" . $j)->getValue();//获取F列的值
$order_time = $objPHPExcel->getActiveSheet()->getCell("G" . $j)->getValue();//获取列的值
$create_time = $objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue();//获取H列的值
$pay_time = $objPHPExcel->getActiveSheet()->getCell("I" . $j)->getValue();//获取I列的值
$arr['id'] = $id;
$arr['order_id'] = $order_id;
$arr['title'] = $title;
$arr['refund_money'] = $refund_money;
$arr['return_money'] = $return_money;
$arr['status'] = $status;
$arr['order_time'] = $order_time;
$arr['create_time'] = $create_time;
$arr['pay_time'] = $pay_time;
$res = DB::table('taoke_refund')->insert($arr);
}
if ($res) {
$this->success('添加成功');
} else {
$this->error('操作失败,请稍后再试');
}
}
}