php获取保存数据到文件 php获取保存数据到文件里
PHP中,从数据库取出几条数据,如何先生成xls文件,然后保存到指定文件夹下面?
$keynames=array(
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站建设、沿滩网络推广、重庆小程序开发、沿滩网络营销、沿滩企业策划、沿滩品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供沿滩建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
'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 [] = "htmlmeta http-equiv=content-type content=\"text/html; charset=UTF-8\"bodytable border='1'";
$xls [] = "trtdID/tdtd" . implode ( "/tdtd", array_values ( $keynames ) ) . '/td/tr';
foreach ( $data as $o ) {
$line = array (++ $index );
foreach ( $keynames as $k = $v ) {
$line [] = $o [$k];
}
$xls [] = 'trtd' . implode ( "/tdtd", $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');
}
php将数据写入文件
使用form表单post数据到PHP,然后用file_put_contents($fileName, $data)写入文件,$fileName是文件名,$data是要写入的数据
新建一个a.php文件,将下面的复制进去访问一下,填写后点击提交,会生成一个a.txt的文件,里面是你填写的内容
可能会有一个notice的报错,不必理会
?php
$data = $_POST['text'];
$fileName = 'a.txt';
file_put_contents($fileName, $data);
?
!doctype html
html
head
meta charset="utf-8"
titletest/title
/head
body
form action="./a.php" method="post"
textarea name="text" id="" cols="30" rows="10"/textarea
input type="submit" value="提交"
/form
/body
/html
求PHP从数据库中读取内容并存入文件和从文件读取数据插入数据库的代码
//第一种
?php
$conn = mysql_connect("主机名","用户名","密码");
mysql_select_db("数据库名",$conn);
$sql = "select * from user";
$result = mysql_query($sql);
$data="";
while($row = mysql_fetch_row($result)){
foreach($row as $v){
$data .=$v."|"; //加个树线隔开字段
}
nl2br($data); //读完一行回车
}
file_put_contents("abc.txt",$data);
//第二种
?php
$filename = "abc.txt";
$res = fopen($filename,"r");
while(!feof($res)){
$buffer = fgets($res);
$buffer = str_replace(' ', '|', $buffer);
$array = explode('|', $buffer);
foreach($array as $v){
if(strlen($v)0){
$arr[] = $v;
}
}
}
fclose($res);
//组装数组插入数据库就可以了
关于PHP文件操作: php保存数据到文本文件,怎么弄?
把你要保存的数据序列化一下,保存到文本中,如果需要修改,则从文本中取出,在反序列化化,在修改后,在序列化一下,保存到文本中。
文本的读写 参考fopen 等函数
php从文件中读取数据,保存到数组,从数组里选取前几列数据写入到文件里
?PHP
$file_name="1.txt";
$fp=fopen($file_name,'r');
$str=array();
$bian=0;
while(!feof($fp))
{
$buffer=fgets($fp);
$str[$bian]=$buffer;
$bian++;
}
fclose($fp);
var_dump($str);
$k=fopen("2.txt","a+");
for($z=0;$z2;$z++){
fwrite($k,$str[$z]);
}
fclose($k);
?
文章标题:php获取保存数据到文件 php获取保存数据到文件里
标题URL:http://myzitong.com/article/hepcgc.html