首頁 > 後端開發 > php教程 > PHP處理excel檔案數據

PHP處理excel檔案數據

不言
發布: 2023-03-24 21:02:01
原創
2635 人瀏覽過

這篇文章介紹的內容是關於PHP處理excel文件數據,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

文章主要記錄如何使用PHPexcel外掛程式對excel文件進行處理的過程,僅作備忘使用,如插件下載遇到問題,可以留言,謝謝瀏覽。以下是筆記內容:

1、引入excel外掛檔案

1

require_once('./PhpSpreadsheet/vendor/autoload.php');

登入後複製

2、取得表格資料內容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//创建文件读取类对象

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");

/**  Advise the Reader that we only want to load cell data  **/

//设置为只读模式$reader->setReadDataOnly(true);

/**  Load $inputFileName to a Spreadsheet Object  **/

//加载文件路径,路径为绝对路径

$spreadsheet = $reader->load($file_path);

//获取excel表中有多少个sheet

$sheetNum = $spreadsheet->getSheetCount();

$sheetData = [];

$temp = [];

//只获取活跃的sheet

//$temp = $spreadsheet->getSheetActive()->toArray(null, true, true, true);

//遍历每一个sheet,获取里面的数据内容

for($i = 0; $i < $sheetNum; $i++){   

$temp = $spreadsheet->getSheet($i)->toArray(null, true, true, true);   

foreach ($temp as $v) {       

if(is_float($v[&#39;A&#39;])){           

$sheetData[] = $v;

        }

    }

}

登入後複製

輸出格式內容如下:

1

2

3

4

5

6

7

var_dump($sheetData);

 

/*   

    &#39;A&#39; => float 41746

    &#39;B&#39; => string &#39;玉雪婵娟&#39; (length=12)    &#39;C&#39; => float 236979210

    &#39;D&#39; => string &#39;353343073072008&#39; (length=15)    &#39;E&#39; => float 41740

    &#39;F&#39; => null*/

登入後複製

3.取得資料中自己覺得有用的部分:

1

2

3

4

5

6

7

8

9

10

11

//获取excel表中的数据记录

$excel_records$temp = [];

$excel_records = [];

foreach ($sheetData as $v){   

//strtotime(gmdate("Y-m-d", ($sheetData[$i][&#39;F&#39;] -25569) * 86400))

 

    //将表格中的时间字符串转为时间格式

    $temp[&#39;date&#39;] = gmdate("Y-m-d", ($v[&#39;A&#39;] -25569) * 86400);//2018年获取为2014年,尚不明原因

 

    $temp[&#39;drname&#39;] = $v[&#39;B&#39;];    $temp[&#39;uid&#39;] = $v[&#39;C&#39;];    $temp[&#39;imei&#39;] = $v[&#39;D&#39;];    $temp[&#39;reg_date&#39;] = $v[&#39;E&#39;];    $excel_records[] = $temp;

}

登入後複製

後續直接對資料進行處理即可。

相關推薦:

php處理陣列key值變更

#php處理表單上傳檔案的方法

以上是PHP處理excel檔案數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
php 匯入excel 前台顯示進度條
來自於 1970-01-01 08:00:00
0
0
0
laravel excel求教
來自於 1970-01-01 08:00:00
0
0
0
織夢cms自訂表單資料匯出excel
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板