本文目录一览:
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++) {
$ziduan = $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,内容,时间\n");
$file_str="";
$mysqli= new mysqli('localhost','root','','test');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", 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']."\n";
}
}else{
echo "nonono!!!";
}
$file_str= iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
?
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和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文件
?php
/**
* Created by PhpStorm.
* User: chenyanphp@qq.com
* Date: 2016/12/21 0021
* Time: 下午 12:08
*/
// 头部标题
$csv_header = ['名称','性别','年龄'];
// 内容
$csv_body = [
['张三','男','13'],
['李四','女','13'],
['王五','男','13'],
['赵六','未知','13']
];
/**
* 开始生成
* 1. 首先将数组拆分成以逗号(注意需要英文)分割的字符串
* 2. 然后加上每行的换行符号,这里建议直接使用PHP的预定义
* 常量PHP_EOL
* 3. 最后写入文件
*/
// 打开文件资源,不存在则创建
$fp = fopen('test.csv','a');
// 处理头部标题
$header = implode(',', $csv_header) . PHP_EOL;
// 处理内容
$content = '';
foreach ($csv_body as $k = $v) {
$content .= implode(',', $v) . PHP_EOL;
}
// 拼接
$csv = $header.$content;
// 写入并关闭资源
fwrite($fp, $csv);
fclose($fp);