Home > Backend Development > PHP Tutorial > PHP processes excel file data

PHP processes excel file data

不言
Release: 2023-03-24 21:02:01
Original
2484 people have browsed it

The content of this article is about PHP processing excel file data. It has certain reference value. Now I share it with you. Friends in need can refer to it.

The article mainly records how to use the PHPexcel plug-in to process excel. The file processing process is only used as a reminder. If you encounter problems downloading the plug-in, you can leave a message. Thank you for browsing. The following is the note content:

1. Introduce the excel plug-in file

require_once('./PhpSpreadsheet/vendor/autoload.php');
Copy after login

2. Get the excel table data content

//创建文件读取类对象
$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;
        }
    }
}
Copy after login

The output format content is as follows:

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*/
Copy after login

3. Get the parts of the data that you find useful:

//获取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;
}
Copy after login

You can directly process the data later.

Related recommendations:

php processing array key value changes

php method of processing form upload files

The above is the detailed content of PHP processes excel file data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template