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);
? >