1. 安裝phpexcel
composer require phpoffice/phpexcel
composer update
2. 前端
<form class="forms-sample" action="../../xxxx/xxxx/do_import_users" method="post" enctype="multipart/form-data">
<div class="control-group row">
<label>Excel表格:</label>
<input type="file" name="users_excel"/>
</div>
<br>
<button type="submit" class="btn btn-primary mr-2">導(dǎo)入</button>
</form>
3.?后端
use PHPExcel_IOFactory; //通過composer加載的第三方類,直接在頭部引入一下就可以
/**
* 批量新增用戶
*/
public function do_import_users()
{
// users_excel為變量名
if(!request()->file('users_excel')){
return $this->error('請(qǐng)上傳excel文件');
}
$path = request()->file('users_excel');
//實(shí)例化PHPExcel類
$PHPExcel = new \PHPExcel();
//默認(rèn)用excel2007讀取excel,若格式不對(duì),則用之前的版本進(jìn)行讀取
$PHPReader = new \PHPExcel_Reader_Excel2007();
if (!$PHPReader->canRead($path)) {
$PHPReader = new \PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($path)) {
return $this->error('請(qǐng)上傳excel文件');
}
}
//讀取Excel文件
$PHPExcel = $PHPReader->load($path);
//讀取excel文件中的第一個(gè)工作表
$sheet = $PHPExcel->getSheet(0);
//取得最大的列號(hào),注意,是列號(hào),不是有多少列,比如Q
$allColumn = $sheet->getHighestColumn();
//取得最大的行號(hào)
$allRow = $sheet->getHighestRow();
// 第一行是列名,從第二行開始插入
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
//獲取B列的值
$data = [
'number'=>$PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue(),
'nickName'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),
'name'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),
'tel'=>$PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue(),
'money'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),
'time'=>self::get_date_by_excel($PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue()),
'is_pay'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),
'shop_name'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),
'remarks'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),
'status'=>0,
'created_at'=>date('Y-m-d')
];
// 其它操作,比如插入數(shù)據(jù)庫
}
}
/**
* excel里的字符串時(shí)間轉(zhuǎn)時(shí)間
*/
public static function get_date_by_excel($date){
if (!$date || $date == '0000-00-00') return null;
$unix_time = \PHPExcel_Shared_Date::ExcelToPHP($date);
return gmdate('Y-m-d H:i',$unix_time);
}
注:現(xiàn)在主要使用phpoffice/phpspreadsheet庫,phpoffice/phpexcel已經(jīng)不再維護(hù),但本文由于環(huán)境無法更新,所以就安裝了phpoffice/phpexcel。基本操作一樣。
參考:https://www.tpxhm.com/fdetail/725.html
注:
一、在ubuntu環(huán)境下提示?Class 'ZipArchive' not found
?查看對(duì)應(yīng)的php版本
php -v
比如得到的版本是php7.4
那么
sudo apt-get install php7.4-zip
安裝之后重啟服務(wù)
sudo /etc/init.d/apache2 restart
最后通過?php -m | grep zip查看是否安裝成功
之前有將在php.ini中將
zlib.output_compression = Off 改為 zlib.output_compression = On
不知道是否有影響
二、如果報(bào)錯(cuò)Call to undefined function simplexml_load_string()
sudo apt-get install php7.4-xml php7.4-xmlrpc
sudo /etc/init.d/apache2 restart
三、如果報(bào)錯(cuò)Trying to access array offset on value of type int
請(qǐng)看
Trying to access array offset on value of type int-CSDN博客文章瀏覽閱讀1.1w次,點(diǎn)贊8次,收藏6次。問題描述:出現(xiàn)報(bào)錯(cuò)信息 先百度翻譯''試圖訪問int類型值的數(shù)組偏移量''通過翻譯得知 int型的數(shù)據(jù)被其他不能使用的類型使用了(個(gè)人理解)關(guān)于這塊 php7.4升級(jí)之后會(huì)有這個(gè)bug 網(wǎng)上大多人是說7.4 版本的向后不兼容更改,非數(shù)組的數(shù)組樣式訪問,現(xiàn)在,嘗試將 null,bool,int,float 或 resource 類型的值用作數(shù)組 ( 例如 $null[“key”] ) 會(huì)產(chǎn)生一個(gè)通知。解決問題:我這個(gè)報(bào)錯(cuò)在83行 我先打印查詢$pValue查看是什么_trying to access array offset on value of type inthttps://blog.csdn.net/qq5201314wx/article/details/124317321文章來源:http://www.zghlxwxcb.cn/news/detail-851135.html
by: 軟件工程小施同學(xué)文章來源地址http://www.zghlxwxcb.cn/news/detail-851135.html
到了這里,關(guān)于thinkphp6入門(23)-- 如何導(dǎo)入excel的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!