当你在使用phpoffice/phpexcel 类库时候。composer 会给你提示一句话

Package phpoffice/phpexcel is abandoned, you should avoid using it. Use phpoffice/phpspreadsheet instead

phpexcel 已被废弃,建议我们用phpspreadsheet,

包地址:

https://packagist.org/packages/phpoffice/phpspreadsheet

composer:

composer require phpoffice/phpspreadsheet

使用

引入

use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\IOFactory;
use \PhpOffice\PhpSpreadsheet\Style\NumberFormat;

导出:

    //模板下载
public function template_download()
{ $spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('导入模板'); //设置当前sheet的标题
$worksheet->getStyle('A1:E1')->getFont()->setBold(true)->setName('Arial')->setSize();
$worksheet->getStyle('B1')->getFont()->setBold(true);
$worksheet->getDefaultColumnDimension()->setWidth(); //设置第一栏的标题
$worksheet->setCellValue('A1', '交易流水号');
$worksheet->setCellValue('B1', '开户名');
$worksheet->setCellValue('C1', '卡号');
$worksheet->setCellValue('D1', '交易金额');
$worksheet->setCellValue('E1', '交易时间'); //默认填充数据
$explame_data_list = array(
array(
'bank_deal_no' => '',
'account_name' => '小明',
'bank_card' => '',
'deal_money' => '100.00',
'deal_time' => date("Y-m-d H:i:s"),
),
); //第二行起
$baseRow = ; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
foreach ($explame_data_list as $k => $val) {
$i = $k + $baseRow;
$worksheet->setCellValue('A' . $i, $val['bank_deal_no']);
$worksheet->setCellValue('B' . $i, $val['account_name']);
$worksheet->setCellValue('C' . $i, $val['bank_card']);
$worksheet->setCellValue('D' . $i, $val['deal_money']);
$worksheet->setCellValue('E' . $i, $val['deal_time']);;
} //处理 数字过大会进行科学计数法
$worksheet->getStyle('A2')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
$worksheet->getStyle('C2')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER); $this->downloadExcel($spreadsheet, '批量导入模板-合同表单选项', 'Xls'); } /*********************************************************************************************************************/ //公共文件,用来传入xls并下载
private function downloadExcel($spreadsheet, $filename, $format)
{
// $format只能为 Xlsx 或 Xls
if ($format == 'Xlsx') {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} elseif ($format == 'Xls') {
header('Content-Type: application/vnd.ms-excel');
} header("Content-Disposition: attachment;filename="
. $filename . date('Y-m-d') . '.' . strtolower($format));
header('Cache-Control: max-age=0');
$objWriter = IOFactory::createWriter($spreadsheet, $format); $objWriter->save('php://output'); //通过php保存在本地的时候需要用到
//$objWriter->save($dir.'/demo.xlsx'); //以下为需要用到IE时候设置
// If you're serving to IE 9, then the following may be needed
//header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
//header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
//header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
//header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
//header('Pragma: public'); // HTTP/1.0
exit;
}

  

导入

    public function import(){
header("content-type:text/html;charset=utf-8"); //上传excel文件
$files = request()->file(); //将文件保存到public/uploads目录下面
try {
validate(['image'=>'fileSize:1048576|fileExt:xls'])
->check($files); $savename = [];
foreach($files as $file){
$savename[] = \think\facade\Filesystem::disk('public')->putFile( 'billfile', $file,'md5');
} } catch (think\exception\ValidateException $e) {
return json(['status' => '', 'message' => $e->getMessage()]);
}
//获取文件路径
$filePath = ROOT_PATH().'/public/uploads/'.$savename[];
$spreadsheet = IOFactory::load($filePath);
$sheetData = $spreadsheet->getActiveSheet()->toArray(true, true, true, true,true);
$row_num = count($sheetData); $now_time = time();
$import_data = []; //数组形式获取表格数据
for ($i = ; $i <= $row_num; $i++) { $bank_deal_no = $sheetData[$i]['A'];
$account_name = $sheetData[$i]['B'];
$bank_card = $sheetData[$i]['C'];
$deal_money = $sheetData[$i]['D'];
$deal_time = $sheetData[$i]['E']; if(!empty($bank_deal_no) && !empty($account_name) && !empty($bank_card) && !empty($deal_money) && !empty($deal_time) ){
$import_data[$i]['bank_deal_no'] = $bank_deal_no;
$import_data[$i]['account_name'] = $account_name;
$import_data[$i]['bank_card'] = $bank_card;
$import_data[$i]['deal_money'] = $deal_money;
$import_data[$i]['deal_time'] = $deal_time;
$import_data[$i]['create_time'] = $now_time;
$import_data[$i]['update_time'] = $now_time;
}
} sort($import_data); if (empty($import_data)) {
return json(['status' => '', 'message' => '数据解析失败']);
} $total_num = count($import_data);
if ($total_num > ) {
return json(['status' => '', 'message' => '数据超出限制,最多100条']);
} //校验是否重复:交易流水号
$data_array = array_column($import_data, 'bank_deal_no');
$data_ids = implode(',', $data_array);
$result_data = Db::name('user_bank_bill')
->field('bank_deal_no')
->where('bank_deal_no', 'in', $data_ids)
->select()
->toArray(); $error_message = '';
if (!empty($result_data)) {
$result_data_array = array_column($result_data, 'bank_deal_no');
$result_data_ids = implode(',', $result_data_array);
$error_message = '以下流水号有重复,已筛选出: '.$result_data_ids;
foreach ($import_data as $key => $value) {
if(in_array($value['bank_deal_no'],$result_data_array)){
unset($import_data[$key]);
}
}
} if(!empty($import_data)){
//将数据保存到数据库
$res = Db::name('user_bank_bill')->insertAll($import_data);
if ($res) {
return json(['status' => '', 'message' => '操作成功','result'=>$error_message]);
} else {
return json(['status' => '', 'message' => '提交失败,请刷新重试']);
}
} return json(['status' => '', 'message' => '数据错误','result' => $error_message]); }

