程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php csv操作類代碼

php csv操作類代碼

編輯:關於PHP編程

請點擊後面地址下載:csv操作類
使用說明如下:

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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved