PHP中,从数据库取出几条数据,如何先生成xls文件,然后保存到指定文件夹下面?
展开全部
$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');
}
'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');
}
追问
大哥你搞过了么?这方法能行?
追答
源代码都给你了,还不行 ?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询