PHP中,从数据库取出几条数据,如何先生成xls文件,然后保存到指定文件夹下面?

 我来答
中药精油提取实验
2014-03-24 · TA获得超过111个赞
知道小有建树答主
回答量:271
采纳率:50%
帮助的人:86.8万
展开全部
$keynames=array(
'0'=>'订单号',
'1'=>'项目名称',
'2'=>'单价',
'3'=>'数量',
'4'=>'运费',
'5'=>'金额',
'6'=>'支付状态',
'7'=>'支付时间',
'8'=>'选项',
'9'=>'备注',
'10'=>'收件人',
'11'=>'手机',
'12'=>'送货地址',
'13'=>'物流',
'14'=>'物流单号'
);

down_excel($eorders, $keynames,$array_key, $name);

//生成简单excel。table生成。生成excel非标准。phpexcel生成的标准,可读写excel
function down_xls($data, $keynames, $name = 'dataxls') {
$xls [] = "<html><meta http-equiv=content-type content=\"text/html; charset=UTF-8\"><body><table border='1'>";
$xls [] = "<tr><td>ID</td><td>" . implode ( "</td><td>", array_values ( $keynames ) ) . '</td></tr>';
foreach ( $data as $o ) {
$line = array (++ $index );
foreach ( $keynames as $k => $v ) {
$line [] = $o [$k];
}
$xls [] = '<tr><td>' . implode ( "</td><td>", $line ) . '</td></tr>';
}
$xls [] = '</table></body></html>';
$xls = join ( "\r\n", $xls );
header ( 'Content-Disposition: attachment; filename="' . $name . '.xls"' );
die ( mb_convert_encoding ( $xls, 'UTF-8', 'UTF-8' ) );
}
//add by wqc excel
function down_excel($data, $keynames,$array_key, $name = 'dataxls') {

require_once(dirname(dirname(dirname(__FILE__))) . '/PHPExcel/PHPExcel.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/PHPExcel/PHPExcel/IOFactory.php');
//实例化phpexcel
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(16);//设置单元格宽度
$objPHPExcel->getActiveSheet()->setTitle('kutuan');//设置当前工作表的名称
foreach($keynames as $k=>$r){
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($k, 1)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($k, 1, $r);
}
foreach($data as $key=>$value)
{
//$objPHPExcel->getSheet(1)->getProtection()->setSheet(true);
//$objPHPExcel->getSheet(1)->protectCells(0, ($key+2),$value['id']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, ($key+2),$value['id']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, ($key+2),$value['team_id']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, ($key+2),$value['price']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, ($key+2),$value['quantity']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4,($key+2),$value['fare']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, ($key+2),$value['origin']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, ($key+2),$value['state']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, ($key+2),$value['pay_time']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, ($key+2),$value['condbuy']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, ($key+2),$value['remark']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, ($key+2),$value['realname']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, ($key+2),$value['mobile']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, ($key+2),$value['address']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, ($key+2),$value['express']);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, ($key+2),$value['express_no']);
}

$objPHPExcel->setActiveSheetIndex(0);//设置打开excel时显示哪个工作表$name.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".$name);
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
return $objWriter->save('php://output');
}
追问
大哥你搞过了么?这方法能行?
追答
源代码都给你了,还不行 ?
轻风来客
2014-03-24 · TA获得超过217个赞
知道小有建树答主
回答量:183
采纳率:0%
帮助的人:185万
展开全部
你去下载个phpexcel 操作包下来就好了,里面写好了很多方法 ,如果网上找不到,。贴个邮箱给我我发给你
追问

那个方法我看过,但是就是不知道怎么做?

追答
里面不是都有方法吗?你去看看文件里的具体方法,应该有样例的吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式