【原】phpExcel之导入数据(php+mysql)
发表于2014-10-31 16:42 | 次阅读 | 0条评论 | 作者:siru90
EXCEL:
HTML:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" class="file" name="upExcel" id="upExcel"/>
<b style="display:block;" class="btn green_button add_green_button">上传</b>
</form>
PHP:
if (isset($_FILES["upExcel"]))
{
if ($_FILES["upExcel"]["size"] > 500*1024*1024)
{
CommUtils::resOut("-1", "上传文件过大,最大值为500M。");
return;
}
else
$upExcel = $_FILES["upExcel"];
}
else
{
CommUtils::resOut("-1", "请选择一个Excel文件");
return;
}
$ext = pathinfo($_FILES["upExcel"]["name"]);
$ext = $ext["extension"];
if ("xls" == $ext || "xlsx" == $ext)
{
if (is_uploaded_file($_FILES["upExcel"]["tmp_name"]))
{
$tmp_name = $_FILES["upExcel"]["tmp_name"];
$upfile = "php/upload/".$tmp_name;
move_uploaded_file($_FILES["upExcel"]["tmp_name"], $upfile)
//引入phpexcel
require_once "php/comm/PHPExcel.php";
$excelReader = new PHPExcel_Reader_Excel2007();
if (!$excelReader->canRead($upfile))
{
$excelReader = new PHPExcel_Reader_Excel5();
if (!$excelReader->canRead($upfile))
{
$this->alert("无法识别的Excel文件");
return;
}
}
/*读取Excel*/
$excel = $excelReader->load($upfile);
$sheet = $excel->getSheet(0);
$colNum = $sheet->getHighestColumn();
$rowNum = $sheet->getHighestRow();
$dataArr = array();
for ($rowIndex = 1; $rowIndex <= $rowNum; $rowIndex++)
{
/* //单列读取
$cellA = trim($sheet->getCell('A'.$rowIndex)->getValue());
$cellB = trim($sheet->getCell('B'.$rowIndex)->getValue());
$cellC = trim($sheet->getCell('C'.$rowIndex)->getValue());
*/
for ($colIndex = 'A'; $colIndex <= $colNum; $colIndex++) //循环读取
{
$addr = $colIndex.$rowIndex;
$cell = trim($sheet->getCell($addr)->getValue());
$dataArr[] = $cell;
}
}
var_dump($
dataArr);
}
}