導出功能參考:http://www.cnblogs.com/zhouqi666/p/5978017.htmlphp
比較嚴重的問題:當遇到excel數據量比較大的時候,會發生內存溢出的狀況,目前沒法解決html
excel的數據格式爲數據庫
A B數組
前面對應數據庫type,後面對應電話號碼ui
數據庫spa
使用的是THINKPHP3.2.3其實就是鏈接數據庫插入的時候用到和引用加載文件的時候用到TP只是了3d
代碼以下excel
//引入PHPExcel 若是不是TP用require_once
vendor("PHPExcel.PHPExcel");
vendor("PHPExcel.IOFactory");
$cacheMethod = \PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize ' => '8MB');
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
// 實例化exel對象
//文件路徑
$file_path = './Public/excel/test1.xlsx';
if (!file_exists($file_path)){
die('file not exists');
}
//文件的擴展名
$ext = strtolower(pathinfo($file_path,PATHINFO_EXTENSION));
if ($ext == 'xlsx'){
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file_path);
}elseif($ext == 'xls'){
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_path);
}
$objReader->setReadDataOnly(true);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();//獲取總行數
$highestColumn = $sheet->getHighestColumn();//獲取總列數
//10 B
for ($i = 1;$i<=$highestRow;$i++){
$record = array();//申明每條記錄數組
for ($j = 'A';$j<=$highestColumn;$j++){
$record[] = $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue();//讀取單元格
}
$record['type'] = $record[0];
$record['phone'] = $record[1];
$res = D('Test')->add($record);
header("Content-type:text/html;charset=utf-8");
if($res == false){
echo '插入數據出錯!數據格式爲:';
dump($record);
die;
}
}htm