-
-
/*
- * 將MySQL 資料匯出到CSV 的PHP 程式碼
- *
- * 傳送MySQL 的結果作為查詢CSV 檔案下載
- * 易於轉換為UTF-8。
- */
-
- /*
- * 建立資料庫連線
- */
-
- $conn = mysql_connect('localhost', 'login', 'pass') 或die (mysql_error());
- mysql_select_db('database_name', $conn) 或die(mysql_error($conn));
- mysql_query("SET NAMES CP1252");
- mysql_query("SET NAMES CP1252");
- /*
- * 執行sql查詢
- */
- $query = sprintf('SELECT field1,field2 FROM table_name');
- $result = mysql_query($query, $conn) 或die(mysql_error($conn));
- /*
- * 將回應標頭髮送到瀏覽器
- * 以下標頭指示瀏覽器將資料視為csv 檔案名稱為export.csv
- */
- header('Content- Type: text/csv; charset=cp1252');
- header('Content-Disposition: Attachment;filename=output.csv');
- /*
- * 輸出標題行(如果至少存在一行)
- */
-
- $row = mysql_fetch_assoc($result);
- if ($row) {
- echocsv(array_keys($row));
- }
-
- /*
- * 輸出資料行(如果至少存在一行)
- */ 匯出csv檔
- while ($row) {
- echocsv($row);
- $row = mysql_fetch_assoc( $result);
- }
-
- /*
- * 將輸入數組回顯為csv 數據,與大多數CSV 實現保持一致
- * - 必要時使用雙引號作為括弧
- * - 使用雙雙引號來轉義雙引號
- * - 使用CRLF 作為行分隔符號
- */
-
- function echocsv($fields)
- {
- $separator = ' ';
- foreach ($fields as $field) {
- if ( preg_match('/\r|\n|,|"/', $field)) {
- $field = '"' . str_replace('"', '""', $field) . '"';
- }
- echo $separator . $field;
- $separator = ',';
- }
- echo "rn";
- }
?>
複製程式碼
二、php匯入與匯出csv檔的類別
php實作csv檔案的匯入和匯出類別。
代碼:
-
-
/**
- * CSV 檔案處理類別
- */
- class Csv{
- public $csv_array;
- public $csv_str; //csv檔案資料
- public function __construct($param_arr, $column){
- $this->csv_array = $param_arr;
- $this->path = $path;
- $this->column = $column;
- }
- /**
- * 導出
- **/
- public function export(){
- if(empty($this->csv_array) | | empty($this->column)){
- return false;
- }
- $param_arr = $this->csv_array;
- unset($this->csv_array);
- $export_str> $export_str = implode(',',$param_arr['nav'])."n";
- unset($param_arr['nav']);
- //組裝資料
- foreach($param_arr as $ k=>$v){
- foreach($v as $k1=>$v1){
- $export_str .= implode(',',$v1)."n";
- }
- }
- //將$export_str導出
- header( "Cache-Control: public" );
- header( "Pragma: public" );
- header("Content-type:application/vndtype:application/vndtype:application/vndtype:application/vndtype:application/vndtype .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;
- }
- /**
- * 導入
- **/
- public function import($path,$column = 3){
- $flag = flase;
- $code = 0;
- $msg = '未處理';
- $code = 0;
- $msg = '未處理';
- $filesize = 1; //1MB
- $maxsize = $filesize * 1024 * 1024;
- $max_column = 1000;
-
- //偵測檔案是否存在
- if($flag = == flase){
- if(!file_exists($path)){
- $msg = '檔案不存在';
- $flag = true;
- }
- }
- / /偵測檔案格式
- if($flag === flase){
- $ext = preg_replace("/.*.([^.] )/","$1",$path);
- if($ext != 'csv'){
- $msg = '只能匯入CSV格式檔案';
- $flag = true;
- }
- }
- //偵測檔案大小
- if($flag === flase){
- if(filesize($path)>$maxsize){
- $msg = '匯入的檔案不得超過'.$maxsize.'B檔案';
- $flag = true;
- }
- }
- //讀取檔案
- if($flag == flase){
- $row = 0;
- $handle = fopen($path,'r');
- $dataArray = array();
- while($data = fgetcsv($handle,$max_column,",")){
- $num = count( $data);
- if($num $msg = '檔案不符合規格真實有:'.$num.'列資料';
- $flag = true;
- break;
- }
- if($flag === flase){
- for($i=0;$i if($row == 0) {
- break;
- }
- //組成資料
- $dataArray[$row][$i] = $data[$i];
- }
- }
- $ row ;
- }
- }
- return $dataArray;
- }
- }
- $param_arr = array(
- 'nav'=>array('用戶名','密碼','郵箱'),
- 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 SettingsAdministratorTemptxxx.csv';
- $import_arr = $ettingsAdministratorTemptxxx.csv';
$import_arr = $cscsv ->import($path,3); var_dump($import_arr); ?>
| 複製程式碼