csv.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * 邮件类
  4. *
  5. * 邮件操作类,目前只支持smtp服务的邮件发送
  6. *
  7. *
  8. * @package
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. final class Csv{
  12. public $filename;
  13. public function export($data){
  14. if (!is_array($data)) return;
  15. $string = '';$new = array();
  16. foreach ($data as $k=>$v) {
  17. foreach ($v as $xk=>$xv) {
  18. $v[$xk] = str_replace(',','',$xv);
  19. }
  20. $new[] = implode(',',$v);
  21. }
  22. if (!empty($new)){
  23. $string = implode("\n",$new);
  24. }
  25. header("Content-Type: application/vnd.ms-excel; charset=GBK");
  26. header("Content-Disposition: attachment;filename={$this->filename}.csv");
  27. echo $string;
  28. }
  29. /**
  30. * 转码函数
  31. *
  32. * @param mixed $content
  33. * @param string $from
  34. * @param string $to
  35. * @return mixed
  36. */
  37. public function charset($content, $from='gbk', $to='utf-8') {
  38. $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  39. $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  40. if (strtoupper($from) === strtoupper($to) || empty($content)) {
  41. //如果编码相同则不转换
  42. return $content;
  43. }
  44. if (function_exists('mb_convert_encoding')) {
  45. if (is_array($content)){
  46. $content = var_export($content, true);
  47. $content = mb_convert_encoding($content, $to, $from);
  48. eval("\$content = $content;");return $content;
  49. }else {
  50. return mb_convert_encoding($content, $to, $from);
  51. }
  52. } elseif (function_exists('iconv')) {
  53. if (is_array($content)){
  54. $content = var_export($content, true);
  55. $content = iconv($from, $to, $content);
  56. eval("\$content = $content;");return $content;
  57. }else {
  58. return iconv($from,$to,$content);
  59. }
  60. } else {
  61. return $content;
  62. }
  63. }
  64. }
  65. ?>