Php operation csv file code instance summary

Source: Internet
Author: User
1. read the csvdata and output it to the sales.csv File: $ salesarray (array (Northeast, 2004-01-01,2004-02-01, 12.54), array (Northwest, 2004-01-01,2004-02-01,546.33 ), array (Southeast, 200 1. read csv data and output it to the sales.csv file:

$sales = array(  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 and use special stream output

$sales = array(  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 and output it to the buffer.

$sales = array(  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 csv file data

$fp = fopen('sample3.csv', 'r') or die("can't open file");print "
 
 
  
  \n";while($csv_line = fgetcsv($fp)){  print '
  
  
     '; for($i=0, $j=count($csv_line); $i<$j; $i++){ // print ' 
    '; print ' 
    '; } print " 
   \n";}print " 
  
'.htmlentities($csv_line[$i]).''.htmlentities(iconv("gb2312","utf-8",$csv_line[$i])).'
\n";fclose($fp) or die("can't close file");

5. download the CSV file

$ Sales = array ('Northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('Southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('China', '2017-01-01 ', '2017-02-01', 2004 ),); $ 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'); // output each row of data, increment $ totalforeach ($ 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 the specified row and interval rows of the CSV file.

/***** Read the specified row in the CSV file *****/function get_file_line_a ($ file_name, $ line) {$ 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_201511s.csv", 10 ); // enter 10th rows/***** read the range 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;} // use get_file_line to read and output 11th rows to 20th rows $ aa = get_file_line ("windows_201511s.csv", 11, 20 ); // from row 11th to row 20th foreach ($ aa as $ bb) {echo $ bb."
";}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.