php csv操作类代码
程序员文章站
2022-06-12 18:28:11
请点击后面地址下载: 使用说明如下: 1.生成csv文件 复制代码 代码如下: require "./include/csvdatafile.php"; set_time_...
请点击后面地址下载:
使用说明如下:
1.生成csv文件
require "./include/csvdatafile.php";
set_time_limit(200);
header("content-type: application/rfc822");
header('content-disposition: attachment; filename=export.csv');
$arr_export_titles = array("学生编号","学号","学生姓名");
$csvfile = new csvdatafile("", ",", "w");
echo $csvfile->printline($arr_export_titles);
//方法一
$print_data1[] = 1;
$print_data1[] = "039413301";
$print_data1[] = "张三";
echo $csvfile->printline($print_data1);
$print_data2[] = 2;
$print_data2[] = "039413302";
$print_data2[] = "李四";
echo $csvfile->printline($print_data2);
$print_data3[] = 3;
$print_data3[] = "039413303";
$print_data3[] = "王五";
echo $csvfile->printline($print_data3);
//方法二
$print_data[1][] = 1;
$print_data[1][] = "039413301";
$print_data[1][] = "张三";
$print_data[2][] = 2;
$print_data[2][] = "039413302";
$print_data[2][] = "李四";
$print_data[3][] = 3;
$print_data[3][] = "039413303";
$print_data[3][] = "王五";
echo $csvfile->printcsv($print_data);
2.打开csv读数据
代码
require "./include/csvdatafile.php";
$filename = "e:/development/csvfile/datefile.csv";
// read file source
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
// format content for special chars
$contents = @addslashes($contents);
$contents = @str_replace('\,', '\ ,', $contents);
$contents = @stripslashes($contents);
// write to new file
$handle = @fopen($filename, "w");
@fwrite($handle, $contents);
@fclose($handle);
$fd = @fopen($filename, "rb");
$first_line = str_replace(' ,',',',str_replace('"','',trim(@fgets($fd, 1000)))) ;
@fclose($fd);
if($first_line != "学生编号,学号,学生姓名") {
$pass = false;
}
if($pass){
$csv = new csvdatafile($filename);
while($csv->next_row()) {
$userid = trim($csv->f('学生编号'));
$classno = trim($csv->f('学号'));
$username = trim($csv->f('学生姓名'));
}
}
使用说明如下:
1.生成csv文件
复制代码 代码如下:
require "./include/csvdatafile.php";
set_time_limit(200);
header("content-type: application/rfc822");
header('content-disposition: attachment; filename=export.csv');
$arr_export_titles = array("学生编号","学号","学生姓名");
$csvfile = new csvdatafile("", ",", "w");
echo $csvfile->printline($arr_export_titles);
//方法一
$print_data1[] = 1;
$print_data1[] = "039413301";
$print_data1[] = "张三";
echo $csvfile->printline($print_data1);
$print_data2[] = 2;
$print_data2[] = "039413302";
$print_data2[] = "李四";
echo $csvfile->printline($print_data2);
$print_data3[] = 3;
$print_data3[] = "039413303";
$print_data3[] = "王五";
echo $csvfile->printline($print_data3);
//方法二
$print_data[1][] = 1;
$print_data[1][] = "039413301";
$print_data[1][] = "张三";
$print_data[2][] = 2;
$print_data[2][] = "039413302";
$print_data[2][] = "李四";
$print_data[3][] = 3;
$print_data[3][] = "039413303";
$print_data[3][] = "王五";
echo $csvfile->printcsv($print_data);
2.打开csv读数据
代码
复制代码 代码如下:
require "./include/csvdatafile.php";
$filename = "e:/development/csvfile/datefile.csv";
// read file source
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
// format content for special chars
$contents = @addslashes($contents);
$contents = @str_replace('\,', '\ ,', $contents);
$contents = @stripslashes($contents);
// write to new file
$handle = @fopen($filename, "w");
@fwrite($handle, $contents);
@fclose($handle);
$fd = @fopen($filename, "rb");
$first_line = str_replace(' ,',',',str_replace('"','',trim(@fgets($fd, 1000)))) ;
@fclose($fd);
if($first_line != "学生编号,学号,学生姓名") {
$pass = false;
}
if($pass){
$csv = new csvdatafile($filename);
while($csv->next_row()) {
$userid = trim($csv->f('学生编号'));
$classno = trim($csv->f('学号'));
$username = trim($csv->f('学生姓名'));
}
}