PHP怎样将查询出来的数据导出成excel表格?
根据下列编码程序可以。
1./*** 批量导出数据* @param $arr 从数据库查询出来,即要导出的数据* $name excel表歌名*/2.function expExcel($arr,$name){ require_once 'PHPExcel.php';3. //实例化 $objPHPExcel = new PHPExcel(); /*右键属性所显示的信息*/4.$objPHPExcel->getProperties()->setCreator("zxf") // ->setLastModifiedBy("zxf") //最后一 ->setTitle('数据EXCEL导出') //标题->setSubject('数据EXCEL导出') //主题>setDescription('导出数据') //描>setKeywords("excel") //标记>setCategory("result file"); //类别5. //设置当前的表格 $objPHPExcel->setActiveSheetIndex(0);// 设置表格第一行显示内容$objPHPExcel->getActiveSheet() ->setCellValue('A1', '业主姓名') ->setCellValue('B1', '密码')->setCellValue('C1', '手机号码' ->setCellValue('D1', '地址')6.//设置第一行为红色字体 ->getStyle('A1:D1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);$key = 1; /*以下就是对处理Excel里的数据。
PHP如何将查询出来的数据导出成excel表格(最好做一个按钮)?
讲的复杂了啊!你先在一个完整版的PHPExcel之后解压,在“Examples”目录下会找到一大堆例子,根据你的要求这个“01simple-download-xlsx.php”文件就可以了!注:你先保持“01simple-download-xlsx.php”文件所在的目录位置不要变,测试好了,再改变名,移到别的地方,地方变了的话,文件里的 “require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';”的所在位置也要变!我们要改动代码很少,如下:// Add some data$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'Hello')->setCellValue('B2', 'world!')->setCellValue('C1', 'Hello')->setCellValue('D2', 'world!');// Miscellaneous glyphs, UTF-8$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A4', 'Miscellaneous glyphs')->setCellValue('A5', 'éàèùaê?????ü???ü?');、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、直接用的我的替换$objPHPExcel->setActiveSheetIndex(0)//这个就是现实导出的表第一行,有几列是根据你的那张表有几列!->setCellValue('A1', '单号')->setCellValue('B1', '标题')->setCellValue('C1', '内容')->setCellValue('D1', '序列')->setCellValue('E1', '数字');//下面实现的就是建立数据库连接,直接到表,你的连接数据库、表、字段应该与我的不一样,你可以参考$conn=@mysql_connect("localhost","root","root") or die("数据库服务器连接错误".mysql_error());//连接mysql数据库mysql_select_db("temp",$conn) or die("数据库访问错误".mysql_error());//数据库mysql_query("set character set gb2312");mysql_query("set names gb2312");$sqlgroups="select * from test ";//查询这一张表的条件$resultgroups=mysql_query($sqlgroups);$numrows=mysql_num_rows($resultgroups);if ($numrows>0){$count=1;while($data=mysql_fetch_array($resultgroups)){$count+=1;$l1="A"."$count";$l2="B"."$count";$l3="C"."$count";$l4="D"."$count";$l5="E"."$count";$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($l1, $data['id'])//这就是你要导出表的字段、与对应的名称->setCellValue($l2, $data['title'])->setCellValue($l3, $data['content'])->setCellValue($l4, $data['sn'])->setCellValue($l5, $data['num']);}}
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文件
$letter[$i]1"D',':attachment;filename="//header("Cache-Control:must-revalidate, post-check=0, pre-check=0");header(",有点基础的都能明白是怎么回事,下面进入代码;20';header("Pragma;);/填充表头信息for($i = 0;$i++) {$excel->Content-Transfer-Encoding:PHPExcel。
下面是一个简单的demo;,','/创建Excel输入对象$write = new PHPExcel_Writer_Excel5($excel):phpexcel,结果如图所示,说明PHP导出的Excel是正确的;性别';//F',分享给大家;);3': public");header("1'G',': 0"PHP导出Excel,PHP输入Excel4./,'F'PHPExcel.php';B'男');header(','101'),array('C'Content-Type:application/force-download");header("./phpexcel/创建对象$excel = new PHPExcel();Content-Type:application/vnd.ms-execl");header(",点击第一个结果;Excel表格式,这里简略写了8列$letter = array(',array('4';,'A','年龄',客户希望把一些数据能导出成为Excel表格,'20'102');表头数组$tableheader = array('100',如图所示;PHP导出Excel;,"$tableheader[$i]");}PHP导出Excel,PHP输入Excel5./,'。
如果出现错误,检查一下你的表格数组和数据数组吧,'103'));$i setCellValue("Expires;填充表格信息for ($i = 2;小王';,','小李'//);PHP导出Excel,PHP输入ExcelPHP导出Excel;/output'.因为这里给大家做演示,所以建了一个测试文件;testdata.xls"foreach ($data[$i - 2] as $key=>$value) {$excel->getActiveSheet()->');header(",让你选择文件保存路径和文件名称;Content-Type:application/download");;//昨天项目里有个新需求,刚开始用PHP原生输入Excel表格,发现效果不是很理想,于是找到一个比较著名的库;,'$write->save('php:/,希望可以帮到有同样需求的朋友。
1.百度;/引入PHPExcel库文件(路径根据自己情况)include ','姓名'$i < count($tableheader);学号','女','20'Classes/,'
php导出excel表格
建议你可以使用php里面的csv函数,直接导出为csv格式的文件,这样导出是可以即时导出的,不需考虑溢出问题。
如果必须为excel,可以使用phpexcel插件使用,网上有很多说明。
function excelexport(){$where = 查询条件;set_time_limit(0);$name = '数据.csv';dynamicHeader($name); //设置消息头$handle = fopen('php://output', 'ab');$fieldname = array('**','**','**'); //表格标题foreach($fieldname as &$hd){$hd = mb_convert_encoding($hd, 'GBK', 'UTF-8');}fputcsv($handle, $fieldname);$count = D('Kkk')->getcount($where); //获取数据量$limit = 100;$times = ceil($count / $limit);for($i = 0; $i $dataresult = array();$offset = $i * $limit;$dataresult = D('Kkk')->getpageUsers($where,$offset,$limit); //分配获取数据if ($i == 10) {ob_flush();flush();}foreach($dataresult as $row) {//兼容用excel打开时不乱码foreach($row as &$val){//$val = mb_convert_encoding($val, 'GBK', 'UTF-8');$val = iconv('UTF-8', 'GBK//IGNORE', $val);}fputcsv($handle, $row);}}fclose($handle);}有问题可以私聊
谁有关于PHP导出Excel的实例?可以操作Excel样式,以及?
? session_stat(); if ($_SESSION['Company']==''){ exit; } $path = diname(__FILE__); equie_once($path.'....ModuleFactoy.php'); $Factoy = new Factoy(); $BLL_Goods = $Factoy-FactoyGoods(); $Goods=new Goods(); $Goods-BaCode=$_POST['GoodsID']; $Goods-Poduct-BaCode=$_POST['PoductID']; if($_SESSION['StationTitle']==''){ $Goods-Station-ID = $_SESSION['Station']; } else { $Goods-Station-ID = $_POST['StationID']; } $Goods-State-ID=$_POST['StateID']; $Page=new Page(); if ($_GET['Page']=='') { $Page-Page=0; } else { $Page-Page=$_GET['Page']; } $Page-Page=15; $GoodsList=$BLL_Goods- PageScanGoods( $Goods,$Page); ?!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TansitionalEN" "http:www.w3.ogTRxhtml1DTDxhtml1-tansitional.dtd"html xmlns="http:www.w3.og1999xhtml"headmeta http-equiv="Content-Type" content="texthtml; chaset=g2312" titletitlelink hef="goods.css" el="stylesheet" type="textcss" headodydiv class="W500"div class="Title"物品列表divdiv class="ListTitle"div class="Items1" style="width:100px"物品条码divdiv class="Items1" style="width:200px"所属产品divdiv class="Items1" style="width:100px"入库时间divdiv class="Items1" style="width:50px"租金天divdiv class="Items1" style="width:100px"物品价格divdiv class="Items1" style="width:70px"物品状态divdiv class="Items1" style="width:80px"所在地点divdiv? if (count($GoodsList)!=0) { foeach ($GoodsList as $Goods){ $Station = new Station(); $Station-ID = $Goods-Station-ID; $Station = $BLL_Goods-GetStation($Station); $State=new State(); $State-ID=$Goods-State-ID; $State=$BLL_Goods-GetState($State); $Poduct=new Poduct(); $Poduct-BaCode= $Goods-Poduct-BaCode; $Poduct=$BLL_Goods-GetPoduct($Poduct); ?div class="List" onmouseove="this.className='ListB'" onmouseout="this.className='List'"div class="Items1" style="width:100px"?echo $Goods-BaCode?divdiv class="Items1" style="width:200px; colo:#FF0000"?echo $Goods-Poduct-BaCode.'→'.$Poduct-Title?divdiv class="Items1" style="width:100px"?echo date('Y年m月d日',$Goods-EntTime)?divdiv class="Items1" style="width:50px"?echo $Poduct-Hie ?元divdiv class="Items1" style="width:100px"?echo $Poduct-Pice ?元divdiv class="Items1" style="width:70px"?echo $State-Title ?divdiv class="Items1" style="width:80px"?echo $Station-Title ?divdiv? } } ?div class="Page"?echo $Page-Page+1?页?echo $Page-MaxPage+1?页 ? if ($Page-Page==0) { echo '首页'; } else { echo ' a hef="CheckGoodsok.php?Page=0"首页a'; } ??if ($Page-Page0) {?a hef="CheckGoodsok.php?Page=?echo $Page-Page-1?"上一页a? } else { echo '上一页'; } ??if ($Page-Page$Page-MaxPage) {?a hef="CheckGoodsok.php?Page=?echo $Page-Page+1?"下一页a? } else { echo '下一页'; } ?? if ($Page-Page==$Page-MaxPage) { echo '尾页'; } else { echo 'a hef="CheckGoodsok.php?Page='.$Page-MaxPage.'"尾页a'; } ? 共?echo $Page-Sum?条记录 divdiva hef="CheckGoodsToExcel.php?GoodsID=?echo $_POST['GoodsID']?&PoductID=?echo $_POST['PoductID']?&Station=?echo $_POST['Station']?&StateID=?echo $_POST['StateID']?"导出Excel表格aodyhtml
用php怎么从数据里调取数据,导出形式是excel
很简单的,1首先你连接数据库,2然后查询数据库里面所有的数据,3查询出来的数据放在一个里面例如:$_sql="select * from 表名";mysql_query("set names utf8");//编码设置为utf8$query=mysql_query($_sql);echo "";while($row=mysql_fetch_array($query)){ //从数据库查询出来的字段$数据1=$row["字段1"];$数据2=$row["字段2"];$数据3=$row["字段3"];//将数据放到html的表格中echo "$数据1$数据2$数据3";}echo "";?>这个是取出数据生成一个html表格,希望你能采纳我,因为我马上要升级了,谢谢!
转载请注明出处51数据库 » php 导出word表格数据