CVS檔案的匯入和匯出php類代碼

來源:互聯網
上載者:User

CVS檔案的匯入和匯出php類代碼,通過這個自訂的php類可以實現資料庫的資料和CVS檔案的轉換

  1. /**
  2. * CSV 檔案處理類
  3. */
  4. class Csv{
  5. public $csv_array; //csv數組資料
  6. public $csv_str; //csv檔案資料
  7. public function __construct($param_arr, $column){
  8. $this->csv_array = $param_arr;
  9. $this->path = $path;
  10. $this->column = $column;
  11. }
  12. /**
  13. * 匯出
  14. * */
  15. public function export(){
  16. if(empty($this->csv_array) || empty($this->column)){
  17. return false;
  18. }
  19. $param_arr = $this->csv_array;
  20. unset($this->csv_array);
  21. $export_str = implode(',',$param_arr['nav'])."n";
  22. unset($param_arr['nav']);
  23. //組裝資料
  24. foreach($param_arr as $k=>$v){
  25. foreach($v as $k1=>$v1){
  26. $export_str .= implode(',',$v1)."n";
  27. }
  28. }
  29. //將$export_str匯出
  30. header( "Cache-Control: public" );
  31. header( "Pragma: public" );
  32. header("Content-type:application/vnd.ms-excel");
  33. header("Content-Disposition:attachment;filename=txxx.csv");
  34. header('Content-Type:APPLICATION/OCTET-STREAM');
  35. ob_start();
  36. // $file_str= iconv("utf-8",'gbk',$export_str);
  37. ob_end_clean();
  38. echo $export_str;
  39. }
  40. /**
  41. * 匯入
  42. * */
  43. public function import($path,$column = 3){
  44. $flag = flase;
  45. $code = 0;
  46. $msg = '未處理';
  47. $filesize = 1; //1MB
  48. $maxsize = $filesize * 1024 * 1024;
  49. $max_column = 1000;
  50. //檢測檔案是否存在
  51. if($flag === flase){
  52. if(!file_exists($path)){
  53. $msg = '檔案不存在';
  54. $flag = true;
  55. }
  56. }
  57. //檢測檔案格式
  58. if($flag === flase){
  59. $ext = preg_replace("/.*.([^.]+)/","$1",$path);
  60. if($ext != 'csv'){
  61. $msg = '只能匯入CSV格式檔案';
  62. $flag = true;
  63. }
  64. }
  65. //檢測檔案大小
  66. if($flag === flase){
  67. if(filesize($path)>$maxsize){
  68. $msg = '匯入的檔案不得超過'.$maxsize.'B檔案';
  69. $flag = true;
  70. }
  71. }
  72. //讀取檔案
  73. if($flag == flase){
  74. $row = 0;
  75. $handle = fopen($path,'r');
  76. $dataArray = array();
  77. while($data = fgetcsv($handle,$max_column,",")){
  78. $num = count($data);
  79. if($num < $column){
  80. $msg = '檔案不符合規格真實有:'.$num.'列資料';
  81. $flag = true;
  82. break;
  83. }
  84. if($flag === flase){
  85. for($i=0;$i<3;$i++){
  86. if($row == 0){
  87. break;
  88. }
  89. //組建資料
  90. $dataArray[$row][$i] = $data[$i];
  91. }
  92. }
  93. $row++;
  94. }
  95. }
  96. return $dataArray;
  97. }
  98. }
  99. $param_arr = array(
  100. 'nav'=>array('使用者名稱','密碼','郵箱'),
  101. array(0=>array('xiaohai1','123456','xiaohai1@zhongsou.com'),
  102. 1=>array('xiaohai2','213456','xiaohai2@zhongsou.com'),
  103. 2=>array('xiaohai3','123456','xiaohai3@zhongsou.com')
  104. ));
  105. $column = 3;
  106. $csv = new Csv($param_arr, $column);
  107. //$csv->export();
  108. $path = 'C:Documents and SettingsAdministratorLocal SettingsTemptxxx.csv';
  109. $import_arr = $csv->import($path,3);
  110. var_dump($import_arr);
  111. ?>
複製代碼
CVS, php
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.