PHP processing CSV Table File Common operation method Summary _php Example

Source: Internet
Author: User

To do the online Excel table editing function, Excel xls file format parsing is a problem, after all, this is the Microsoft Office private patent format.
So to do it, use the universal CSV (Comma separated value, comma separated values) format.
A variety of office software can recognize the CSV table, in fact, is a specific separator (such as commas) to separate the cells table.
For PHP, Fgetcsv reads the CSV table, returns an array,
Then foreach outputs the <table> of HTML, which can be done in a few lines of code, very simple.
The workload is mainly in the front end of the browser, it is recommended that you use jquery for DOM and AJAX operations,
It's not difficult to double-click a cell edit as fine-grained as phpMyAdmin, and then the Ajax commit
Or the entire table is written after a one-time $ ("form"). Serialize (); Then the AJAX submission is OK.

Several rules for the CSV table:
1. The cell contents of each row are separated by commas.
2. If the contents of the cell itself have commas, the contents of the cell will be enclosed in quotation marks.
3. If the contents of the cell itself have quotes:
(1) Quotation marks are not in the first or end, the contents of this cell will not be enclosed in quotes.
(2) The quotation marks are at the beginning or end, the contents of the cell are enclosed in quotation marks, and the original closing quotation marks are escaped.

Read and write CSV

<?php
header (' Content-type:text/plain; Charset=utf-8 ');

Export CSV table: array csv
$arr = arrays (
  ' AB ', ' CD '),
  array (' a,b ', ' "c,d")
;
$fp = fopen (' file.csv ', ' W ');
foreach ($arr as $row) {
  //format a row as a CSV and write to the file pointer
  fputcsv ($FP, $row);
Fclose ($FP);
Unset ($arr);

Import CSV table: CSV to array
$fp = fopen (' file.csv ', ' R ');
while ($row = Fgetcsv ($fp))!== FALSE) {
  //reads a row from the file pointer and resolves the CSV
  $arr [] = $row;
}
Fclose ($FP);
Var_export ($arr);

Save as a separate file
download.php:

 <?php
$list = Array (
  ' aaa,bbb,ccc,dddd ', '
  123,456,789 ', '
  "AAA", "BBB"
);
Session_Start ();
$_session[' Outputarray ']= $list;
? >
<a href= "test.php" target= "_blank" > download csv file </a>

test.php:
<?php
session_start ();
$outputArray =$_session[' Outputarray '];
Header (' content-type:application/csv ');
Header (' Content-disposition:attachment;filename= "sales.csv");
$output =fopen (' Php://output ', ' w ') or Die ("Can not open");
foreach ($outputArray as $line) {
  fputcsv ($output, Split (', ', $line));
}
Fclose ($output) or Die ("Can not Close");
? >

Automatically save CSV file to specified location
<?php

$list = Array (
  ' aaa,bbb,ccc,dddd ', '
  123,456,789 ', '
  "AAA", "BBB"
);

$fp = fopen (' file.csv ', ' W ');

foreach ($list as $line) {
  fputcsv ($fp, Split (', ', $line));
}

Fclose ($FP);
? >

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.