本文實(shí)例講述了ThinkPHP 框架實(shí)現(xiàn)的讀取excel導(dǎo)入數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
入口文件中:
require_once VENDOR_PATH.'PHPExcel/PHPExcel/IOFactory.php';
require_once VENDOR_PATH.'PHPExcel/PHPExcel.php';
PHP:
namespace Home\Controller;
class ExcelController extends CommonController
{
public function Import() {
// vendor('PHPExcel.PHPExcel.IOFactory');
vendor("PHPExcel.PHPExcel.PHPExcel");
vendor("PHPExcel.PHPExcel.Writer.Excel5");
vendor("PHPExcel.PHPExcel.Writer.Excel2007");
//$excel = new PHPExcel();
$fileName = './trans_rate.xlsx';
date_default_timezone_set('PRC');
// 讀取excel文件
try {
$objPHPExcel = \PHPExcel_IOFactory::load($fileName);
$inputFileType = \PHPExcel_IOFactory::identify($fileName);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
// $objPHPExcel = $objReader->load($fileName);
// 確定要讀取的sheet $sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// 獲取一行的數(shù)據(jù)
// $phone_str = '';
for ($row = 3; $row = $highestRow; $row++) {
$row_data = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
//獲取excel表中一行的數(shù)組數(shù)據(jù)
//dump($row_data);
$row_data = $row_data[0];
$time = date('Y-m-d H:i:s', strtotime(trim($row_data[0])));
$start_province = trim($row_data[1]);
$start_city = trim($row_data[2]);
...
// $phone_str .= '"' . $phone . '",';
$where['phone'] = $phone;
$id_arr = M(數(shù)據(jù)表名)->where($where)->getField('id');
$user_id = !empty($id_arr) ? $id_arr : 0;
$fields[] = [
'數(shù)據(jù)表字段' => $user_id,//用戶id
...
];
}
// dump($fields);
$rate_add = M(數(shù)據(jù)表名)->addAll($fields);
dump($rate_add);
echo M()->getLastSql();
if (!(0 $rate_add)) {
CommonController::logProfile('添加excel數(shù)據(jù),SQL:' . M()->getLastSql()); $this->endBack(0); }
// echo $phone_str . 'br />';
// dump($user_id);
} catch (Exception $e) {
die('加載文件發(fā)生錯(cuò)誤:"' . pathinfo($fileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } }}
php讀取excel表數(shù)據(jù):
?php
include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php';
$inputFileName = './trans_rate.xlsx';
date_default_timezone_set('PRC');
// 讀取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('加載文件發(fā)生錯(cuò)誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// 確定要讀取的sheet
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// 獲取一行的數(shù)據(jù)
for ($row = 1; $row = $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
//這里得到的rowData都是一行的數(shù)據(jù),得到數(shù)據(jù)后自行處理
var_dump($rowData);
echo "br>";
}
//$data為從excel中獲取到的數(shù)組
for ($i =0; $icount($data);$i++){
echo 'br>';
$gettime= explode('-',$data[$i][0]);
if (checkdate($month=$gettime[0],$day=$gettime[1],$year=$gettime[2])){
echo gmdate('Y-m-d',gmmktime(0,0,0,$month,$day,$year));
}else{
echo ($data[$i][0]);
}
echo '-----------';
echo $data[$i][1];
}
?php
include 'ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php';
$inputFileName = './test.xlsx';
date_default_timezone_set('Asia/Shanghai');
// 讀取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
// 確定要讀取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();//最大行
$highestColumn = $sheet->getHighestColumn();//最大列
$data = array();
for($rowIndex=2;$rowIndex=$highestRow;$rowIndex++){ //循環(huán)讀取每個(gè)單元格的內(nèi)容。注意行從1開始,列從A開始
for($colIndex='A';$colIndex=$highestColumn;$colIndex++){
$addr = $colIndex.$rowIndex;
if($colIndex==="A"){ //指定H列為時(shí)間所在列
$cell = gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue()));
// $cell = PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell($addr)->getValue());
// var_dump($cell);die;
}else{
$cell = $sheet->getCell($addr)->getValue();
}
// if($cell instanceof PHPExcel_RichText){ //富文本轉(zhuǎn)換字符串
// $cell = $cell->__toString();
// }
$data[$rowIndex][$colIndex] = $cell;
}
}
// return $data;
var_dump($data);
} catch(Exception $e) {
die('加載文件發(fā)生錯(cuò)誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- thinkPHP5框架導(dǎo)出Excel文件簡(jiǎn)單操作示例
- ThinkPHP使用PHPExcel實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出完整實(shí)例
- ThinkPHP基于PHPExcel導(dǎo)入Excel文件的方法
- 詳解thinkphp實(shí)現(xiàn)excel數(shù)據(jù)的導(dǎo)入導(dǎo)出(附完整案例)
- ThinkPHP框架實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的方法示例【基于PHPExcel】
- thinkPHP實(shí)現(xiàn)將excel導(dǎo)入到數(shù)據(jù)庫中的方法
- Dwz與thinkphp整合下的數(shù)據(jù)導(dǎo)出到Excel實(shí)例
- thinkphp3.2中實(shí)現(xiàn)phpexcel導(dǎo)出帶生成圖片示例
- 基于ThinkPHP+uploadify+upload+PHPExcel 無刷新導(dǎo)入數(shù)據(jù)
- thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
- thinkphp5.1 框架導(dǎo)入/導(dǎo)出excel文件操作示例