CSV file is a plain text file, but using Excel file opening can be used as an Excel file, let me summarize several commonly used PHP instances to read Excel.
PHP has its own analysis. csv function: fgetcsv
Array fgetcsv (int $handle [, int $length [, String $delimiter [, String $enclosure]])
Handle a valid file pointer produced by fopen (), Popen (), or fsockopen ().
Length (optional) must be greater than the longest line in the CVS file. This parameter is optional in PHP 5. If this parameter is omitted (set to 0 in a later version of PHP 5.0.4), then there is no limit to the length, but it may affect execution efficiency.
Delimiter (optional) Sets the field delimiter (one character only), and the default value is a comma.
Enclosure (optional) Sets the field wrapping character (only one character is allowed), and the default value is double quotation marks. This parameter is added in PHP 4.3.0. Similar to Fgets (), only fgetcsv () resolves the read-in rows and finds fields in CSV format and returns an array containing those fields.
Fgetcsv () returns FALSE when an error occurs, including when the file ends.
Note: Empty rows in the CSV file are returned as an array containing a single null field and will not be treated as an error.
Cases
| The code is as follows |
Copy Code |
$row = 1; $handle = fopen ("Test.csv", "R"); while ($data = Fgetcsv ($handle, 1000, ",")) { $num = count ($data); echo " $num fields $row: n "; $row + +; for ($c =0; $c < $num; $c + +) { echo $data [$c]. "N"; } } Fclose ($handle); ?> |
Example 2
Baidu statistics and webmaster tools used in the process will involve a lot of CSV files, such as we download Baidu Webmaster Tools 404 Statistics, directly can use the following PHP script to read the CSV file and then update the submission.
PHP read the Excel file (. csv) Reference code:
| code as follows |
copy code |
function Getcsvdata ($filename) { $row = 1;//First line starts if ($handle = fopen ($filename, "R"))!== false) { while (($DATASRC = Fgetcsv ($handle))!== false) { $num = count ($DATASRC); for ($c =0; $c < $num; $c + +)//Columns column { if ($row = = = 1)//First row as field { $dataName [] = $DATASRC [$c];//field name } Else { foreach ($dataName as $k = $v) { if ($k = = $c)//fields corresponding to { $data [$v] = $DATASRC [$c]; } } } } if (!empty ($data)) { $dataRtn [] = $data; Unset ($data); } $row + +; } Fclose ($handle); return $dataRtn; } }
$aData = Getcsvdata (' all_www.bKjia.c0m. csv ');
foreach ($aData as $k = = $v) { echo "http://" $v [' a ']. " "; } ?> |
PHP Custom Classes
Pros: Cross-platform. Some classes support write operations. Support for. xls binaries
The common classes are phpexcelreader and Phpexcel. The latter supports read and write, but requires php5.2 or more.
The Phpexcelreader is designed to read files. Returns an array that contains all the contents of the table.
The method used by this class can refer to the example.php in compressed files downloaded from the website.
Example 3.php data import and Export Excel
Upload CVS and import into the database, the test is successful (some of the code is not standardized, such as php_self there to be rewritten as
| The code is as follows |
Copy Code |
$_server["Php_self"]) PHP code $fname = $_files[' MyFile ' [' name ']; $do = Copy ($_files[' MyFile ' [' tmp_name '], $fname); if ($do) { echo "Import data successfully "; } else { echo ""; } ?>
error_reporting (0); Import files in CSV format $connect =mysql_connect ("localhost", "a0530093319", "123456") or Die ("could does connect to database"); mysql_select_db ("a0530093319", $connect) or Die (Mysql_error ()); $fname = $_files[' MyFile ' [' name ']; $handle =fopen ("$fname", "R"); while ($data =fgetcsv ($handle, 10000, ",")) { $q = "INSERT INTO Test (code,name,date) VALUES (' $data [0] ', ' $data [1] ', ' $data [2] ')"; mysql_query ($q) or Die (Mysql_error ()); } Fclose ($handle); ?> |
Use PHP to export the database to Excel, test completely successful
PHP code
| The code is as follows |
Copy Code |
$DB _server = www.bKjia.c0m;//Here is your data connection $DB _username = "a0530093319"; $DB _password = "123456"; $DB _dbname = "a0530093319"; $DB _tblname = "Member"; $savename = Date ("Ymjhis"); $Connect = @mysql_connect ($DB _server, $DB _username, $DB _password) Or Die ("couldn ' t connect."); mysql_query ("Set Names ' gb2312 '"); $file _type = "Vnd.ms-excel"; $file _ending = "xls"; Header ("content-type:application/$file _type"); Header ("content-disposition:attachment; Filename= ". $savename.". $file _ending "); Header ("Pragma:no-cache"); Header ("expires:0"); $now _date = Date ("Y-m-j h:i:s"); $title = "Database name: $DB _dbname, Data sheet: $DB _tblname, Backup date: $now _date"; $sql = "SELECT * from $DB _tblname"; $ALT _db = @mysql_select_db ($DB _dbname, $Connect) Or Die ("couldn ' t Select Database"); $result = @mysql_query ($sql, $Connect) Or Die (Mysql_error ()); Echo ("$title"); $sep = ""; for ($i = 0; $i < Mysql_num_fields ($result); $i + +) { Echo Mysql_field_name ($result, $i). " "; } Print (""); $i = 0; while ($row = Mysql_fetch_row ($result)) { $schema _insert = ""; for ($j =0; $j if (!isset ($row [$j])) $schema _insert. = "NULL". $sep; ElseIf ($row [$j]! = "") $schema _insert. = "$row [$j]". $sep; Else $schema _insert. = "". $sep; } $schema _insert = Str_replace ($sep. " $ "," ", $schema _insert); $schema _insert. = ""; Print (Trim ($schema _insert)); Print ""; $i + +; } return (true); ?> |
http://www.bkjia.com/PHPjc/632917.html www.bkjia.com true http://www.bkjia.com/PHPjc/632917.html techarticle CSV file is a plain text file, but using Excel file opening can be used as an Excel file, let me summarize several commonly used PHP instances to read Excel. PHP has its own analysis. csv function ...