【PHP】使用phpoffice/phpspreadsheet导入导出数据的更多相关文章

  1. 【PHP】使用phpoffice/phpexcel导入导出数据

    本例以thinkphp5.1为例 包地址: https://packagist.org/packages/phpoffice/phpexcel 使用: composer require phpoffi ...

  2. CRL快速开发框架系列教程九(导入/导出数据)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  3. mysql导入导出数据中文乱码解决方法小结

    linux系统中 linux默认的是utf8编码,而windows是gbk编码,所以会出现上面的乱码问题. 解决mysql导入导出数据乱码问题 首先要做的是要确定你导出数据的编码格式,使用mysqld ...

  4. Android开发笔记:SQLite导入导出数据

    SQLite是Android中最方便使用的数据库了,现在看下如何快速的在SQLite中导入导出数据. 首先由于是.NET项目转Android,原有数据库使用的是SQLSERVER,由于项目相同部分结构 ...

  5. 解决mysql导入导出数据乱码问题

    最近在linux上面用mysqldump导出数据,放在windows系统中导入就会出现中文乱码,然后就会导致出现: Unknown MySQL server host和Can't connect to ...

  6. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  7. oracle中导入导出数据备份数据库

    原文:oracle中导入导出数据备份数据库 数据库所在位置                         将数据导出到的文件名                    用户名 备份数据库 :exp c ...

  8. PLSQL导入/导出数据方法

    PLSQL导入/导出数据方法 PLSQL导入/导出数据方法 以前导数据库信息的时候,总是会先开启sql窗口,把自己手写的建表文件复制进去,然后再导入数据信息. 今天突然懒得去找以前的建表文件,而想用S ...

  9. oracle10g和oracle11g导入导出数据区别

    其中flxuser为用户名,flxuser为密码,file值为导入到数据库中的备份文件. oracle10g和oracle11g导入导出数据的命令方式大有不同: oracle10g导入数据: imp  ...

随机推荐

  1. zabbix--基础概念及原理

    zabbix 基础概念及工作原理整理 什么是 zabbix? Zabbix 能监控各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位.解决存在的各种问题.是一个基于 W ...

  2. 如果centos7添加新网卡,系统不识别的解决办法

    #ifconfig 2.获取新增网卡的真实mac #ip addr 3.复制eth0到eth1并修改配置文件 #cd /etc/sysconfig/network-scripts #cp ifcfg- ...

  3. C++类库开发详解(转)

    前言:这是一篇总结性的文章,需要有一点C++和dll基本知识的基础,在网上查阅了很多资料感觉没有一篇详细.具体.全面的dll开发介绍,我这是根据最近项目和网上资料整理出来的,并附带实例的一个总结性的文 ...

  4. "<<"和“>>”运算

  5. Event 事件(最简单实用)

    public partial class Form1 : Form { /// <summary> /// 定义事件 /// </summary> public event E ...

  6. oracle封装OracleHelper

    1.Oracle 封装类 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  7. [React] Handle React Suspense Errors with an Error Boundary

    Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...

  8. vim命令(转)

    1.Linux下创建文件 vi test.txt 或者 vim test.txt 或者 touch test.txt 2.vi/vim 使用 基本上 vi/vim 共分为三种模式,分别是命令模式(Co ...

  9. 如何进行seo优化要点总结

    一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...

  10. Java程序设计学习知识点总结

    Java程序设计学习知识点总结 Java语言简单,面向对象,分布式,解释性,健壮,安全与系统无关,可移植,高性能,多线程,动态语言. 什么是框架 可以认为是某种应用的半成品,就是一组组件用来完善自己的 ...