通过PHPExcle把excle表导入数据库,变成数据表;也就是一个excle表对应就是一个数据表:
Excle表里的格式为:
. | A | B | C |
---|---|---|---|
1 | 对应数据库的字段 | 对应数据库的字段 | 对应数据库的字段 |
2 | 字段的注释 | 字段的注释 | 字段的注释 |
3 | 值 | 值 | 值 |
代码:
<?php
header("Content-Type:text/html;charset=utf-8");
error_reporting(0);
define('FY_MTIME',microtime(TRUE));
require_once 'inc/config.php';
$d = dir('xls/');
while (($file = $d->read()) !== false){
if(substr(strrchr($file, '.'), 1) == 'xls'){
$arr[] = $file;
}
}
//我是把所有xls文件列出列表,勾选提交到这边$_POST
foreach($_POST as $k => $item){
foreach($item as $v){
getExeclData('xls/'.$arr[$v]);
}
}
/**
* 获取Execl表格数据
*/
function getExeclData($file)
{
//首先导入PhPExcel
require_once 'PHPExcel/PHPExcel.php';
$filePath = $file;
if ($filePath == null || $filePath == '') {
return;
}
$PHPReader = new PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filePath)) {
echo 'no Excel';
return;
}
//建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath);
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//下面改成'!=',所以把$allColumn+1,'<='就有问题
$allColumn++;
//获取excle表中第一行的字段名
$hdata = array();
$null = array();
for($colIndex='A'; $colIndex != $allColumn; $colIndex++){
$haddr = $colIndex . 1; //编号A1、B1
$hcell = $currentSheet->getCell($haddr)->getValue(); //编号对应的值
if(!empty($hcell)){
array_push($hdata, $hcell);
}else{
array_push($null[$colIndex], $hcell);
}
}
//把excel的空字段组成数组
$key = array_keys($null);
//获取xls文件名,与数据表同名
$filePath = substr($filePath,strpos($filePath,'/')+1);
$sheetName = explode('.',$filePath);
$sheetName = $sheetName[0];
//把数据表的字段列出来
foreach($hdata as $k => $item){
if(!empty($item)){
if($k == 0){
$field .= '`'.$item.'`';
}else{
$field .= ','.'`'.$item.'`';
}
}
}
//不需要操作的表
$jump = array(
0 => 't_const',
1 => 't_update_code',
2 => 't_properties_message',
3 => 't_error_code',
4 => 't_properties_mail'
);
//跳过不需要操作的表
if(!in_array($sheetName, $jump)){
//把数据表的原先数据清空
$sSql = "DELETE FROM ".$sheetName."";
$res = mysql_query($sSql);
if($res){
//循环读取每个单元格的内容。注意行从3开始,列从A开始
for ($rowIndex = 3; $rowIndex <= $allRow; $rowIndex++) {
$data = array();
for($colIndex='A'; $colIndex!=$allColumn; $colIndex++){
//空字段对应的列的数据都不插入数据表
if(!in_array($colIndex, $key)){
$addr = $colIndex . $rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText){
//富文本转换字符串,不然就出现 PHPExcel_RichText Object
$cell = $cell->__toString();
}
array_push($data, $cell);
}
}
foreach($data as $k => $item){
//防止和item里的引号冲突
if($k == 0){
$value .= '"'.AddSlashes($item).'"';
}else{
$value .= ','.'"'.AddSlashes($item).'"';
}
}
//把excel表中的数据都插入数据库
$sql = "insert into ".$sheetName."(".$field.")";
$sql .= " values (".$value.")";
$sql = 'insert into '.$sheetName.'('.$field.')';
$sql .= ' values ('.$value.')';
mysql_query('SET NAMES UTF8');
$res = mysql_query($sql);
if($res){
//插入成功
//echo 1;
}else{
echo $sql.'<br /><br />';
//插入失败,显示报错信息
//错误打log
file_put_contents('errorlog/insert_error_'.$sheetName.'_'.date('Y-m-d').'.log','[START]------'.mysql_error().'------[END]'.date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND);
}
unset($value);
}
}else{
echo mysql_error();
//错误打log
file_put_contents('errorlog/delete_error_'.$sheetName.'_'.date('Y-m-d').'.log','[START]------'.mysql_error().'------[END]'.date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND);
}
}else{
file_put_contents('errorlog/pass_'.$sheetName.'_'.date('Y-m-d').'.log','[START]------'.$sheetName.'表跳过导入------[END]'.date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND);
}
unlink($filePath);
sleep(1);
}
if(error_log()){
file_put_contents('errorlog/php_error_'.date('Y-m-d').'.log','[START]------'.error_log().'------[END]'.date('Y-m-d H:i:s').PHP_EOL,FILE_APPEND);
}
echo '用时:'. ( microtime(TRUE) - FY_MTIME );
如果导入数据库的数据有出现“=某个单元格”(=A3),就先把.xls文件转为.csv文件,再按上面的代码导入数据库。
转格式的做法在下面的链接里:
xls文件转csv文件