php怎么读取excel 文件数据并输出
PHPExcel
PHPExcel 是用来操作Office Excel 文一个PHP类库,它基于微软的OpenXML和PHP语言。可以使用它来、写入不同格式的表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。
PHP读取示例代码
//获取上传的excel临时文件
$path = $_FILES["file"]["tmp_name"];
//将临时文件移动当前目录,可自定义存储位置
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
//将获取在服务器中的Excel文件,此处为上传文件名
$path = $_FILES["file"]["name"];
//调用readExcel函数返回一个
二维数组
$exceArray = readExcel($path);
//创建一个读取
excel函数
function readExcel($path){
//引入PHPExcel类库
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
$type = 'Excel5';//设置为Excel5代表支持2003或以下版本,
Excel2007代表2007版
$xlsReader = \PHPExcel_IOFactory::createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$Sheets = $xlsReader->load($path);
//开始读取上传到服务器中的Excel文件,返回一个
二维数组
$dataArray = $Sheets->getSheet(0)->
toArray();
return $dataArray;
}
PHP怎样将查询出来的数据导出成excel表格?
根据下列编码程序可以。
phpexcel 读取excel里的数据并在页面显示出来
public function excel(){
$list = M('map') -> select();//var_dump($list);die;
foreach($list as $key => $val){
$uid[$key] = $val['uid'];
}
$uid = arr_to_str(array_unique($uid));
$user_info = json_decode(getUserInfoList($uid));
foreach($user_info as $key => $val){
if(($username = $val -> user_name) || ($username = $val -> true_name) || ($username = $val -> email)){
$username = $username;
}
$user_list[$val->id] = $username;
}
$genre = array('','自然灾害','环境问题','其它','社会问题');
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=益图-".date("Ymd",time()).".xls");
echo iconv('utf-8', 'gb2312', '地图标题'); echo "\t";
echo iconv('utf-8', 'gb2312', '发布人'); echo "\t";
echo iconv('utf-8', 'gb2312', '介绍'); echo "\t";
echo iconv('utf-8', 'gb2312', '分类'); echo "\t";
echo iconv('utf-8', 'gb2312', '发布时间'); echo "\t";
echo "\n";
foreach($list as $key => $val){
echo iconv('utf-8', 'gb2312',$val['title']);echo "\t";
echo iconv('utf-8', 'gb2312',$user_list[$val['uid']]);echo "\t";
echo iconv('utf-8', 'gb2312',t($val['content']));echo "\t";
echo iconv('utf-8', 'gb2312',$genre[$val['genre_id']]);echo "\t";
echo iconv('utf-8', 'gb2312',date("Y-m-d",$val['create_time']));echo "\t";
echo "\n";
}
}