php生成csv
Ⅰ php 生成csv文件并提示保存
<?php
//文件名
$filename="test.csv";
//数据(具体的根据需要做处理,如果是从数据库查询,原理与此类似,只需设置好写入格式和数据即可。
$data="测试csv";
//设置header
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');header('Pragma:public');
echo$data;
?>
Ⅱ php 生成 csv文件
$filename="export_".date('Ymd').".csv";
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="'.$filename.'"');
header('Cache-Control:max-age=0');
//写入你的查询代码
$fp=fopen('php://output','a');
$print_hea=array("姓名","年龄","性别");
fputcsv($fp,$print_hea);
//下面是你的查询结果代码,把结果循环在数组中后使用
fputcsv($fp,$print_hea_new);
Ⅲ php中如何导入导出CSV格式的文件
php导入CSV文件:
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z这样就可以去掉表头的那一行
for ($i = 0; $i < count($data); $i++) {
$zian = $data[$i];
}
}
2.php导出CSV文件
header( "Cache-Control: public" );
header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=txxx.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str = iconv("utf-8",'gbk',"信息id,标题,名称,电话,QQ,Email,内容,时间
");
$file_str="";
$mysqli= new mysqli('localhost','root','','test');
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$sql='select * from messages';
$mysqli->query("set names utf8 ;");
$result=$mysqli->query($sql);
if($result){
while ($row = mysqli_fetch_assoc($result)){
$file_str.= $row['id'].','.$row['title'].','.$row['name'].','."'{$row['telephone']}'".','.$row['qq'].','.$row['email'].','.str_ireplace(',',',',$row['content']).','.$row['retime']."
";
}
}else{
echo "nonono!!!";
}
$file_str= iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
?>
Ⅳ php怎么样把xls转换成csv
PHPExcel完全可以满足你的要求,给个简单处理例子,具体的你还是看看PHPExcel的文档:
$filename = "myexcel.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($filename);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save(str_replace('.xlsx', '.csv',$filename));
Ⅳ php导出excel或csv
<?php
ob_start();
header("<meta http-equiv=\"content-type\" content=\"text/html;charset=uft-8\">");
header("Content-Type: application/vnd.ms-excel");
header("Expires:0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=文件名称");
$row = $db -> ... ... # 这里可以进行数据库的操作,记住:ob_start();前不要有输出
?>
Ⅵ php 如何从数据里选择所需要信息然后生成csv文件自动保存在指定的路径(不提示保存,自动)
命令行模式下,或者web模式下保存的路径在服务器中,可以达到你的要求(有该路径的权限即可)
如果是在web模式下,不提示直接保存到访客的电脑中的某个位置,是不可以的。
web模式保存到客户端,只能通过下载,由用户指定,或保存到默认的下载目录。
以下是代码示例:
$dsn='mysql:dbname=testdb;host=127.0.0.1';
$user='dbuser';
$password='dbpass';
//连接数据库pdo
try{
$dbh=newPDO($dsn,$user,$password,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SETNAMES'UTF8'"));
}catch(PDOException$e){
echo'Connectionfailed:'.$e->getMessage();exit;
}
//读取数据,具体sql你根据情况修改
$stat=$dbh->prepare('SELECT*FROM`table`WHERE`id`>0LIMIT100;');
$stat->execute();
$result=$stat->fetchAll(PDO::FETCH_ASSOC);
//创建csv文件并打开文件指针
$filepath='file.csv';
$fp=fopen($filepath,'w');
//写入数据
foreach($resultas$i=>$row){
//写入标题行
if($i==0){
fputcsv($fp,array_keys($row));
}
fputcsv($fp,$row);
}
//关闭文件指针
fclose($fp);
//把文件输出到下载
$file=fopen($filepath,"r");//打开文件
$size=filesize($filepath);
Header("Content-type:application/octet-stream");
Header("Accept-Ranges:bytes");
Header("Accept-Length:".$size);
Header("Content-Disposition:attachment;filename=download.csv");
echofread($file,$size);
fclose($file);
相关知识点:
PHP: fputcsv
PHP:PDO
数据库查询的时候只返回列名,防止列出现重复,csv里会有重复的数据
Ⅶ 如何使用PHP导出csv和excel文件
(一)phpexcel文件导出:
步骤1,引入文件require APPPATH.'/libraries/PHPExcel.PHP';
步骤2, 实例化PHPEXCEL对象 $objPHPExcel=new PHPExcel();
步骤3, 设置表头 $column = array('A','B','C'); $line = array('词语','频次','词性');
//填充表头信息
for($i = 0;$i < count($tableheader_all);$i++) {
$objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
//上一行中"$letter[$i]1"表示第“1”行第“$i”列
}
步骤4, 填充数据
for ($i = 2;$i <= count($data) + 1;$i++) {//因为内容是从第二行开始的,所以i=2表示从第二行开始填充数据
$j = 0;
foreach ($tmp as $key=>$val ){
$objPHPExcel->getActiveSheet()->setCellValue("$letter[$j]$i",$data[$i-2][$val]); //数据是从第一条开始但是i的初值是2所以想从第一条开始应该是$data[$i-2]开始
$j++;
}
}
步骤5,写进excel中并输出
$write = new PHPExcel_Writer_Excel5($objPHPExcel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="数据导出_词云图.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
(一)CSV文件导出:
csv文件导出最令人头疼的问题就是编码问题,现在分享一下我的经验
首先看服务器是linux的还是windows的 如果是windows的服务器那么编码问题只能借助于mb_convert_encoding()或者是iconv两个函数相互转换中文编码
如果服务器是linux的 那么很简单 utf-8: setlocale(LC_ALL, ‘en_US.UTF-8′); 简体中文:setlocale(LC_ALL, ‘zh_CN');
下面来说csv导出步骤:
$result = mysql_query("select * from student order by id asc");
$str = "姓名,性别,年龄\n";
$str = iconv('utf-8','gb2312',$str);
while($row=mysql_fetch_array($result)){
$name = iconv('utf-8','gb2312',$row['name']); //中文转码
$sex = iconv('utf-8','gb2312',$row['sex']);
$str .= $name.",".$sex.",".$row['age']."\n"; //用引文逗号分开
}
$filename = date('Ymd').'.csv'; //设置文件名
export_csv($filename,$str); //导出
要将数据导出到本地即下载,需要修改header信息,代码如下:
function export_csv($filename,$data) {
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}
Ⅷ php中如何导入导出CSV格式的文件
其实你可以用
phpmyadmin
实现导入导出,不过导入的时候字段要和数据表的字段一一对应,参考资料里面的直接用PHP读取excel的,可能也会对你有帮助!
Ⅸ PHP生成CSV文件问题 麻烦各位高手帮忙看看
csv很简单
数据以,分开
行用\n
存为.csv就行了
生成xls的话稍微复杂一点儿,下面是一个生成xls的类,你可以按照你的需要修改调用一下。
======================================
用PHP生成xls,csv格式文件的类
2008年09月23日 星期二 上午 09:56
fileOperation.php
****************************************************
class fileOperation {//基类
var $fileName='test';
var $extendName='csv';
var $mPath='./report/';
var $mFp;
function fileOperation() {
}
function openFile($mode='w'){
if(empty($this->fileName)){
$this->setTimeFileName();
}
if (empty($this->extendName)){
$this->setExtendName();
}
$fp=fopen($this->mPath.'/'.$this->fileName.'.'.$this->extendName,$mode);
if($fp){
$this->mFp=$fp;
}else{
return 0;
}
}
function closeFile(){
return fclose($this->mFp);
}
function setTimeFileName($type='Ymd'){
if(!empty($type)){
$this->fileName=$type;
}else{
$this->fileName=time();
}
}
function setExtendName($extend='txt'){
if(!empty($extend)){
$this->extendName=$extend;
}else{
$this->extendName='.csv';
}
}
function setPath($path='./'){
$this->mPath=$path;
}
}
xlsHelper.php
****************************************************
require_once 'fileOperation.php';
class xlsHelper extends fileOperation{//具体实现子类
var $mSpace = '';
var $mHead;
var $mBody='';
function addHeader($head=array()){
$this->mHead='<table width="500" border="1" align="center" cellpadding="5"><tr>';
if (is_array($head)){
foreach($head as $hd){
$this->mHead.='<th bgcolor="#A5A0DE">'.$hd.'</th>';
}
}
$this->mHead.='</tr>';
}
function addBodyData($body=array()){
if(is_array($body)){
for($i=0;$i<count($body);$i++){
$childBody=$body[$i];
$this->mBody.='<tr>';
$this->mSpace = '<td align="center">';
for($j=0;$j<count($childBody);$j++){
$this->mBody.=$this->mSpace.$childBody[$j].'</td>';
}
$this->mBody.="</tr>";
}
}
$this->mBody.='</table>';
}
function _construct(){
}
function writeCSVDate(){
return fwrite($this->mFp,$this->mHead.mb_convert_encoding($this->mBody,'sjis','sjis'));
}
function setSpace($type=','){
$this->mSpace=$type;
}
}
test.php
****************************************************
$xls=new xlsHelper();
$xls->fileName='xxx';//设置生成文件的文件名
$xls->extendName='xls';//文件扩展名
$xls->mPath='./';//文件保存路径
$headerarr=array('姓名','年龄','邮箱');//头部字段名
$xls->addHeader($headerarr);
$datasarr=array(//注意:此处的二维数组一定要是数字索引
array('yht',20,'[email protected]'),
array('ktv009',23,'[email protected]'),
);
$xls->addBodyData($datasarr);
$xls->openFile('w');
if($xls->writeCSVDate()) echo "<script language='javascript'>生成文件成功</script>";
else echo ""<script language='javascript'>无法生成文件</script>";
Ⅹ 如何使用PHP导出csv和excel文件
把Excel文件导入mysql:
打开excel文件,可用phpExcel开源的类
或者:
先把excel文件另存为csv格式,最好是utf8编码。
fgetcsv() — 从文件指针中读入一行并解析 CSV 字段,返回数组