php表导入,php操作excel表格的导入和导出

public function import_batch_send()

{

header("content-type:text/html;charset=utf-8");

//上传excel文件

$file = request()->file('file');

//将文件保存到public/uploads目录下面

$info = $file->validate(['size' => 1048576, 'ext' => 'xls,xlsx'])->move('./uploads');

if ($info) {

//获取上传到后台的文件名

$fileName = $info->getSaveName();

//获取文件路径

$filePath = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $fileName;

//获取文件后缀

$suffix = $info->getExtension();

//判断哪种类型

if ($suffix == "xlsx") {

$reader = \PHPExcel_IOFactory::createReader('Excel2007');

} else {

$reader = PHPExcel_IOFactory::createReader('Excel5');

}

} else {

return json(['status' => '1', 'message' => '文件过大或格式不正确导致上传失败-_-!']);

}

//载入excel文件

$excel = $reader->load($filePath, $encode = 'utf-8');

//读取第一张表

$sheet = $excel->getSheet(0);

//获取总行数

$row_num = $sheet->getHighestRow();

//获取总列数

$col_num = $sheet->getHighestColumn();

$import_data = []; //数组形式获取表格数据

for ($i = 2; $i <= $row_num; $i++) {

$import_data[$i]['nickname'] = $sheet->getCell("B" . $i)->getValue();

$import_data[$i]['phone'] = $sheet->getCell("C" . $i)->getValue();

}

if (empty($import_data)) {

return json(['status' => '1', 'message' => '数据解析失败']);

}

//校验手机号是否重复

$phone_array = array_column($import_data, 'phone');

$phone_ids = implode(',', $phone_array);

$result_phone = db('user')

->field('phone')

->where('phone', 'in', $phone_ids)

->select();

if (!empty($result_phone)) {

$result_phone_array = array_column($result_phone, 'phone');

$result_phone_ids = implode(',', $result_phone_array);

return json(['status' => '3', 'message' => '数据重复', 'result' => $result_phone_ids]);

}

//将数据保存到数据库

$res = db('user')->insertAll($import_data);

if ($res) {

return json(['status' => '2', 'message' => '导入成功']);

} else {

return json(['status' => '1', 'message' => '提交失败,请刷新重试']);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要安装 PHPExcel 扩展,可以使用 Composer 安装或手动下载安装。 接下来就是编写导入导出 Excel 的代码了,以下是一个基本的示例: 导出 Excel: ```php <?php require_once 'path/to/PHPExcel/Classes/PHPExcel.php'; // 创建新的 Excel 对象 $objPHPExcel = new PHPExcel(); // 设置 Excel 文件属性 $objPHPExcel->getProperties() ->setCreator('Your Name') ->setLastModifiedBy('Your Name') ->setTitle('Title') ->setSubject('Subject') ->setDescription('Description') ->setKeywords('Keywords') ->setCategory('Category'); // 设置工作标题 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Column 1') ->setCellValue('B1', 'Column 2') ->setCellValue('C1', 'Column 3'); // 填充数据 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A2', 'Data 1') ->setCellValue('B2', 'Data 2') ->setCellValue('C2', 'Data 3'); // 设置表格格式 $objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); // 下载 Excel 文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="filename.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); ``` 导入 Excel: ```php <?php require_once 'path/to/PHPExcel/Classes/PHPExcel.php'; // 读取 Excel 文件 $inputFileName = 'path/to/file.xlsx'; $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); // 获取第一个工作 $worksheet = $objPHPExcel->getActiveSheet(); // 获取行数和列数 $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // 遍历行数据 for ($row = 1; $row <= $highestRow; $row++) { // 遍历列数据 for ($col = 0; $col < $highestColumnIndex; $col++) { $cell = $worksheet->getCellByColumnAndRow($col, $row); $value = $cell->getValue(); // 处理单元格数据 // ... } } ``` 以上是一个简单的示例,具体的代码需要根据实际情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值