thinkphp5.1 框架導(dǎo)入/導(dǎo)出excel文件操作示例
本文實例講述了thinkphp5.1 框架導(dǎo)入/導(dǎo)出excel文件操作。分享給大家供大家參考,具體如下:
thinkphp5.1 導(dǎo)入excel文件
public function importExcel()
{
try {
//獲取表格的大小,限制上傳表格的大小
if ($_FILES['file']['size'] > 10 * 1024 * 1024) { //文件過大
log_debug($log_title . 'END === MSG:' . '文件過大');
parent::endBack(['state' => 0, 'msg' => '文件過大']);
}
//限制上傳表格類型
$ext = substr(strrchr($_FILES['file']["name"], '.'), 1);
if ($ext != 'xls' && $ext != 'xlsx') {
log_debug($log_title . 'END === MSG:' . '文件格式不正確');
parent::endBack(['state' => 0, 'msg' => '上傳文件必須為excel表格']);
}
//讀取表格
$filename = $_FILES['file']['tmp_name'];
$reader = IOFactory::createReader('Xlsx'); //Xls,Xlsx都可讀取
$canRead = $reader->canRead($filename);
if (!$canRead) {
log_debug($log_title . 'END,文件格式不正確,SQL:' . Db::name('')->getLastSql());
parent::endBack(['state' => 0, 'msg' => '文件格式不正確', 're_login' => false]);
}
$spreadsheet = $reader->load($filename); //載入excel表格
$worksheet = $spreadsheet->getActiveSheet(); //選中sheet表
$highestRow = $worksheet->getHighestRow(); // 總行數(shù)
// $highestColumn = $worksheet->getHighestColumn(); // 總列數(shù)
if (!(0 < $highestRow)) {
log_debug($log_title . 'END,文件內(nèi)容空,SQL:' . Db::name('')->getLastSql());
parent::endBack(['state' => 0, 'msg' => '文件沒有數(shù)據(jù)', 're_login' => false]);
}
//循環(huán)讀取--有效判斷
$sst_word_arr = []; //存放敏感詞的數(shù)組
for ($row = 1; $row <= $highestRow; $row++) {
//取列數(shù)A列的數(shù)據(jù)
$tmp_word = $spreadsheet->getActiveSheet()->getCell('A' . $row)->getValue();
if ('' != trim($tmp_word) && null != $tmp_word) {
$sst_word_arr[] = $tmp_word;
break; //發(fā)現(xiàn)有效數(shù)據(jù),直接退出,接下來插入數(shù)據(jù)
}
}
// $sst_word_arr = array_unique($sst_word_arr);
if (empty($sst_word_arr)) {
log_debug($log_title . 'END,文件無有效數(shù)據(jù),SQL:' . Db::name('')->getLastSql());
parent::endBack(['state' => 0, 'msg' => '文件無有效數(shù)據(jù)', 're_login' => false]);
}
//判斷和數(shù)據(jù)庫操作
for ($row = 2; $row <= $highestRow; $row++) {
//取列數(shù)A列的數(shù)據(jù)
$tmp_old_car_num = $spreadsheet->getActiveSheet()->getCell('A' . $row)->getValue();
$car_num = trim($tmp_old_car_num);
if ('' != $car_num && null != $car_num) {
//數(shù)據(jù)庫操作
}
}
}
$ret_arr = [
'state' => 1,
//返回數(shù)據(jù)
];
log_debug($log_title . 'END,SUCCESS');
parent::endBack($ret_arr);
} catch (\Exception $e) {
//
}
}
excel文件格式為:

thinkphp5.1 導(dǎo)出excel文件
namespase app\test;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class test {
public function carNumsExport()
{
$log_title = '測試 => 車牌列表導(dǎo)出[' . __METHOD__ . '] ';
try {
$file_name = '《車牌列表》from y8zh - ' . $user_info['uid'] . '.xlsx';
$file_relative_path = parent::$module_name . DIRECTORY_SEPARATOR . 'fcb_car_nums' . DIRECTORY_SEPARATOR;
$file_path = parent::$api_file_root_path . $file_relative_path;
// 已生成過則直接返回
if (file_exists($file_path . $file_name)) {
$ret_arr = [
'state' => 1,
'download_url' => parent::$api_file_get_url . $file_relative_path . $file_name,
];
parent::endBack($ret_arr);
}
if (!is_dir($file_path)) {
mkdir($file_path, 0777, true);
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//獲取所有車牌號
$car_nums = Db::connect('db_config_yun')->name('vechicle')->column('DISTINCT number');
$i = 1;
$sheet->setCellValue('A' . $i, '車牌號')->getStyle('A' . $i)->getFont()->setBold(true);
$i++;
// 表內(nèi)容
if (!empty($car_nums)) {
foreach ($car_nums as $k_c => $v_c) {
$sheet->setCellValue('A' . $i, $v_c);
$i++;
}
}
$writer = new Xlsx($spreadsheet);
$writer->save($file_path . $file_name);
$ret_arr = [
'state' => 1,
'download_url' => parent::$api_file_get_url . $file_relative_path . $file_name,
];
log_debug($log_title . 'END === DOWNLOAD_URL:' . $ret_arr['download_url']);
parent::endBack($ret_arr);
} catch (\Exception $e) {
//
}
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
Zend?Framework框架實現(xiàn)發(fā)送郵件的方法
這篇文章主要介紹了Zend?Framework框架實現(xiàn)發(fā)送郵件的方法,實例分析了Zend?Framework使用smtp郵件類實現(xiàn)郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
yii2.0數(shù)據(jù)庫遷移教程【多個數(shù)據(jù)庫同時同步數(shù)據(jù)】
這篇文章主要介紹了yii2.0數(shù)據(jù)庫遷移的方法,可實現(xiàn)多個數(shù)據(jù)庫同時同步數(shù)據(jù)的功能,較為詳細(xì)的分析了Yii2針對遷移的創(chuàng)建、提交、重做及自定義遷移的相關(guān)概念與使用方法,需要的朋友可以參考下2016-10-10

