common.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * excel导出数据
  4. * @param $resHeader array 表头
  5. * @param $resData array 数据
  6. * @param $filename string 文件名
  7. * @param $strType array 需要设置成文本设置的单元格
  8. */
  9. function export_excel_data($resHeader, $resData, $filename, $strType = null)
  10. {
  11. if ($resHeader && $resData && $filename) {
  12. header("Content-type:application/vnd.ms-excel");
  13. header("Content-Disposition: attachment; filename=" . $filename . ".xls");
  14. echo "<style>
  15. br {mso-data-placement:same-cell;}
  16. </style>";
  17. echo "<table border='1'><tr>";
  18. $t_field = [];
  19. foreach ($resHeader as $ha => $h) {
  20. $t_field[] = $ha;
  21. echo "<th>" . $h . "</th>";
  22. }
  23. echo "</tr>";
  24. foreach ($resData as $dd => $d) {
  25. echo "<tr>";
  26. foreach ($t_field as $td => $t) {
  27. //判断是否合并单元格
  28. echo '<td';
  29. //判断是否设置单元格为文本
  30. if (is_array($strType) && in_array($t, $strType)) {
  31. echo ' style="vnd.ms-excel.numberformat:@"';
  32. }
  33. echo ">";
  34. //判断是否拆分单元格
  35. if (is_array($d[$t])) {
  36. echo '<table width=\'100%\' border=\'1\' rules=\'all\'>';
  37. foreach ($d[$t] as $rowValue) {
  38. echo '<tr><td>' . $rowValue . '</td></tr>';
  39. }
  40. echo '</table>';
  41. } else {
  42. echo $d[$t];
  43. }
  44. echo "</td>";
  45. }
  46. echo "</tr>";
  47. }
  48. echo "</table>";
  49. exit;
  50. }
  51. }
  52. /**
  53. * CSV导出
  54. * @param $file_name
  55. * @param $header
  56. * @param $list
  57. */
  58. function exportCsv($header, $list, $file_name)
  59. {
  60. static $source = '';
  61. if (empty($source)) {
  62. header('Content-type: text/csv; charset=utf-8');
  63. header('Content-Disposition: attachment;filename="' . $file_name . '.csv"');
  64. header('Cache-Control: max-age=0');
  65. // PHP文件句柄,php://output 表示直接输出到浏览器
  66. $source = fopen('php://output', 'a');
  67. // 写入列头
  68. foreach ($header as $k => $v) {
  69. // CSV的Excel支持GBK编码,一定要转换,否则乱码
  70. //$header[$k] = iconv('utf-8', 'gbk', $v);
  71. $header[$k] = $v;
  72. }
  73. fputcsv($source, $header);
  74. }
  75. foreach ($list as $key => $value) {
  76. $temp = [];
  77. foreach ($header as $k => $v) {
  78. //$temp[$k] = "\t" . iconv('utf-8', 'gbk', $value[$k]);
  79. $temp[$k] = "\t" . $value[$k];
  80. }
  81. fputcsv($source, $temp);
  82. ob_flush();
  83. flush();
  84. }
  85. }
  86. /*
  87. * 生成随机字符串
  88. *
  89. * $length 字符串长度
  90. */
  91. function randomStr($length)
  92. {
  93. // 密码字符集,可任意添加你需要的字符
  94. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  95. $str = '';
  96. for ($i = 0; $i < $length; $i++) {
  97. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  98. }
  99. return $str;
  100. }
  101. function get_image_url($file) {
  102. if (strpos($file, "http") === 0) {
  103. return $file;
  104. } else if (strpos($file, "/") === 0) {
  105. return cmf_get_domain() . cmf_get_root() . $file;
  106. } else {
  107. $storage = \cmf\lib\Storage::instance();
  108. return $storage->getPreviewUrl($file);
  109. }
  110. }
  111. /**
  112. * 生成唯一的订单号 20110809111259232312
  113. * 2011-年日期
  114. * 08-月份
  115. * 09-日期
  116. * 11-小时
  117. * 12-分
  118. * 59-秒
  119. * 2323-微秒
  120. * 12-随机值
  121. * @return string
  122. */
  123. function get_trade_no()
  124. {
  125. list($usec, $sec) = explode(" ", microtime());
  126. $usec = substr(str_replace('0.', '', $usec), 0, 4);
  127. $str = rand(10, 99);
  128. return date("YmdHis") . $usec . $str;
  129. }