1. Read CSV data and output to Sales.csv file:
$sales = Array (
' northeast ', ' 2004-01-01 ', ' 2004-02-01 ', 12.54),
Array (' Northwest ', ' 2004-01-01 ', ') 2004-02-01 ', 546.33,
Array (' Southeast ', ' 2004-01-01 ', ' 2004-02-01 ', 93.26),
Array (' Southwest ', ' 2004-01-01 ', ' 2004-02-01 ', 945.21),
Array (' All regions ', '---', '--', 1597.34 '),
);
$fh = fopen (' sales.csv ', ' w ') or Die ("Can ' t Open sales.csv");
foreach ($sales as $sales _line) {
if (fputcsv ($fh, $sales _line) = = False) {
die ("Can ' t write CSV line");
}
}
Fclose ($FH) or Die ("Can ' t close sales.csv");
2. Read CSV data, use special stream output
$sales = Array (
' northeast ', ' 2004-01-01 ', ' 2004-02-01 ', 12.54),
Array (' Northwest ', ' 2004-01-01 ', ') 2004-02-01 ', 546.33,
Array (' Southeast ', ' 2004-01-01 ', ' 2004-02-01 ', 93.26),
Array (' Southwest ', ' 2004-01-01 ', ' 2004-02-01 ', 945.21),
Array (' All regions ', '---', '--', 1597.34 '),
);
$fh = fopen (' Php://output ', ' W ');
foreach ($sales as $sales _line) {
if (fputcsv ($fh, $sales _line) = = False) {
die ("Can ' t write CSV line");
}
}
Fclose ($FH);
3. Read CSV data, output to buffer
$sales = Array (
' northeast ', ' 2004-01-01 ', ' 2004-02-01 ', 12.54),
Array (' Northwest ', ' 2004-01-01 ', ') 2004-02-01 ', 546.33,
Array (' Southeast ', ' 2004-01-01 ', ' 2004-02-01 ', 93.26),
Array (' Southwest ', ' 2004-01-01 ', ' 2004-02-01 ', 945.21),
Array (' All regions ', '---', '--', 1597.34 '),
);
Ob_start ();
$fh = fopen (' Php://output ', ' w ') or Die ("Can ' t Open php://output");
foreach ($sales as $sales _line) {
if (fputcsv ($fh, $sales _line) = = False) {
die ("Can ' t write CSV line");
}
}
Fclose ($FH) or Die ("Can ' t close Php://output");
$output = Ob_get_contents ();
Ob_end_clean ();
4. Read data from CSV file
$fp = fopen (' sample3.csv ', ' r ') or Die ("can ' t open file");
print "<table>\n";
while ($csv _line = Fgetcsv ($fp)) {
print ' <tr> ';
For ($i =0, $j =count ($csv _line); $i < $j; $i + +) {
//print ' <td> '. Htmlentities ($csv _line[$i]). ' </td> ';
print ' <td> '. Htmlentities (Iconv ("gb2312", "Utf-8", $csv _line[$i]). ' </td> ';
}
print "</tr>\n";
print "</table>\n";
Fclose ($fp) or Die ("can ' t close file");
5. Download csv file
$sales = Array (
' northeast ', ' 2004-01-01 ', ' 2004-02-01 ', 12.54),
Array (' Northwest ', ' 2004-01-01 ', ') 2004-02-01 ', 546.33,
Array (' Southeast ', ' 2004-01-01 ', ' 2004-02-01 ', 93.26),
Array (' Southwest ', ' 2004-01-01 ', ' 2004-02-01 ', 945.21),
Array (' China ', ' 2004-01-01 ', ' 2004-02-01 ', 945.21),
);
$fh = fopen (' Php://output ', ' w ') or Die ("can ' t open php://output");
$total = 0;
Tell the browser to send a CSV file
header (' content-type:application/csv ');
Header (' content-disposition:attachment; filename= "Sales.csv");
Output Header
fputcsv ($output, Array (' Region ', ' Start date ', ' End Date ', ' Amount '));
Outputs each row of data and increments $total
foreach ($sales as $sales _line) {
if (fputcsv ($fh, $sales _line) = False) {
Die (" Can ' t write CSV line ");
} else{
$total + + $sales _line[3]
;
}
Fputcsv ($fh, Array (' All regions ', '--', '--', $total));
Fclose ($FH) or Die ("Can ' t close php://output");
6. read CSV file specify line and interval rows
/***** reads the specified row *****/function get_file_line_a ($file _name, $line) in the csv file $n = 0;
$handle = fopen ($file _name, ' R ');
if ($handle) {while (!feof ($handle)) {+ + $n;
$out = Fgets ($handle, 4096);
if ($line = = $n) break;
} fclose ($handle);
if ($line = = $n) return $out;
return false; Echo get_file_line ("Windows_2011_s.csv", 10)//Enter the 10th line of content/***** read the interval row in the CSV file *****/function Get_file_line_b ($file _
Name, $line _star, $line _end) {$n = 0;
$handle = fopen ($file _name, "R");
if ($handle) {while (!feof ($handle)) {+ + $n;
$out = Fgets ($handle, 4096);
if ($line _star <= $n) {$ling [] = $out;
if ($line _end = = $n) break;
} fclose ($handle);
if ($line _end== $n) return $ling;
return false; Read and output rows 11th to 20th $AA = Get_file_line ("Windows_2011_s.csv", 11, 20) with Get_file_line; From line 11th to line 20th, foreach ($aa as $BB) {echo $bb. "
<br> "; }