如何在PHP中使用PhpSpreadsheet操作Excel?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
專業(yè)成都網(wǎng)站建設公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設,五站合一網(wǎng)站設計制作,服務好的網(wǎng)站設計公司,成都做網(wǎng)站、網(wǎng)站建設負責任的成都網(wǎng)站制作公司!PhpSpreadsheet是一個用純PHP編寫的庫,提供了一組類,使您可以讀取和寫入不同的電子表格文件格式
PhpSpreadsheet提供了豐富的API接口,可以設置諸多單元格以及文檔屬性,包括樣式、圖片、日期、函數(shù)等等諸多應用,總之你想要什么樣的Excel表格,PhpSpreadsheet都能做到
使用 PhpSpreadsheet 開發(fā)的PHP要求 7.1或更高版本
PhpSpreadsheet 支持鏈式操作
https://phpspreadsheet.readthedocs.io
composer require phpoffice/phpspreadsheet
2、獲取工作簿
getActiveSheet
getActiveSheet();
兩種獲取單元格方式
getCell
getCellByColumnAndRow
getActiveSheet(); # 獲取單元格 $cell = $sheet->getCell('A1'); $cell = $sheet->getCellByColumnAndRow(1,1);
setValue
參數(shù):單元格的值
getActiveSheet(); # 獲取單元格 $cellA = $sheet->getCell('A1'); # 設置單元格值 $cellA->setValue('歐陽克'); # 獲取單元格 $cellB = $sheet->getCellByColumnAndRow(1,2); # 設置單元格值 $cellB->setValue('黃蓉'); # 獲取設置單元格,鏈式操作 $sheet->getCell('A3')->setValue('郭靖'); $sheet->getCellByColumnAndRow(1,4)->setValue('楊康');
getValue 獲取單元格值
getCoordinate 獲取單元格坐標
getActiveSheet(); # 獲取單元格 $cellA = $sheet->getCell('A1'); # 設置單元格值 $cellA->setValue('歐陽克'); echo '值: ', $cellA->getValue(),PHP_EOL; echo '坐標: ', $cellA->getCoordinate();
getActiveSheet(); # 獲取單元格 $cellA = $sheet->getCell('A1'); # 設置單元格值 $cellA->setValue('歐陽克'); # 獲取單元格 $cellB = $sheet->getCellByColumnAndRow(1,2); # 設置單元格值 $cellB->setValue('黃蓉'); # 獲取設置單元格,鏈式操作 $sheet->getCell('A3')->setValue('郭靖'); $sheet->getCellByColumnAndRow(1,4)->setValue('楊康'); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
setCellValue
參數(shù)1:單元格位置
參數(shù)2:單元格的值
setCellValueByColumnAndRow
參數(shù)1:列位置
參數(shù)2:行位置
參數(shù)3:單元格的值
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黃蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17歲'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getStyle 獲取單元格樣式
getFont 獲取單元格文字樣式
setBold 設置文字粗細
setName 設置文字字體
setSize 設置文字大小
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黃蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17歲'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); $sheet->getStyle('B2')->getFont()->setBold(true)->setName('宋體')->setSize(20); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getColor() 獲取坐標顏色
setRGB() 設置字體顏色
getRGB() 獲取字體顏色
setARGB() 設置字體顏色
getARGB() 獲取字體顏色
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黃蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17歲'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); $sheet->getStyle('B2')->getFont()->getColor()->setRGB('#AEEEEE'); echo $sheet->getStyle('B2')->getFont()->getColor()->getRGB(),PHP_EOL; $sheet->getStyle('B3')->getFont()->getColor()->setARGB('FFFF0000'); echo $sheet->getStyle('B3')->getFont()->getColor()->getARGB(); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getNumberFormat 獲取格式
setFormatCode 設置格式
getActiveSheet(); $sheet->setCellValue('A1','2019-10-10 10:10:10'); $sheet->setCellValue('A2','2019-10-10 10:10:10'); $sheet->getStyle('A2')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
setWrapText 設置文本里的\n符合為:換行
getActiveSheet(); $sheet->setCellValue('A1',"歐陽克\n黃蓉"); $sheet->getStyle('A1')->getAlignment()->setWrapText(true); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getHyperlink 獲取單元格鏈接
setUrl 設置單元格鏈接
getActiveSheet(); $sheet->setCellValue('A1','www.php.cn'); $sheet->getCell('A1')->getHyperlink()->setUrl('http://www.php.cn'); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getActiveSheet(); $sheet->setCellValue('A1','10'); $sheet->setCellValue('B1','15'); $sheet->setCellValue('C1','20'); $sheet->setCellValue('D1','25'); $sheet->setCellValue('E1','30'); $sheet->setCellValue('G1','35'); $sheet->setCellValue('A2', '總數(shù):'); $sheet->setCellValue('B2', '=SUM(A1:G1)'); $sheet->setCellValue('A3', '平均數(shù):'); $sheet->setCellValue('B3', '=AVERAGE(A1:G1)'); $sheet->setCellValue('A4', '最小數(shù):'); $sheet->setCellValue('B4', '=MIN(A1:G1)'); $sheet->setCellValue('A5', '較大數(shù):'); $sheet->setCellValue('B5', '=MAX(A1:G1)'); $sheet->setCellValue('A6', '較大數(shù):'); $sheet->setCellValue('B6', '\=MAX(A1:G1)'); // 使用轉(zhuǎn)義字符 # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
fromArray 從數(shù)組中的值填充工作表
參數(shù)1:數(shù)據(jù)(數(shù)組)
參數(shù)2:去除某個值
參數(shù)3:從哪個位置開始
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->fromArray( [ [1,'歐陽克','18歲','188cm'], [2,'黃蓉','17歲','165cm'], [3,'郭靖','21歲','180cm'] ], 3, 'A2' ); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getActiveSheet(); $sheet->mergeCells('A1:B5'); $sheet->getCell('A1')->setValue('歐陽克'); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
合并后,賦值只能給A1,開始的坐標。
getActiveSheet(); $sheet->mergeCells('A1:B5'); $sheet->unmergeCells('A1:B5'); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getColumnDimension 獲取一列
getWidth 獲取一列的寬度
setWidth 設置一列的寬度
setAutoSize 設置一列的寬度自動調(diào)整
getDefaultColumnDimension 獲取一列的默認值
getActiveSheet(); echo $sheet->getColumnDimension('A')->getWidth(); $sheet->getColumnDimension('A')->setWidth(100); $sheet->getColumnDimension('B')->setAutoSize(true); $sheet->getDefaultColumnDimension()->setWidth(1); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getRowDimension 獲取一行
getRowHeight 獲取一行的高度
setRowHeight 設置一行的高度
getActiveSheet(); echo $sheet->getRowDimension(1)->getRowHeight(); $sheet->getRowDimension(1)->setRowHeight(100); $sheet->getDefaultRowDimension()->setRowHeight(1); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
getHighestColumn 獲取總列數(shù)
getHighestRow 獲取總行數(shù)
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黃蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17歲'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); echo $sheet->getHighestColumn(); echo $sheet->getHighestRow(); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
applyFromArray
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $styleArray = [ // use PhpOffice\PhpSpreadsheet\Style\Alignment; 文件里常量,就是參數(shù) // Alignment::HORIZONTAL_CENTER 水平居中 // Alignment::VERTICAL_CENTER 垂直居中 'alignment' => [ // 'horizontal' => Alignment::HORIZONTAL_CENTER, //水平居中 // 'vertical' => Alignment::VERTICAL_CENTER, //垂直居中 'horizontal' => 'center', //水平居中 'vertical' => 'center', //垂直居中 ], // use PhpOffice\PhpSpreadsheet\Style\Border; 文件里常量,就是參數(shù) // Border::BORDER_THICK 邊框樣式 'borders' => [ 'outline' => [ // 'borderStyle' => '\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK', 'borderStyle' => 'thick', 'color' => ['argb' => 'FFFF0000'], ], ], 'font' => [ 'name' => '黑體', 'bold' => true, 'size' => 22 ] ]; $sheet->getStyle('A1')->applyFromArray($styleArray); # Xlsx類 將電子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
IOFactory::createWriter 寫入到文件
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); // MIME 協(xié)議,文件的類型,不設置,會默認html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 協(xié)議的擴展 header('Content-Disposition:attachment;filename=1.xlsx'); // 緩存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一個只寫數(shù)據(jù)流, 允許你以 print 和 echo一樣的方式寫入到輸出緩沖區(qū)。 $writer->save('php://output');
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $filename = '1.xls'; header('Content-Type:application/vnd.ms-excel'); header('Content-Disposition:attachment;filename=1.xls'); header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); $writer->save('php://output');
setTitle
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年齡'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '歐陽克'); $sheet->setCellValueByColumnAndRow(3, 2, '18歲'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setTitle('歐陽克'); // MIME 協(xié)議,文件的類型,不設置,會默認html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 協(xié)議的擴展 header('Content-Disposition:attachment;filename=1.xlsx'); // 緩存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一個只寫數(shù)據(jù)流, 允許你以 print 和 echo一樣的方式寫入到輸出緩沖區(qū)。 $writer->save('php://output');
load('1.xlsx'); # 獲取活動工作薄 $sheet = $spreadsheet->getActiveSheet(); # 獲取 單元格值 和 坐標 $cellC1 = $sheet->getCell('B2'); echo '值: ', $cellC1->getValue(),PHP_EOL; echo '坐標: ', $cellC1->getCoordinate(),PHP_EOL; $sheet->setCellValue('B2','歐陽鋒'); # 獲取 單元格值 和 坐標 $cellC2 = $sheet->getCell('B2'); echo '值: ', $cellC2->getValue(),PHP_EOL; echo '坐標: ', $cellC2->getCoordinate();
后綴 | MIME Type |
---|---|
.doc | application/msword |
.dot | application/msword |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template |
.docm | application/vnd.ms-word.document.macroEnabled.12 |
.dotm | application/vnd.ms-word.template.macroEnabled.12 |
.xls | application/vnd.ms-excel |
.xlt | application/vnd.ms-excel |
.xla | application/vnd.ms-excel |
.xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xltx | application/vnd.openxmlformats-officedocument.spreadsheetml.template |
.xlsm | application/vnd.ms-excel.sheet.macroEnabled.12 |
.xltm | application/vnd.ms-excel.template.macroEnabled.12 |
.xlam | application/vnd.ms-excel.addin.macroEnabled.12 |
.xlsb | application/vnd.ms-excel.sheet.binary.macroEnabled.12 |
.ppt | application/vnd.ms-powerpoint |
.pot | application/vnd.ms-powerpoint |
.pps | application/vnd.ms-powerpoint |
.ppa | application/vnd.ms-powerpoint |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.potx | application/vnd.openxmlformats-officedocument.presentationml.template |
.ppsx | application/vnd.openxmlformats-officedocument.presentationml.slideshow |
.ppam | application/vnd.ms-powerpoint.addin.macroEnabled.12 |
.pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
.potm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
.ppsm | application/vnd.ms-powerpoint.slideshow.macroEnabled.12 |
login_log 登陸日志表
CREATE TABLE `login_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) DEFAULT NULL COMMENT '管理員ID', `client` tinyint(4) unsigned DEFAULT '0' COMMENT '0-PC 1-ios 2-android', `add_time` int(11) DEFAULT '0' COMMENT '創(chuàng)建時間', `ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '登錄IP', PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1122 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='登錄日志';
index.php 數(shù)據(jù)列表頁面
導出數(shù)據(jù)
ID | 用戶ID | 登陸設備 | 登陸時間 | 登陸ip |
---|---|---|---|---|
download.php 導出操作
getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','用戶ID'); $sheet->setCellValue('C1','登陸設備'); $sheet->setCellValue('D1','登陸時間'); $sheet->setCellValue('E1','登陸ip'); $sheet->fromArray( $select, null, 'A2' ); // MIME 協(xié)議,文件的類型,不設置,會默認html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 協(xié)議的擴展 header('Content-Disposition:attachment;filename=1.xlsx'); // 緩存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一個只寫數(shù)據(jù)流, 允許你以 print 和 echo一樣的方式寫入到輸出緩沖區(qū)。 $writer->save('php://output'); ?>
數(shù)據(jù)庫
# 商品分類表 CREATE TABLE `shop_cat` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `pid` int(10) unsigned DEFAULT '0' COMMENT '父ID', `name` varchar(50) DEFAULT NULL COMMENT '分類名', `status` tinyint(1) unsigned DEFAULT '1' COMMENT '狀態(tài) 1開啟 0關閉', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COMMENT='分類表'; INSERT INTO `shop_cat` VALUES (1, 0, '女裝', 1); INSERT INTO `shop_cat` VALUES (2, 0, '男裝', 1); INSERT INTO `shop_cat` VALUES (3, 0, '孕產(chǎn)', 1); INSERT INTO `shop_cat` VALUES (4, 1, '連衣裙', 1); INSERT INTO `shop_cat` VALUES (5, 1, '牛仔褲', 1); INSERT INTO `shop_cat` VALUES (6, 2, '襯衫', 1); INSERT INTO `shop_cat` VALUES (7, 3, '睡衣', 1); # 商品表 CREATE TABLE `shop_list` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `cat_id` int(10) unsigned DEFAULT NULL COMMENT '分類ID', `cat_fid` int(10) unsigned DEFAULT NULL COMMENT '分類父ID', `title` varchar(200) NOT NULL COMMENT '商品標題', `price` double(10,2) unsigned NOT NULL COMMENT '價格', `img` varchar(200) NOT NULL COMMENT '商品圖片', `add_time` int(10) unsigned NOT NULL COMMENT '添加時間', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
index.php 導入頁面
導出數(shù)據(jù)
導入功能
load($file); # 獲取活動工作薄 $sheet = $spreadsheet->getActiveSheet(); # 獲取總列數(shù) $highestColumn = $sheet->getHighestColumn(); # 獲取總行數(shù) $highestRow = $sheet->getHighestRow(); # 列數(shù) 改為數(shù)字顯示 $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); $log = []; for($a=2;$a<$highestRow;$a++){ $title = $sheet->getCellByColumnAndRow(1,$a)->getValue(); $cat_fname = $sheet->getCellByColumnAndRow(2,$a)->getValue(); $cat_name = $sheet->getCellByColumnAndRow(3,$a)->getValue(); $price = $sheet->getCellByColumnAndRow(4,$a)->getValue(); $img = $sheet->getCellByColumnAndRow(5,$a)->getValue(); $cat_fid = find('shop_cat','id','name="'.$cat_fname.'"'); $cat_id = find('shop_cat','id','name="'.$cat_name.'"'); $data = [ 'title' => $title, 'cat_fid' => $cat_fid['id'], 'cat_id' => $cat_id['id'], 'price' => $price, 'img' => $img, 'add_time' => time(), ]; $ins = insert('shop_list',$data); if($ins){ $log[] = '第'.$a.'條,插入成功'; }else{ $log[] = '第'.$a.'條,插入失敗'; } } echo json_encode(['code'=>0,'msg'=>'成功','data'=>$log]);
下載示例(范文)
getActiveSheet(); $sheet->setCellValue('A1','商品標題'); $sheet->setCellValue('B1','一級分類'); $sheet->setCellValue('C1','二級分類'); $sheet->setCellValue('D1','進貨價'); $sheet->setCellValue('E1','圖片'); $data = [ '云朵般輕盈的仙女裙 高級釘珠收腰長裙 氣質(zhì)無袖連衣裙', '女裝', '連衣裙', 279.99, 'https://gd3.alicdn.com/imgextra/i3/266969832/O1CN01PWUBBB2MV6ekBKtb6_!!266969832.jpg_400x400.jpg', ]; $sheet->fromArray( $data, null, 'A2' ); // MIME 協(xié)議,文件的類型,不設置,會默認html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 協(xié)議的擴展 header('Content-Disposition:attachment;filename=商品列表示例.xlsx'); // 緩存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一個只寫數(shù)據(jù)流, 允許你以 print 和 echo一樣的方式寫入到輸出緩沖區(qū)。 $writer->save('php://output');
php是一個嵌套的縮寫名稱,是英文超級文本預處理語言,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因為php是開源的,從而使得php經(jīng)久不衰。
看完上述內(nèi)容,你們掌握如何在PHP中使用PhpSpreadsheet操作Excel的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!