原生php导出csv文件
require('./db/dbManager.php');
require('./session.php');
require('./department_utils.php');
class ExportCSV{
function __construct(){
$this->db=new dbManager();
$this->out = fopen('php://output', 'w');
}
function output(&$row){
for($i=0;$i<count($row);$i++){
$row[$i]=iconv('utf-8','gb18030',$row[$i]);
}
fputcsv($this->out,$row);
}
function user($opts){
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
$fileName=$name.'-用户列表';
}else{
$department=0;
$fileName='所有部门-用户列表';
}
if(isset($opts['download'])){
//header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
$this->set_filename($fileName.'.csv');
}
$h=['部门','用户名','姓名','手机','工号','身份'];
$this->output($h);
$sql='SELECT COUNT(*) FROM users';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' WHERE DepartmentId IN '.$deps;
}
$total=$this->db->querySingle($sql);
$sql='SELECT d.DepartmentName,u.Username,u.Name,u.Phone,u.empno,r.name FROM users u';
$sql.=' LEFT JOIN department d ON d.ID=u.DepartmentId';
$sql.=' LEFT JOIN role r ON r.id=u.role';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' WHERE DepartmentId IN '.$deps;
}
$sql.=' LIMIT ?,10';
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++)
$this->output($arr[$j]);
}
}
function time_range($b,$e){
return date('Ymd',$b).'-'.date('Ymd',$e);
}
function exam($opts){
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
$begin_str=date('Y-m-d H:i:s',$begin);
$end_str=date('Y-m-d H:i:s',$end);
$fileName=$name.'-考试-'.$this->time_range($begin,$end);
if(isset($opts['download'])){
//header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
$this->set_filename($fileName.'.csv');
}
$h=['部门','考试名','时间','时长(分钟)','总分','及格线(百分制)'];
$this->output($h);
$sql='SELECT COUNT(*) FROM exam e';
$sql.=' WHERE e.ExamTime BETWEEN ? AND ?';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND DepartmentId IN '.$deps;
}
$total=$this->db->querySingle($sql,[$begin_str,$end_str]);
$sql='SELECT d.DepartmentName,e.ExamName,e.ExamTime,e.ExamDuration,e.Score,e.PassScore FROM exam e';
$sql.=' LEFT JOIN department d ON d.ID=e.DepartmentId';
$sql.=' WHERE e.ExamTime BETWEEN ? AND ?';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND DepartmentId IN '.$deps;
}
$sql.=' LIMIT ?,10';
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$begin_str,$end_str,$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++){
if(!$arr[$j][0])
$arr[$j][0]='所有部门';
$this->output($arr[$j]);
}
}
}
function grade($opts){
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
$begin_str=date('Y-m-d H:i:s',$begin);
$end_str=date('Y-m-d H:i:s',$end);
$fileName=$name.'-考试成绩-'.$this->time_range($begin,$end);
if(isset($opts['download'])){
//header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
$this->set_filename($fileName.'.csv');
}
$h=['部门','名字','考试名','时间','总分','分数'];
$this->output($h);
$sql='SELECT COUNT(*) FROM grade g';
$sql.=' LEFT JOIN exam e ON e.ID=g.ExamId';
$sql.=' LEFT JOIN users u ON u.ID=g.UserId';
$sql.=' WHERE e.ExamTime BETWEEN ? AND ?';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$total=$this->db->querySingle($sql,[$begin_str,$end_str]);
$sql='SELECT d.DepartmentName,u.Name,e.ExamName,e.ExamTime,e.Score,g.Score FROM grade g';
$sql.=' LEFT JOIN exam e ON e.ID=g.ExamId';
$sql.=' LEFT JOIN users u ON u.ID=g.UserId';
$sql.=' LEFT JOIN department d ON d.ID=u.DepartmentId';
$sql.=' WHERE e.ExamTime BETWEEN ? AND ?';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$sql.=' LIMIT ?,10';
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$begin_str,$end_str,$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++){
if(!$arr[$j][0])
$arr[$j][0]='所有部门';
$this->output($arr[$j]);
}
}
}
function exercise($opts){
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
$fileName=$name.'-练习数据-'.$this->time_range($begin,$end);
if(isset($opts['download'])){
//header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
$this->set_filename($fileName.'.csv');
}
if(!$this->db->exec('ATTACH DATABASE "exercise.db.php" AS "EX"'))
return;
$sql='SELECT COUNT(*) FROM EX.exercise e';
$sql.=' LEFT JOIN users u ON u.ID=e.uid';
$sql.=" WHERE e.time BETWEEN $begin AND $end";
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$total=$this->db->querySingle($sql);
$h=['部门','身份','名字','时间','结果'];
$this->output($h);
$sql='SELECT d.DepartmentName,r.name,u.Name,datetime(e.Time,"unixepoch","localtime"),e.result FROM EX.exercise e';
$sql.=' LEFT JOIN users u ON u.ID=e.uid';
$sql.=' LEFT JOIN department d ON d.ID=u.DepartmentId';
$sql.=' LEFT JOIN role r ON r.ID=u.role';
$sql.=" WHERE e.time BETWEEN $begin AND $end";
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$sql.=' ORDER BY e.time ASC LIMIT ?,10';
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++){
if(!$arr[$j][4])
$arr[$j][4]='错误';
else
$arr[$j][4]='正确';
$this->output($arr[$j]);
}
}
}
function set_filename($fileName){
$userAgent=$_SERVER['HTTP_USER_AGENT'];
if($userAgent && strstr($userAgent,"Firefox")){
header('Content-Disposition: attachment;filename*="'."utf8''".urlencode($fileName).'"');
}else{
header('Content-Disposition: attachment;filename="'.urlencode($fileName).'"');
}
}
function study($opts){
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
$fileName=$name.'-学习时间-'.$this->time_range($begin,$end);
if(isset($opts['download']))
//header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
$this->set_filename($fileName.'.csv');
$sql='SELECT COUNT(*) FROM studyRecord s';
$sql.=' LEFT JOIN users u ON u.ID=s.UserId';
$sql.=" WHERE s.Begin BETWEEN $begin AND $end";
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$total=$this->db->querySingle($sql);
$h=['部门','身份','名字','时间','时长(秒)','类型'];
$this->output($h);
$sql='SELECT d.DepartmentName,r.name,u.Name,datetime(s.Begin,"unixepoch","localtime"),s.Duration,s.Type FROM studyRecord s';
$sql.=' LEFT JOIN users u ON u.ID=s.UserId';
$sql.=' LEFT JOIN department d ON d.ID=u.DepartmentId';
$sql.=' LEFT JOIN role r ON r.ID=u.role';
$sql.=" WHERE s.Begin BETWEEN $begin AND $end";
$sql.=" LIMIT ?,10";
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++){
if($arr[$j][5]==0)
$arr[$j][5]='学习';
else
$arr[$j][5]='练习';
$this->output($arr[$j]);
}
}
}
// 每个学员学习时间要按学习类型分组加起来
function study2($opts){
if(!$this->db->exec('ATTACH DATABASE "exercise.db.php" AS "EX"'))
return;
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
$fileName=$name.'-学习时间-'.$this->time_range($begin,$end);
if(isset($opts['download']))
$this->set_filename($fileName.'.csv');
$total=9999;
$h=['部门','身份','名字','学习时间(秒)','练习时长(秒)','练习数量','练习正确数量','练习正确率'];
$this->output($h);
$sql='SELECT d.DepartmentName,r.name,u.Name,u.ID FROM users u';
$sql.=' LEFT JOIN department d ON d.ID=u.DepartmentId';
$sql.=' LEFT JOIN role r ON r.ID=u.role';
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' WHERE u.DepartmentId IN '.$deps;
}
$sql.=' LIMIT ?,10';
for($i=0;$i<$total;$i+=10){
$arr=$this->db->queryAll($sql,[$i]);
if(!$arr)
break;
for($j=0;$j<count($arr);$j++){
$uid=array_pop($arr[$j]);
$temp=$this->db->query("SELECT sum(-s.Duration*(s.Type-1)),sum(s.Duration*s.Type) FROM studyRecord s WHERE s.UserId=? AND s.Begin BETWEEN $begin AND $end",$uid);
if($temp){
$arr[$j][]=(int)$temp[0];
$arr[$j][]=(int)$temp[1];
}else{
$arr[$j][]=0;
$arr[$j][]=0;
}
$temp=$this->db->query('SELECT COUNT(*),SUM(e.result) FROM EX.exercise e WHERE e.uid=?',$uid);
if($temp){
$arr[$j][]=(int)$temp[0];
$arr[$j][]=(int)$temp[1];
if($temp[0]>0){
$arr[$j][]=sprintf("%.2f",$temp[1]/$temp[0]);
}else{
$arr[$j][]=0;
}
}
$this->output($arr[$j]);
}
}
}
function get_libnames(){
$sql='SELECT Name FROM question_lib group by Name';
return $this->db->querySingleAll($sql);
}
function lib_error($opts){
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$fileName=$name.'-高频错题-'.$this->time_range($begin,$end);
if(isset($opts['download']))
$this->set_filename($fileName.'.csv');
$libs=$this->get_libnames();
if(!$this->db->exec('ATTACH DATABASE "exercise.db.php" AS "EX"'))
return;
$h=['题库','题型','题目','选项','答案','答题数','答题正确数量','正确率'];
$this->output($h);
$sql='CREATE TEMP VIEW stat AS SELECT qid,count(*) as count,sum(result) as result,sum(result)*1.0/count(*)*1.0 as accuracy FROM EX.exercise';
$sql.=' LEFT JOIN users u ON u.ID=uid';
$sql.=" WHERE time BETWEEN $begin AND $end";
if($department>0){
$deps=department_with_child_sql($this->db,$department);
$sql.=' AND u.DepartmentId IN '.$deps;
}
$sql.=' GROUP BY qid';
$ret=$this->db->exec($sql);
if(!$ret){
return;
}
$sql='SELECT q.LibName,q.Type,q.Question,q.Option,q.Answer,s.count,s.result,s.accuracy FROM stat s';
$sql.=' LEFT JOIN EX.question q ON q.ID=s.qid';
$sql.=' WHERE q.LibName=?';
$sql.=' AND s.count>=3';
$sql.=' ORDER BY s.accuracy ASC';
$sql.=' LIMIT 0,10';
for($i=0;$i<count($libs);$i++){
$temp=$this->db->queryAll($sql,$libs[$i]);
if(!$temp)
continue;
for($j=0;$j<count($temp);$j++){
$this->output($temp[$j]);
}
}
}
//过关记录
function manmul_lib($opts){
$begin=(int)$opts['begin'];
$end=(int)$opts['end'];
if(isset($opts['department']) && $opts['department']>0){
$department=(int)$opts['department'];
$name=$this->db->querySingle('SELECT DepartmentName FROM department WHERE ID=?',[$department]);
if(!$name)
return;
}else{
$department=0;
$name='所有部门';
}
$fileName=$name.'-过关记录-'.$this->time_range($begin,$end);
if(isset($opts['download']))
$this->set_filename($fileName.'.csv');
$libs=$this->get_libnames();
if(!$this->db->exec('ATTACH DATABASE "exercise.db.php" AS "EX"'))
return;
$h=['姓名','部门','过关记录','时间','难度系数'];
$this->output($h);
}
}
header('Content-Type: text/plain; charset=GB18030');
//header('Content-Type: application/octet-stream');
if(!isset($_GET['op']))
return;
$op=$_GET['op'];
if(!isset($_SESSION['AdminId']))
return;
$ec=new ExportCSV();
if(!department_can_admin($ec->db,$_GET['department'],$_SESSION['AdminId']))
return;
header('Cache-Control: max-age=0');
if($op=='user')
$ec->user($_GET);
else if($op=='exam')
$ec->exam($_GET);
else if($op=='grade')
$ec->grade($_GET);
else if($op=='exercise')
$ec->exercise($_GET);
else if($op=='study')
$ec->study2($_GET);
else if($op=='libe')
$ec->lib_error($_GET);
else if($op=='manmul')
$ec->manmul_lib($_GET);
?>
一个模块的代码。附上结果图片
下图为study方法中导出的练习数据,头部引入除过session都是封装了crud的db方法类,导出主要参考下面两个方法。
方法:
function set_filename($fileName){
$userAgent=$_SERVER['HTTP_USER_AGENT'];
if($userAgent && strstr($userAgent,"Firefox")){
header('Content-Disposition: attachment;filename*="'."utf8''".urlencode($fileName).'"');
}else{
header('Content-Disposition: attachment;filename="'.urlencode($fileName).'"');
}
}
方法:
function output(&$row){
for($i=0;$i<count($row);$i++){
$row[$i]=iconv('utf-8','gb18030',$row[$i]);
}
fputcsv($this->out,$row);
}
原生php导出csv文件的更多相关文章
- 导出csv文件示例
导出csv文件示例 csv文件默认以英文逗号,做为列分隔符换行符\n作为行分隔符,写入到一个.csv文件即可.含有英文逗号,和换行符会发生数据输出会出现混乱,下面列出一些处理方法.特殊字符处理1.含有 ...
- mysql SQLyog导入导出csv文件
1.选择数据库表 --> 右击属性 --> 备份/导出 --> 导出表数据作为 --> 选择cvs --> 选择下面的“更改” --> 字段 --> 变量长度 ...
- PHP 读取/导出 CSV文件
工作中经常会有遇到导入/导出的需求,下面是常用的方法.读取CSV文件,可以分页读取,设置读取行数,起始行数即可.导出CSV文件,用两种方法进行实现. /** * 读取CSV文件 * @param st ...
- Web 端 js 导出csv文件(使用a标签)
前言 导出文件,使用最多的方式还是服务器端来处理.比如jsp 中使用response 的方式. 但是,有时候可能就想使用web 前端是否也可以把页面上的内容导出来呢? 比如说,导出页面的一个表格. 这 ...
- PHP导出CSV文件出现乱码的解决方法
在做项目时碰到使用外语的情况下,我们就会使用UTF-8编码.但是,在用PHP导出CSV文件时,如果写入的数据是使用UTF-8编码的日语.韩语之类的外文,就会出现乱码. 要解决PHP生成CSV文件的乱码 ...
- [转]PL/SQL Developer 导入导出csv文件
PL/SQL Developer 可以导入或者导出CSV文件. 导入CSV文件步骤: 1.选择tools->text importer.... 2.选择第二个Data to oracle选项卡, ...
- 导出csv文件数字会自动变科学计数法的解决方法
其实这个问题跟用什么语言导出csv文件没有关系.Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法:如果数字大于15位,它不仅用于科学技术费表示,还会只保留高15位,其他位都变0.解决 ...
- web前端导出csv文件
前言 导出文件,使用最多的方式还是服务器端来处理.比如jsp 中使用response 的方式. 但是,有时候可能就想使用web 前端是否也可以把页面上的内容导出来呢? 比如说,导出页面的一个表格. 这 ...
- 转:导出csv文件数字会自动变科学计数法的解决方法
导出csv文件数字会自动变科学计数法的解决方法 其实这个问题跟用什么语言导出csv文件没有关系.Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法:如果数字大于15位,它不仅用于科 ...
随机推荐
- 在select2插件中append下拉选,点击没反应的解决
今天前端大佬帮我解决了一个棘手的问题:克隆了已有系统的网页,尝试把复制下来的html改造成jsp.基本功能正常,然而点击新增按钮,出来的行无法点击下拉选,控制台也没报错. 项目用的是jeesite2. ...
- overflow text-overflow 超过部分隐藏问题
overflow:是针对容器内所有的数据溢出的一种统一处理方式,不管容器内的存储的是文本 图片还是其他的数据 统一取值; hidden隐藏, scroll滚动条显示,visible溢出显示text-o ...
- 系统学习javaweb3----HTML语言3(结束)
说明:昨天是北方小年,需要做的事情有点多,需要祭灶,扫尘.包饺子,吃糖瓜儿,学习时间有点少,所以今天将两天的知识综合一下发出. 自我感觉:虽然感觉大致都了解了HTML语言,但是感觉自己面对程序还是无从 ...
- [Machine Learning] Andrew Ng on Coursera (Week 1)
Week 1 的内容主要有: 机器学习的定义 监督式学习和无监督式学习 线性回归和成本函数 梯度下降算法 线性代数回归 主要是了解一下机器学习的基本概念,重点是学习线性回归模型,以及对应的成本函数和梯 ...
- 花生壳的ddns 关键时刻又掉链子,准备迁到阿里万网
https://www.oray.com/news/affiche/?aid=628 免费版花生壳服务故障 因免费版机房线路节点负荷突然暴增,导致花生壳免费版登录缓慢或异常,或出现域名指向到127.0 ...
- java课java方法动手动脑
动手动脑: import java.util.Scanner; public class Random { public static void main(String[] args) { ...
- z-index优先级小结
z-index是深度属性,设置元素在z轴上面的堆叠顺序. 强调:z-index必须和定位元素position:absollute|relative|fixed一起使用,否则无效 1.z-index属性 ...
- 牛客-Highway
题目传送门 sol:看了题意显然是最大生成树,但是任意两个点之间都有边,大概有n*n条边.用朴素的最小生成树算法显然不行.联想了一下树的直径还是不会.看了大佬的题解,懂了... 所以还是直接贴大佬博客 ...
- MySQL之数据存储引擎
1.什么是存储引擎: 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处 理文本用txt类型,处理表格用excel,处理图片用png等,数据库中的表也应该有不同的 ...
- [LC] 380. Insert Delete GetRandom O(1)
Design a data structure that supports all following operations in average O(1) time. insert(val): In ...