从官网下载phpexcel文件包。
下载地址:http://phpexcel.codeplex.com/releases/view/119187
从数据库导出数据到excel文件代码:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$file = $_FILES['file']['tmp_name'];
if(empty($file)){
echo "<script>alert('请选择文件');</script>";
}
require_once '../PHPExcel/PHPExcel.php';
require_once '../PHPExcel/PHPExcel/IOFactory.php';
require_once '../PHPExcel/PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('excel2007');
$excelpath = $file;
$objPHPExcel = $objReader->load($excelpath);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn = $sheet->getHighestColumn(); //取得总列数
$str = '';
for($j=2;$j<=$highestRow;$j++) //从第二行开始读取数据
{
$str.='(';
$strValue = '';
for($k='A';$k<=$highestColumn;$k++) //从A列读取数据
{
//读取单元格
$strValue.="'".$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()."'".',';
}
$strValue=substr($strValue,0,-1);
$str.=$strValue.'),'; }
$str=substr($str,0,-1);
$sql = "INSERT INTO user (uid,password,mail,phone,name,add_time,ipaddress) VALUES $str";
$res = $nsodb->query($sql);
if($res){
echo "<script>alert('导入成功!');</script>";
}
}
?>
(http://www.thinksite.cn/index.php?m=content&c=index&a=lists&catid=16)