- /*
- * PHP code to export MySQL data to CSV
- *
- * Sends the result of a MySQL query as a CSV file for download
- * Easy-to-convert to UTF-8.
- */
- /*
- * Establish database connection
- */
- $conn = mysql_connect (' localhost ', ' login ', ' pass ') or Die (Mysql_error ());
- mysql_select_db (' database_name ', $conn) or Die (Mysql_error ($conn));
- mysql_query ("SET NAMES CP1252");
- /*
- * Execute SQL query
- */
- $query = sprintf (' SELECT field1,field2 from table_name ');
- $result = mysql_query ($query, $conn) or Die (Mysql_error ($conn));
- /*
- * Send response headers to the browser
- * Following headers instruct the browser to treat the data as a CSV file called Export.csv
- */
- Header (' content-type:text/csv; charset=cp1252 ');
- Header (' content-disposition:attachment;filename=output.csv ');
- /*
- * Output header row (if atleast one row exists)
- */
- $row = Mysql_fetch_assoc ($result);
- if ($row) {
- Echocsv (Array_keys ($row));
- }
- /*
- * Output data rows (if atleast one row exists)
- * * Export CSV file
- while ($row) {
- Echocsv ($row);
- $row = Mysql_fetch_assoc ($result);
- }
- /*
- * echo the input array as CSV data maintaining consistency with most CSV implementations
- *-Uses double-quotes as enclosure when necessary
- *-Uses double double-quotes to escape double-quotes
- *-Uses CRLF as a line separator
- */
- function Echocsv ($fields)
- {
- $separator = ";
- foreach ($fields as $field) {
- if (Preg_match ('/\\r|\\n|,| ' /', $field)) {
- $field = ' "'. Str_replace (' "', '" ' "', $field) ';
- }
- Echo $separator. $field;
- $separator = ', ';
- }
- echo "\ r \ n";
- }
- ?>
Copy CodeSecond, PHP Import and export the CSV file class PHP implements the import and export classes of CSV files. Code:
- /**
- * CSV file Processing class
- */
- Class csv{
- Public $csv _array; CSV array data
- Public $csv _str; CSV file data
- Public function __construct ($param _arr, $column) {
- $this->csv_array = $param _arr;
- $this->path = $path;
- $this->column = $column;
- }
- /**
- * Export
- * */
- Public Function export () {
- if (Empty ($this->csv_array) | | empty ($this->column)) {
- return false;
- }
- $param _arr = $this->csv_array;
- unset ($this->csv_array);
- $export _str = Implode (', ', $param _arr[' nav '). " n ";
- unset ($param _arr[' nav ');
- Assembly data
- foreach ($param _arr as $k = + $v) {
- foreach ($v as $k 1=> $v 1) {
- $export _str. = Implode (', ', $v 1). " n ";
- }
- }
- Export the $export_str
- Header ("Cache-control:public");
- Header ("Pragma:public");
- Header ("Content-type:application/vnd.ms-excel");
- Header ("Content-disposition:attachment;filename=txxx.csv");
- Header (' Content-type:application/octet-stream ');
- Ob_start ();
- $file _str= iconv ("Utf-8", ' GBK ', $export _str);
- Ob_end_clean ();
- echo $export _str;
- }
- /**
- * Import
- * */
- Public Function Import ($path, $column = 3) {
- $flag = flase;
- $code = 0;
- $msg = ' not processed ';
- $filesize = 1; 1MB
- $maxsize = $filesize * 1024 * 1024;
- $max _column = 1000;
- Detects if a file exists
- if ($flag = = = Flase) {
- if (!file_exists ($path)) {
- $msg = ' file does not exist ';
- $flag = true;
- }
- }
- Detecting file formats
- if ($flag = = = Flase) {
- $ext = Preg_replace ("/.*." ( [^.] +)/"," $ ", $path);
- if ($ext! = ' csv ') {
- $msg = ' Import only csv format file ';
- $flag = true;
- }
- }
- Detecting File size
- if ($flag = = = Flase) {
- if (filesize ($path) > $maxsize) {
- $msg = ' The imported file must not exceed '. $maxsize. ' b file ';
- $flag = true;
- }
- }
- Read file
- if ($flag = = flase) {
- $row = 0;
- $handle = fopen ($path, ' R ');
- $dataArray = Array ();
- while ($data = Fgetcsv ($handle, $max _column, ",")) {
- $num = count ($data);
- if ($num < $column) {
- $msg = ' file does not conform to specifications true to have: '. $num. ' Column data ';
- $flag = true;
- Break
- }
- if ($flag = = = Flase) {
- for ($i =0; $i <3; $i + +) {
- if ($row = = 0) {
- Break
- }
- Build data
- $dataArray [$row] [$i] = $data [$i];
- }
- }
- $row + +;
- }
- }
- return $dataArray;
- }
- }
- $param _arr = Array (
- ' Nav ' =>array (' username ', ' password ', ' email '),
- Array (0=>array (' xiaohai1 ', ' 123456 ', ' xiaohai1@jbxue.com '),
- 1=>array (' Xiaohai2 ', ' 213456 ', ' xiaohai2@jbxue.com '),
- 2=>array (' Xiaohai3 ', ' 123456 ', ' xiaohai3@jbxue.com ')
- ));
- $column = 3;
- $csv = new CSV ($param _arr, $column);
- $csv->export ();
- $path = ' C:\Documents and Settings\administrator\temp\txxx.csv ';
- $import _arr = $csv->import ($path, 3);
- Var_dump ($import _arr);
- ?>
Copy Code |