您的位置:首页 > 其它

csv文件导入导出

2013-03-29 14:50 471 查看
<?php
class CSV_Import_Export {

private function export_term_user(){
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="学员成绩.csv"');
header('Cache-Control: max-age=0');
$fp = fopen('php://output', 'a');

$thead = '编号,学员,培训时间,最后成绩'."\n";
fputs($fp, $this->expChangeCode($thead));
$row = array(1,'Mike','2013/12/01',90);
$tbody = join(',', $row)."\n";
fputs($fp, $this->expChangeCode($tbody));

ob_flush();
flush();
}
private function expChangeCode($str)
{
$str = iconv('UTF-8', 'gb18030//IGNORE', $str);
return $str;
}
private function importScores($file){
$handle = fopen($file['tmp_name'],'r');
$this->checklines($handle);
fclose($handle);
}
private function getline($handle) {
if($line = fgetcsv($handle)) { /*此处如果用fgets(),当遇上csv表格里面有换行就有问题了*/
foreach ($line as &$v){
$v = iconv( 'gb18030//IGNORE', 'UTF-8', $v);
}
return $line;
}
else return false;
}
private function checklines($handle){
while($line = $this->getline($handle)){
var_dump($line);
}
}

public function __construct(){
if($_REQUEST['action'] == 'export') {
$this->export_term_user();
}
elseif ($_REQUEST['action'] == 'import') {
$this->import_term_user();
}
}
}
new CSV_Import_Export();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: