当前位置:网站首页>php使用phpoffice/phpspreadsheet导入导出excel表格
php使用phpoffice/phpspreadsheet导入导出excel表格
2022-08-09 04:43:00 【flysnownetwork】
phpoffice/phpspreadsheet引入
composer require phpoffice/phpspreadsheet
导出
可以直接数组导出,注释里代码为循环进行单元格赋值房是
<?php
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\IOFactory;
class ErpOrder extends Model
{
public function export($adminId)
{
try {
$list = $this->field('order_number,good_title,sku,good_number,user_name,user_mobile,user_address,express_number')
->where(['status' => 2,'send_status' => 1,'partner_admin_id' => $adminId])
->select()
->toArray();
//导出表格
$objExcel = new Spreadsheet();
$objWriter = IOFactory::createWriter($objExcel, 'Xlsx');
$objActSheet = $objExcel->getActiveSheet(0);
$objActSheet->setTitle('发货单');//设置excel的标题
$keys= ['订单号','商品名称','sku','数量','收件人姓名','联系电话','收货地址','物流单号'];
array_unshift($list,$keys);
$objActSheet->fromArray($list);
// $objActSheet->setCellValue('A1', '订单号');
// $objActSheet->setCellValue('B1', '商品名称');
// $objActSheet->setCellValue('C1', 'sku');
// $objActSheet->setCellValue('D1', '数量');
// $objActSheet->setCellValue('E1', '收件人姓名');
// $objActSheet->setCellValue('F1', '联系电话');
// $objActSheet->setCellValue('G1', '收货地址');
// $objActSheet->setCellValue('H1', '物流单号');
// $baseRow = 3; //数据从N-1行开始往下输出
// foreach ($list as $r => $d) {
// $i = $baseRow + $r;
// $objExcel->getActiveSheet()->setCellValue('A' . $i, $d['date']);
// $objExcel->getActiveSheet()->setCellValue('B' . $i, $d['num']);
// $objExcel->getActiveSheet()->setCellValue('C' . $i, $d['recharge']);
// $objExcel->getActiveSheet()->setCellValue('D' . $i, $d['rate']);
// $objExcel->getActiveSheet()->setCellValue('E' . $i, $d['invest_money']);
// $objExcel->getActiveSheet()->setCellValue('F' . $i, $d['refund_money']);
// $objExcel->getActiveSheet()->setCellValue('G' . $i, $d['money']);
// $objExcel->getActiveSheet()->setCellValue('H' . $i, $d['status']);
// }
$objWriter->save(__DIR__.'/a12.xlsx');
return ['data' => '', 'code' => 0, 'msg' => 'success'];
} catch (Throwable $e) {
return ['data' => '', 'code' => $e->getCode(), 'msg' => $e->getMessage()];
}
}
}
导入
/** *导入 * @param $url * @param $adminId * @return array * @throws \think\exception\PDOException * @date 2022-08-04 14:40 */
public function import($url,$adminId)
{
try {
$this->startTrans();
$filename = tempnam(sys_get_temp_dir(), "xlsx");
$handle = fopen($filename, "wb");
fwrite($handle, file_get_contents($url));
$objReader = IOFactory::createReader('Xlsx');
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格
$data = $objPHPExcel->getSheet(0)->toArray();//将sheet1表中的数据转换为数组类型数据
// $keys=array_shift($data);//从数组中删除第一条数据,返回值为删除的数据
$keys = ['order_number', 'good_title', 'sku', 'good_number', 'user_name', 'user_mobile', 'user_address', 'express_number'];
array_shift($data);
foreach ($data as $key => $value)
$list[] = array_combine($keys, $value);
foreach ($list as $k => $v) {
if (!empty($v['order_number'])) {
$info = $this->where(['order_number' => $v['order_number'], 'partner_admin_id' => $adminId])->find();
$info->express_number = $v['express_number'];
$info->send_status = 2;
$info->send_time = time();
$info->save();
}
}
$this->commit();
return ['data' => '', 'code' => 0, 'msg' => 'success'];
} catch (Throwable $e) {
$this->rollback();
return ['data' => '', 'code' => $e->getCode(), 'msg' => $e->getMessage()];
}
}
边栏推荐
- [math] dot product and cross product
- B. Arrays Sum
- 安装pytorch和cuda
- LeetCode - remove consecutive nodes with a sum of zero from a linked list
- 【暑期每日一题】洛谷 P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
- 杰理之一拖二 另一台手机超距 通话会无声【篇】
- [21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
- Divisible property 1
- Alibaba Cloud Tianchi Contest Question (Machine Learning) - Repeat Purchase Prediction of Tmall Users (Complete Code)
- JS-DOM-全局、局部、隐式变量,数组()\函数、 prompt输入对话框、confirm(确定用户的决定-弹出对话框)
猜你喜欢

2022 Security Officer-A Certificate Special Work Permit Exam Question Bank and Online Mock Exam

杰理之智能充电仓低电发码关机 触摸不开机【篇】

【服务器数据恢复】Ext4文件系统fsck后mount不上并报错的数据修复案例
Improve the user experience and add a small detail to your modal popup

软件质效领航者 | 优秀案例•国金证券DevOps建设项目

I.MX6U-ALPHA开发板(高精度定时器)

软件质效领航者 | 优秀案例•东风集团DevOps改革项目

337. 打家劫舍 III

杰理之采用mix out eq 没有作用【篇】
Construction and practice of full stack code test coverage and use case discovery system
随机推荐
LeetCode-从链表中删去总和值为零的连续结点
Understanding ML Cross Validation Fast
JVM垃圾回收机制简介
Dingding conflicts with RStudio shortcuts--Dingding shortcut settings
【二叉树】重建二叉树
pr22.5最新版下载地址
`数学` 极限, 渐进分析, 近似阶, 线性化, 线性近似, 线性函数
337. 打家劫舍 III
杰理之智能充电仓低电发码关机 触摸不开机【篇】
"IP" command to configure network interface
Detailed explanation of Oracle's windowing function
2022高压电工考试试题及答案
How to do the stability test, this article thoroughly explains it!
BaseDexClassLoader的正确使用方式
阿里云天池大赛赛题(机器学习)——工业蒸汽量预测(完整代码)
Gopacket source code analysis
The influence law of genes for disease - read the paper
【暑期每日一题】洛谷 P1048 [NOIP2005 普及组] 采药
TASSEL软件导入plink格式文件报错
2022 Security Officer-A Certificate Special Work Permit Exam Question Bank and Online Mock Exam