PHP處理excel檔案資料

來源:互聯網
上載者:User

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

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

1、引入excel外掛程式檔案

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

2、擷取excel表資料內容

//建立檔案讀取類對象$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['A'])){            $sheetData[] = $v;        }    }}

輸出格式內容如下:

var_dump($sheetData);/*        'A' => float 41746    'B' => string '玉雪嬋娟' (length=12)    'C' => float 236979210    'D' => string '353343073072008' (length=15)    'E' => float 41740    'F' => null*/

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

//擷取excel表中的資料記錄$excel_records$temp = [];$excel_records = [];foreach ($sheetData as $v){    //strtotime(gmdate("Y-m-d", ($sheetData[$i]['F'] -25569) * 86400))    //將表格中的時間字串轉為時間格式    $temp['date'] = gmdate("Y-m-d", ($v['A'] -25569) * 86400);//2018年擷取為2014年,尚不明原因    $temp['drname'] = $v['B'];    $temp['uid'] = $v['C'];    $temp['imei'] = $v['D'];    $temp['reg_date'] = $v['E'];    $excel_records[] = $temp;}

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.